Erro in calling function in fsolve that has conditional operators

2 views (last 30 days)
Here is my full code:
s1=4;
s2=1.2;
r=0.3;
t1=2.52;
tu=37; %limit for qfunc
v1=sqrt(1-r^2);
v2=r/v1;
v3=exp(-s2^2/2);
c1=t1/v1;
c2=(s1-r*s2)/v1;
x2d=(c1+tu)/v2;
x2n=x2d-c2/v2;
F1=ferror(x2d,x2n)
Error=fsolve(@(x2) F1,0)
function F1=ferror(x2d,x2n,x2)
%case 1 x2<x2n
if x2 < x2n
F1= v3.*exp(x2.*s2).*qfunc(-c1+v2.*x2)/qfunc(-c1+c2+x2.*v2);
% case 2 x2n<x2<x2d
else if x2n <= x2 & x2 <= x2d
F1 = -(v3.*exp(x2.*s2-(-c1+v2.*x2)^2/2))/(qfunc((-c1+c2+x2.*v2))*sqrt(2*pi)*(-c1+v2.*x2));
%case 3 x2>x2d
%
else
F1 = v3.*((-c1+c2+x2.*v2)/(-c1+v2.*x2)).*exp(-(-c1+v2.*x2)^2/2+(-c1+c2+x2.*v2)^2/2+x2.*s2);
end
end
end
If I run this it says undefined variable x2
please help.

Accepted Answer

Matt J
Matt J on 4 Sep 2019
Edited: Matt J on 4 Sep 2019
Change these two lines,
%F1=ferror(x2d,x2n)
Error=fsolve(@(x2) ferror(x2d,x2n,x2),0)
Also, make sure to pass all variables that ferror() needs, like s2,c2, etc... or make ferror() a nested function.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!