1
$\begingroup$

This question already has an answer here:

I am trying to plot a function which is based on the solution of some equation; I could get the correct figure but only after lots of warnings are printed out. The following is a minimum version of my problem:

solx[z_] := FindRoot[x z == 40, {x, 1.}]
r[z_, f_] := f x /. solx[z];
Plot[Evaluate[Table[r[z, f], {f, 1., 5., 1.}]], {z, 1., 5.}]

Then I got the following warning messages:

FindRoot::nlnum: The function value {-40.+1. z} is not a list of numbers with dimensions {1} at {x} = {1.}.

ReplaceAll::reps: {FindRoot[x z==40,{x,1.}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

FindRoot::nlnum: The function value {-40.+1. z} is not a list of numbers with dimensions {1} at {x} = {1.}.

...

What can be done to avoid the warning messages? (I have tested that if I remove the $f$-dependence and plot $r[z]$, there is no warning messages, i.e.,

solx[z_] := FindRoot[x z == 40, {x, 1.}]
r[z_] := x /. solx[z];
Plot[r[z], {z, 1., 5.}]

works without any problem.)

$\endgroup$

marked as duplicate by Michael E2, Öskå, Yves Klett, bobthechemist, m_goldberg Jul 1 '14 at 19:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
$\begingroup$

The reason for the warning is that in your case mathematica tries to evaluate the function symbolically at one point and then passes numerical values to obtain function values for plotting. The symbolic value causes the warning, because your function contains FindRoot, which can only operate with numerical parameters z.

To see this behavior you can try the following:

Plot[Print[x]; x, {x, 0, 1}]

You should see that x is passed as an argument the second time Plot calls the function to be evaluated.

To suppress this symbolic evaluation you could either rely on mathematica figuring out that it should not do it by removing Evaluate in your Plot call or you could explicitly specify that your function only takes numeric values by using parameter testing with NumericQ, which in your case would be:

solx[z_] := FindRoot[x z == 40, {x, 1.}]
r[z_?NumericQ, f_] := f x /. solx[z];
Plot[Evaluate[Table[r[z, f], {f, 1., 5., 1.}]], {z, 1., 5.}]

For more details you might want to take a look at: How does Plot work?

$\endgroup$
  • $\begingroup$ Thanks, that solves my problem. I also understand better the use of the functions from the simple example and the alternative solutions. $\endgroup$ – Hao Wang Jul 1 '14 at 16:08
1
$\begingroup$

Remove the Evaluate in your code example. Stangely this works even without NumericQ, but the lines are not colored differently. Michael's comment shows a better approach.

solx[z_] := FindRoot[x z == 40, {x, 1.}]
r[z_, n_] := n x /. solx[z];
Plot[Table[r[z, n], {n, 1., 5.}], {z, 1., 5.}, Ticks -> {Automatic, Range[40, 200, 40]}]

enter image description here

$\endgroup$
  • $\begingroup$ For different colored curves, wrap the first argument of the Plot expression with Evaluate[...]. $\endgroup$ – murray Jul 1 '14 at 14:40

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