MATLAB Conditional statement problem in ODE code in Matlab

AI Thread Summary
The discussion centers on a MATLAB user encountering an error related to integration tolerances while attempting to implement conditional statements in their code. The user identifies that the issue arises from the way the conditional statements are structured, specifically the use of chained comparisons like "yY(1)<=Y(1)<yY(2)". This syntax is incorrect in MATLAB, as it does not support chained comparisons. Instead, the correct approach involves using the logical AND operator "&&" to combine conditions, such as "if((yY(1)<=Y(1)) && (Y(1)<yY(2)))". This adjustment is crucial for the code to function properly and for the ODE solver to interpret the conditions correctly.
musikmaniac
Messages
1
Reaction score
0
I am new in MATLAB and recently, I have tried coding in matlab. So far, I have been getting error msg

"Warning: Failure at t=2.859413e+002. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (1.015868e-012) at time t."

I have referred back to my codes and realized that the problems occurs due to the conditional statement. To cut the story short, the ode doesn't seem to want to read the if, else-if statement in my codes.

e.g

if yY(1)<=Y(1)<yY(2)
hY = -0.0014*(Y(1)-yY(1))^3+ 0.0200*(Y(1)-yY(1))^2+ 0.8515*(Y(1)-yY(1));
elseif yY(2)<=Y(1)<yY(3)
hY = -0.0014*(Y(1)-yY(2))^3+ 0.0151*(Y(1)-yY(2))^2+ 0.8935*(Y(1)-yY(2))+ 1.0431;
elseif yY(3)<=Y(1)<yY(4)
hY = -0.0012*(Y(1)-yY(3))^3+ 0.0088*(Y(1)-yY(3))^2+ 0.9306*(Y(1)-yY(3))+ 2.4600;
else
hY = 1.0000*(Y(1)-yY(19))+ 510.8083;
end

It only wants to read the first highlighted piece of the code.

Anyone help please!
 
Physics news on Phys.org
Your statements are syntactically wrong. Each if-else should have used &&:
if((yY(1)<=Y(1)) && (Y(1)<yY(2)))
 

Similar threads

Back
Top