fzero function with cases

1 回表示 (過去 30 日間)
Simo
Simo 2020 年 5 月 15 日
回答済み: Star Strider 2020 年 5 月 16 日
I have to search for the zero of a function that has 2 cases , but it doesn't work
Are there other way to define a function that has cases?
e = 0.1;
zeri=zeros(length(step),1);
i=1;
f=@(e,par) (par==1).*((sqrt(2*mass*e/h2))*sin(sqrt(2*mass*e/h2)*a)-(sqrt(2*mass*(vo-e)/h2))*cos(sqrt(2*mass*e/h2)*a))+(par==2).*((sqrt(2*mass*e/h2))*cos(sqrt(2*mass*e/h2)*a)+(sqrt(2*mass*(vo-e)/h2))*sin(sqrt(2*mass*e/h2)*a));
par = 1; % ^case 1 ^case 2
while e<vo
try y = fzero(f, [e,e+0.1]);
line([-a a],[y y],'Color','red');
zeri(i)=y;
i = i +1
if par==1
par=2
else
par=1
end
end
end
e = e +0.1

採用された回答

Star Strider
Star Strider 2020 年 5 月 16 日
The correct way to use ‘f’ with fzero in the posted code is:
y = fzero(@(e)f(e,par), [e,e+0.1]);
There are too many missing variables to be able to run your code (and I have no idea what you are doing with half of a try-catch block).
Also, there may be problems using the limits for fzero, since there may not be a zero-crossing between those values.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by