Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Task: Finding roots in loop

t = List[1, 2, 3, 4, 5]
fx[x_] := a*x^2 - 5
List[Do[Print[FindRoot[fx[k] == 1, {a, 1}]], {k, 0, 5}]]

Output: Currently the output is as follows:

 {a-> 6}
 {a-> 1.5}

and so forth

But I want to have the values of a in a List in order to perfom further tasks (Plotting root of f_a against a)

share|improve this question
3  
Using Table and Map are things you should get really comfortable with in general, so look them up in documentation center, and browse around here a bit: wolfram.com/support/learn –  ssch Feb 11 '13 at 16:06
1  
Please try to avoid procedural loops –  belisarius Feb 11 '13 at 16:31
2  
Don't use Print to generate values that you want to process further on. ssch has good advice. The List you use doesn't make sense here either. Another advice: Generally you should not define functions that have more dependencies than shown by the variables in the call pattern. In your case, the function depends on a as well, but that is not made explicit. –  Sjoerd C. de Vries Feb 11 '13 at 16:48
add comment

1 Answer 1

You have many many options. Here are just two:

Table[FindRoot[fx[k] == 1, {a, 1}], {k, t}]

Or

FindRoot[fx[#] == 1, {a, 1}] & /@ t

Here is one way to make a plot of t vs the roots:

pts = Table[{k, a /. FindRoot[fx[k] == 1, {a, 1}]}, {k, t}];
ListLinePlot@pts

Example plot of t vs roots

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.