MATLAB Answers

Why function crashes when using conditional statements?

7 views (last 30 days)
Tara Behmanesh
Tara Behmanesh on 2 Jul 2020
Commented: Geoff Hayes on 5 Jul 2020
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

Answers (1)

Geoff Hayes
Geoff Hayes on 2 Jul 2020
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)
% 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
Note tha I changed the above code to use eps instead of 0.1 which may or may not be appropriate for your code.
Geoff Hayes
Geoff Hayes on 5 Jul 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by