Undefined variable within ODE45 code

1 view (last 30 days)
Hi!
I´m solving a differencial ecuation system in Matlab with ODE45. I´ve defined a variable within a double if condictional but when I play the function, return me that this variable isnt defined. If I define the variable before the 'if' there isnt problem, but the solution is false. Somebody knows the problem?. The function is called by the external file 'RK'.
Thank you so much

Accepted Answer

Walter Roberson
Walter Roberson on 13 Dec 2015
You have
if -pi<=O<=0
This means
if ((-pi<=O)<=0)
The (-pi<=O) part returns 0 (false) or 1 (true). That result is then compared to 0 by your <=0, and that is going to be a match only if the -pi<=O part returned 0, false. The overall expression is then
if ~(-pi<=O)
which is the same as
if (pi>O) || isnan(O)
If you want to test O against a range you need to use two comparisons
if -pi<=O && O<= 0
Later on you have a large series of if/elseif statements in which there is no final "else" to handle the possibility that none of the tested conditions matched. When that happens you are not going to be assigning values to some variables. When you have an if/elseif chain, you should always end with a final "else"; you can put in warning() or error() calls there if you do not expect to get there, and if you know the situation to be possible but you do not happen to have any code to execute there then make the "else" a series of comments explaining what the situation is and why you do not need to execute anything.
  1 Comment
Antonio José Jiménez
Antonio José Jiménez on 15 Dec 2015
First of all, thank you for you answer.
But, when I solve this problem, the programm doesnt read the large series of if/elseif and else, so I dont get solution. What can it be?

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!