# Problem with plotting a function that calls FindRoot [closed]

I am having difficulties in plotting the solution of FindRoot to solve 4 variables. I defined a function and then tried tp plot it, but got an error message concerning the last parameter. However, using FindRoot outside the function gives a result. Could you please helps?

ClearAll["Global*"]
EU = Log[#] &;

R = 1.6
pi = 0.35
λh = .81
λl = .79

ys2[pi_, λh_, λl_, R_, ph2_, pl2_, po2_] := ((pl2 - po2) (-pi pl2 po2 + ph2 (pl2 + (-1 + pi) po2)) λh + (-(ph2 - po2) (-pi pl2 po2 + ph2 (pl2 + (-1 + pi) po2)) + (ph2 - pl2) (ph2 pi + pl2 - pi pl2 - po2) po2 λh) λl)/((ph2 - po2) (-pl2 + po2) (-pl2 λh + po2 (λh - λl) + ph2 λl));

ds2[pi_, λh_, λl_, R_, ph2_, pl2_,po2_] := ((ph2 - pl2) (pi (λh - λl) + λl))/(-pl2 \λh + po2 (λh - λl) + ph2 λl);

yr2[pi_, λh_, λl_, R_, ph2_, pl2_, po2_] := ph2 (-1 + pi) + (pi pl2)/(pl2 - po2) + (ph2^2 (-1 + pi))/(-ph2 + po2);

dr2[pi_, λh_, λl_, R_, ph2_, pl2_,po2_] := ((-1 + pi) (ph2 pl2- (1 + ph2) pl2 po2 + ph2 po2^2))/(po2 (-ph2 + po2));

(*SAFE BANK*)
ysff2 = ((pl2 - po2) (-pi pl2 po2 +ph2 (pl2 + (-1 + pi) po2)) λh + (-(ph2 -po2) (-pi pl2 po2 + ph2 (pl2 + (-1 + pi) po2)) + (ph2 -pl2) (ph2 pi + pl2 - pi pl2 -po2) po2 λh) λl)/((ph2 - po2) (-pl2 +po2) (-pl2 λh + po2 (λh - λl) +ph2 λl));
dsff2 = ((ph2 - pl2) (pi (λh - λl) + λl))/(-pl2 \λh + po2 (λh - λl) + ph2 λl);
c1hs2 = dsff2;
c2hs2 = ((ysff2 + ph2*((1 - ysff2)/po2) - λh*dsff2)/((1 - λh)*(ph2/R)));
c1ls2 = dsff2;
c2ls2 = ((ysff2 + pl2*((1 - ysff2)/po2) - λl*dsff2)/((1 - λl)*(pl2/R)));

(*RISKY BANK*)
yrff2 = ph2 (-1 + pi) + (pi pl2)/(pl2 - po2) + (ph2^2 (-1 + pi))/(-ph2 + po2);
drff2 = ((-1 + pi) (ph2 pl2 - (1 + ph2) pl2 po2 + ph2 po2^2))/(po2 (-ph2 +po2));
c1hr2 = (yrff2 + ph2*(1 - yrff2/po2));
c2hr2 = (yrff2 + ph2*(1 - yrff2/po2));
c1lr2 = drff2;
c2lr2 = ((yrff2 + pl2*(1 - yrff2/po2) - λl*drff2)/((1 - λl)*(pl2/R)));


Then I define function to plot graph of FindRoot

mktL2[pi_, λh_, λl_, R_, ph2_, pl2_,po2_] := (ys2[pi, λh, λl, R, ph2, pl2,po2] - λl*ds2[pi, λh, λl, R, ph2, pl2, po2])/(1 - ys2[pi, λh, λl, R, ph2, pl2, po2]) - (λl*dr2[pi, λh, λl, R, ph2, pl2, po2] - yr2[pi, λh, λl, R, ph2, pl2, po2])/yr2[pi, λh, λl, R, ph2, pl2, po2]

mktH2[pi_, λh_, λl_, R_, ph2_, pl2_, po2_] := (ys2[pi, λh, λl, R, ph2, pl2, po2] - λh*ds2[pi, λh, λl, R, ph2, pl2, po2])/(1 - ys2[pi, λh, λl, R, ph2, pl2, po2]) - ph2*(1 - yr2[pi, λh, λl, R, ph2, pl2, po2]/po2)/yr2[pi, λh, λl, R, ph2, pl2, po2]

indif2[pi_, λh_, λl_, R_, ph2_, pl2_, po2_] := (pi*(λh*EU[c1hs2] + (1 - λh)*EU[c2hs2]) + (1 - pi)*(λl*EU[c1ls2] + (1 - λl)*EU[c2ls2])) - (pi*(λh*EU[c1hr2] + (1 - λh)*EU[c2hr2]) + (1 - pi)*(λl*EU[c1lr2] + (1 - λl)*EU[c2lr2]))

mkt02[pi_, λh_, λl_, R_, ph2_, pl2_, po2_, ρ2_] := ρ2*(1 - ysff2) - (1 - ρ2)*yrff2]

f[R_?NumericQ] := FindRoot[{mktL2[pi, λh, λl, R, ph2, pl2, po2], mktH2[pi, λh, λl, R, ph2, pl2, po2],indif2[pi, λh, λl, R, ph2, pl2, po2],mkt02[pi, λh, λl, R, ph2, pl2, po2, ρ2]}, {{ph2, 0.719, 0, 1}, {pl2, R, 1, R}, {po2, 1.046, 0.1, R}, {ρ2, 0.3, 0, 1}}][[1, 2]]

Plot[f[R], {R, 1.5, 3}]


For first three variables, ph,pl, and po, I can get the graphs but for the last variable, Rho, there was always an error message popping up

FindRoot::reged: The point {0.721957,1.47528,1.03537,1.} is at the edge of the search region {0.,1.} in coordinate 4 and the computed search direction points outside the region. >>

However, when I use FindRoot alone without defining function to it, I could get the result.

{ph2 -> 0.668173, pl2 -> 1.34191, po2 -> 1.0526, ρ2 -> 0.811842}


• When i copy the code, there are numerous syntax errors, so I'm not sure if you pasted it incorrectly. Specifically, the two instances of \\[Lambda]h and the extraneous closing bracket. Then, to your issue, you are restricting the range for the four variables, and sometimes FindRoot` doesn't find a decent answer within that region. – Jason B. Dec 3 '15 at 15:57