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에서 Schedule Model Components에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by