Conditional statement problem in ODE code in Matlab

Y = -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)) && (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)) && (Y
  • #1
musikmaniac
1
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
  • #2
Your statements are syntactically wrong. Each if-else should have used &&:
if((yY(1)<=Y(1)) && (Y(1)<yY(2)))
 

1. What is a conditional statement in ODE code in Matlab?

A conditional statement in ODE code in Matlab is a statement that allows you to specify different actions to be taken based on certain conditions. It typically follows the "if-then-else" logic, where the code executes a certain set of instructions if a condition is met, and a different set of instructions if the condition is not met.

2. How do I write a conditional statement in ODE code in Matlab?

To write a conditional statement in ODE code in Matlab, you can use the "if-elseif-else" or the "switch-case" structures. These structures allow you to specify the conditions and the corresponding actions to be taken. You can also use logical operators such as "&&" (AND) and "||" (OR) to combine multiple conditions.

3. How do I troubleshoot a conditional statement problem in ODE code in Matlab?

If you encounter a problem with your conditional statement in ODE code in Matlab, the first step is to check your syntax and make sure you have correctly specified the conditions and actions. You can also use the "disp" or "fprintf" functions to print out the values of your variables and check if they are as expected. Additionally, you can use the "debugging" feature in Matlab to step through your code and identify any errors.

4. Can I use multiple conditional statements in ODE code in Matlab?

Yes, you can use multiple conditional statements in ODE code in Matlab. You can nest them within each other, or use them in combination with loops and other control structures to create complex conditions. However, it is important to keep your code organized and easy to follow to avoid any confusion.

5. Are there any common mistakes to avoid when using conditional statements in ODE code in Matlab?

Yes, some common mistakes to avoid when using conditional statements in ODE code in Matlab include not properly defining the variables used in the conditions, forgetting to use the "end" keyword to close the conditional statement, and using incorrect logical operators. It is also important to test your code with different conditions to ensure it works as intended.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
0
Views
1K
Back
Top