-1
$\begingroup$

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)?

$\endgroup$
4
  • $\begingroup$ Have you looked at Export? $\endgroup$
    – Yves Klett
    Mar 2, 2015 at 11:31
  • $\begingroup$ Trying to figure out how to use that here. $\endgroup$
    – Doon
    Mar 2, 2015 at 11:39
  • $\begingroup$ It's kind of hard to comment on this without working code. (e.g. define f1, f2, fix the {{p,1},{p,2}} etc.) $\endgroup$
    – djp
    Mar 2, 2015 at 12:23
  • $\begingroup$ Something like this '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[FindRoot[{f1[p, q] - 5 == 0, f2[p, q] - 10 == 0}, {{p, 5}, {q, 1}}]]];' $\endgroup$
    – Doon
    Mar 2, 2015 at 12:32

1 Answer 1

2
$\begingroup$

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]
$\endgroup$
3
  • $\begingroup$ I've fixed the {{p,1},{p,2}} thing. I think it's very odd that your code doesn't use x anywhere. $\endgroup$
    – djp
    Mar 2, 2015 at 12:29
  • $\begingroup$ Can one use 'Export' when we are using 'For'? $\endgroup$
    – Doon
    Mar 2, 2015 at 12:45
  • $\begingroup$ Don't use For. mathematica.stackexchange.com/questions/18393/… $\endgroup$
    – djp
    Mar 2, 2015 at 13:02

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