# Solving a function that carries a numerical approximation as variable

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"}]}]


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?

• 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? – bill s Nov 5 '17 at 20:14
• 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. – Laura K Nov 5 '17 at 20:18

## 1 Answer

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]}]


• Of course. That is great. Thank you very much, Bob Harlon! :) – Laura K Nov 5 '17 at 20:43