-1

Here I'm solving two simultaneous to get different values of the roots depending upon another variable 'x'. I've written up to the following using 'Find Root'

g[x_] = 2*x; 
f1[p_, q_] = 2*g[x]*p^4 + q^2;
f2[p_, q_] = 5 p^2 + q - g[x]; 
For[x = 0, x < 10, x = x + 1, 
  Print[{p, q, x} /.
    FindRoot[{f1[p, q] - 5 == 0, f2[p, q] - 10 == 0}, {{p, 5}, {q, 1}}]]];

Now can I export this in a data file (three column)?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Doon
  • 319
  • 2
  • 8
  • Have you looked at Export? – Yves Klett Mar 02 '15 at 11:31
  • Trying to figure out how to use that here. – Doon Mar 02 '15 at 11:39
  • It's kind of hard to comment on this without working code. (e.g. define f1, f2, fix the {{p,1},{p,2}} etc.) – djp Mar 02 '15 at 12:23
  • Something like this 'g[x_] = 2x; f1[p_, q_] = 2g[x]*p^4 + q^2; f2[p_, q_] = 5 p^2 + q - g[x]; For[x = 0, x < 10, x = x + 1, Print[FindRoot[{f1[p, q] - 5 == 0, f2[p, q] - 10 == 0}, {{p, 5}, {q, 1}}]]];' – Doon Mar 02 '15 at 12:32

1 Answers1

2

Use Table, not For:

output = Table[
    {p, q, x} /. FindRoot[{f1[p, q] - 0.03 == 0, f2[p, q] - 0.0234 == 0}, {{p, 1}, {q, 2}}],
    {x, 0, 359}
  ]

Look in $ExportFormats to find the format you want.

Export["output.csv", output]
djp
  • 1,493
  • 14
  • 18
  • I've fixed the {{p,1},{p,2}} thing. I think it's very odd that your code doesn't use x anywhere. – djp Mar 02 '15 at 12:29
  • Can one use 'Export' when we are using 'For'? – Doon Mar 02 '15 at 12:45
  • Don't use For. http://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/18396#18396 – djp Mar 02 '15 at 13:02