Erro in calling function in fsolve that has conditional operators

2 次查看(过去 30 天)
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.

采纳的回答

Matt J
Matt J 2019-9-4
编辑:Matt J 2019-9-4
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by