0
$\begingroup$

I want to find the zero of a function that is a function of a numerical approximation. The simplest example that mimics my real problem I could find is:

fx[y_, z_] := 
 FindRoot[0.25*x + Log[x] - Log[y] + 0.5*z, {x, 5}][[1, 2]]
g[y_, z_] := 0.1*fx[y, z] + 0.1*z
h[y_, z_] := 0.5*fx[y, z] - 2*z
condition[y_, z_] := 0.3 - g[y, z] + 0.6*h[y, z]

We can clearly see that, for a given z, there is y such that condition[y,z]=0 holds:

tabletest = Table[{yy, condition[yy, 1]}, {yy, 27, 31, 0.1}];
ListLinePlot[{tabletest}, 
 AxesLabel -> {"y", 
   Row[{"Condition"}, BaseStyle -> {FontFamily -> "Libertine"}]}]

enter image description here However, when I try to solve for condition[y,z]==0 I get an error:

Solve[condition[yy, 1] == 0 && 28 <= yy <= 29, yy]

"The function value {-1.77759-1.\ Log[yy]} is not a list of numbers \ with dimensions {1} at {x} = {0.1}. "

I am clearly doing something wrong but I can't tell what. Anyone can help me?

$\endgroup$
  • $\begingroup$ The function fx[y, z] evaluates to Log[x]. Since there is no numerical value assigned to x, this is "not a list of numbers". What do you expect fx[y,z] to be? $\endgroup$ – bill s Nov 5 '17 at 20:14
  • $\begingroup$ fx[y,z] gives me the value x such that fx[y,z]==0. For example. fx[29, 1]=5.01748. But if I want to find the root of fx[y,1] such that y obeys condition[yy, 1] == 0 I can't, for some reason. $\endgroup$ – Laura K Nov 5 '17 at 20:18
3
$\begingroup$

Functions that directly or indirectly use numeric techniques should be restricted to numeric arguments with NumericQ

fx[y_?NumericQ, z_?NumericQ] :=
  FindRoot[1/4*x + Log[x] - Log[y] + 1/2*z, {x, 5}][[1, 2]];
g[y_?NumericQ, z_?NumericQ] := (fx[y, z] + z)/10;
h[y_?NumericQ, z_?NumericQ] := 1/2*fx[y, z] - 2*z;
condition[y_?NumericQ, z_?NumericQ] := 3/10 - g[y, z] + 3/5*h[y, z];

Since you know that 28 <= yy <= 29 use FindRoot rather than Solve

soln = FindRoot[condition[yy, 1] == 0, {yy, 28.5}]

(* {yy -> 28.773} *)

Plot[condition[yy, 1], {yy, 27, 31}, Epilog -> {Red,
   AbsolutePointSize[6], Point[{yy, condition[yy, 1]} /. soln]}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Of course. That is great. Thank you very much, Bob Harlon! :) $\endgroup$ – Laura K Nov 5 '17 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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