Conditional statement problem in ODE code in Matlab

Click For Summary
SUMMARY

The discussion centers on a conditional statement issue in MATLAB, specifically regarding the use of if-else statements in ODE code. The user encounters a warning about integration tolerances and identifies that the conditional statements are not functioning as intended. The solution provided emphasizes the need to use logical operators correctly, specifically replacing chained comparisons with the logical AND operator (&&) to ensure proper evaluation of conditions. This adjustment resolves the issue, allowing the ODE solver to interpret the conditions accurately.

PREREQUISITES
  • Understanding of MATLAB syntax and programming constructs
  • Familiarity with ODE (Ordinary Differential Equations) solvers in MATLAB
  • Knowledge of logical operators in programming
  • Basic mathematical modeling concepts
NEXT STEPS
  • Review MATLAB documentation on conditional statements and logical operators
  • Explore MATLAB's ODE solver functions, such as ode45 and ode23
  • Learn about error handling and debugging techniques in MATLAB
  • Investigate best practices for writing efficient conditional statements in MATLAB
USEFUL FOR

New MATLAB users, students learning numerical methods, and developers working with ODEs who need to troubleshoot conditional logic in their code.

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

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
3K