Skipping fzero error in a loop

2 ビュー (過去 30 日間)
Saeid
Saeid 2017 年 4 月 14 日
回答済み: Star Strider 2017 年 4 月 14 日
I am trying to solve an algabraic equation with two parameters that are varying in a loop. The basic form of the program is as follows:
nn=linspace(0.01,1,10);
aa=logspace(0,3,10);
inn=length(nn);
iaa=length(aa);
ss=1+1./nn;
for ii=1:inn
for jj=1:iaa
ii
jj
Y0=[0 1];
fun=@(Y) (1+Y)^ss(ii)-Y^ss(ii)-ss(ii)*aa(jj);
YY(ii,jj)=fzero(fun,Y0)
end
end
Now at some point at values of ii=2 & jj=7 the programs figures that a solution is not available, and stops. The error message reads: "The function values at the interval endpoints must differ in sign.", which understandably is due to the nature of the
Is there a way to get the program to keep running?

採用された回答

Star Strider
Star Strider 2017 年 4 月 14 日
Yes!
Now I suppose you want to know how?
Define: ‘Y0=1’, not a vector of values. This gives you a matrix with many NaN values and many others with solutions outside ‘[0 1]’. You can convert these to NaN on your own later if you like, with:
YY(YY>1) = NaN;
The point is that the code completes and fills your matrix.

その他の回答 (1 件)

the cyclist
the cyclist 2017 年 4 月 14 日
One possible solution is to use a try-catch control structure.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by