5
$\begingroup$

I know this has been asked, but I cannot seem to understand the solutions. What I need to know, because I have my first domiciliary test this Monday, is how to plot the output from FindRoot for a system of equations as the parameters vary

I havent even been able to do it with one equation either

Say I have define

g[x_, y_] := 2 x + y

I can define the function

t[y_] := FindRoot[g[x, y], {x, 1}]

Wich does give me an answer for different y, but when I plot

Plot[t[x], {x, 0, 1}]

It wont do anything. I mean, the axis appear but nothing will be drawn on it.

If I had a system of equations, FindRoot would give several values, say {x, y}.

How can I draw x[d], y[d], where this last two are the roots of FindRoots for different values of parameter d?

Thank you very much and I am very sorry if this is really simple but I havent been able to solve it.

$\endgroup$
8
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Jul 15, 2015 at 1:38
  • $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. $\endgroup$
    – Michael E2
    Jul 15, 2015 at 1:38
  • $\begingroup$ Have you look at the many other questions about plotting and FindRoot? You may find some help there. $\endgroup$
    – Michael E2
    Jul 15, 2015 at 1:39
  • $\begingroup$ I did. I try with several, specially making changes to this one, but in the solution i can not understand what [[1,2]] are, but i fI remove then it does not plot and it gives errors mathematica.stackexchange.com/questions/23573/… $\endgroup$ Jul 15, 2015 at 1:41
  • $\begingroup$ Many of the answers suggest trying t[y_?NumericQ] := .... See mathematica.stackexchange.com/questions/18393/… for more information. -- oh, and be sure to ClearAll[t] before trying the ?NumericQ fix. -- and thanks for editing the question. $\endgroup$
    – Michael E2
    Jul 15, 2015 at 1:52

2 Answers 2

8
$\begingroup$

Thank you Michael!! I will write the answer here for other people.

t[y_?NumericQ] := x /. FindRoot[g[x, y], {x, 1}]

worked like charm

$\endgroup$
1
  • 1
    $\begingroup$ First @ FindRoot will have your function return a number instead of a list. $\endgroup$ Jul 15, 2015 at 2:11
2
$\begingroup$

For multiple results you could do for example:

g1[x_, y_, z_] := 2 x + y - z
g2[x_, y_, z_] := x^2 - y^3 - z^2
t[x_] := t[x] = {y, z} /. FindRoot[{g1[x, y, z], g2[x, y, z]}, {{y, 1}, {z, 1}}]
Plot[{t[x][[1]], t[x][[2]]}, {x, 1, 5}, PlotStyle -> {Red, Green}]

Mathematica graphics

ContourPlot3D[{g1[x, y, z] == 0, g2[x, y, z] == 0}, {x, 0, 5}, {y, 0, -5}, {z, 0, 5}]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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