Why function crashes when using conditional statements?

Hi In function "chapter" there is a statement that for some reason denominator of a fraction in that statement my be zero. So I need to check and see if it is less than a particular amount (for example 0.1) I should replace the denominator with 1 . But when i tried if-else and switch-case and both of them caused crash. Hope you can help me :) Sorry for my bad English.
Matlab R2018a

 採用された回答

Geoff Hayes
Geoff Hayes 2020 年 7 月 2 日

0 投票

Tara - try replacing the switch with an if and use a tolerance check (note that using a switch statement for doubles or floats will not work as expected because of how floating point numbers are represented - see Floating-Point Numbers for more details)
pa=2*0.11*(1-(F(3)-Fa0)/Fa0);
pc=2*0.11*(F(3)-Fa0)/Fa0;
% the amount of pc should be checked .if pc is less than eps it should be
% replaced with "1" in ''sqrt(pa/pc)'' term.
if abs(pc) < eps
ra=-k.*sqrt(pa).*(pa-(pc/kp.*pa).^2);
else
ra=-k.*sqrt(pa/pc).*(pa-(pc/kp.*pa).^2);
end
Note tha I changed the above code to use eps instead of 0.1 which may or may not be appropriate for your code.

6 件のコメント

Tara Behmanesh
Tara Behmanesh 2020 年 7 月 2 日
Thank you :) but it didn't work No error and no output
Geoff Hayes
Geoff Hayes 2020 年 7 月 3 日
Tara - please clarify what you mean by no output. Was there nothing drawn to your figure or did the code not complete (i.e. running a very long time)? When I try running your code, it seems to run for a while without completing. Also, the chapter function doesn't seem to use the first input w. Is this intentional? You may need to step through your code to ensure that it is doing what you expect it to be doing.
Tara Behmanesh
Tara Behmanesh 2020 年 7 月 3 日
yes it runs for while without completing. And about w: the code should solve the equation(set of ODEs) for w from zero to seven. In fact w is the independent variable. Range that set of ODEs should be solved in. and thanks again 🌺
Geoff Hayes
Geoff Hayes 2020 年 7 月 3 日
Tara - you may want to review the chapter code to ensure that it is doing what you expect. I don't understand enough of what it is doing to offer any (good) advice.
Tara Behmanesh
Tara Behmanesh 2020 年 7 月 4 日
編集済み: Tara Behmanesh 2020 年 7 月 4 日
Hi again I ran my function and let it be for some minutes and then I paused it I think the problem is in this line but I have no idea what it is about? Can you guide me?
In ode45.m Line 344
err=absh*norm((fE)) ./ max(max(abs(y),abs(ynew)),threshold),inf);
if nonNegative && (err <= rtol) && any(ynew(idxNonNegative)<0)
I understand that in code above there are many variables that I didn't mention the definition but I was wondering maybe you know what is that if statement about? (What it checks?) Really appreciated
Geoff Hayes
Geoff Hayes 2020 年 7 月 5 日
Tara - that code is part of the ode45 function from MATLAB which would have been executing when you paused the program. I don't think that there is any reason to believe that there something incorrect about this line. I still think that you need to review your chapter code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by