MATLAB: Fzero, Function value at starting guess must be finite and real.

fzero

Hi Everyone, my code is:
format long;
b=50;a=3;e=50;
spdf = @(x) 3.*(50.^3)./(x+50).^(3+1)./(1-(50./(50+50)).^3);
fun2 = @(x,u,m) min(x,max(0,fzero(@(y) 0.5.*(100-12-x+y).^(-0.5)-m-2.*u.*y,0))).^2.*spdf(x);
fun3 = @(u,m) integral(@(x) fun2(x,u,m),0,50,'arrayvalued', true);
fun21 = @(x,u,m) min(x,max(0,fzero(@(y) 0.5.*(100-12-x+y).^(-0.5)-m-2.*u.*y,0))).*spdf(x);
fun31 = @(u,m) integral(@(x) fun21(x,u,m),0,50,'arrayvalued', true);
[us,ms] = solve([fun31(u,m)-12==0, fun3(u,m)-178.57145==0], [u m])
The error is:
Error using fzero (line 328)
Function value at starting guess must be finite and real.
I try to solve two equations with two parameters. But it invloves fzero in my equation. One staff gave me some suggestion. But I didn't figure it out. Anyone can help me fix my code? Any help is very appreciated!

Best Answer

This runs without error, although it does not appear to change the initial parameter estimates during the optimisation:
% % % z(1) = u1, z(2) = m1
b=50;a=3;e=50;w=100;p=12;
spdf = @(x) a.*(b.^a)./(x+b).^(a+1)./(1-(b./(e+b)).^a);
fun2 = @(x,z) min(x,max(0,fsolve(@(y) 0.5.*(w-p-x+y).^(-0.5)-z(2)-2.*z(1).*y,0))).^2.*spdf(x);
fun3 = @(z) integral(@(x) fun2(x,z),0,e,'arrayvalued', true);
fun21 = @(x,z) min(x,max(0,fsolve(@(y) 0.5.*(w-p-x+y).^(-0.5)-z(2)-2.*z(1).*y,0))).*spdf(x);
fun31 = @(z) integral(@(x) fun21(x,z),0,e,'arrayvalued', true);
[Z,fval] = fsolve(@(z)[fun31(z)-12; fun3(z)-178.57145], [1; 1])
fprintf(1, 'u = %.5f\nm = %.5f\n\n', Z)
I will let you pursue that.