Solving ODE in Matlab with If Statements

  • MATLAB
  • Thread starter sristi89
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses solving a ODE numerically in Matlab. The code is set up with specific values for K, s, y0, npoints, and dt. The for loop is used to calculate the solution at each timestep, but there is a problem with resetting the initial condition if the value of y(step+1) goes over 150. Possible solutions are discussed, including recalculating y(step+1) with the reset value or leaving it unchanged and only recalculating the next datapoint.
  • #1
sristi89
8
0
Hi everyone,

So I have to solve a ODE (for dy/dt = K*(y-s) ) numerically in Matlab.

This is how I set it up:
K = 1;
s = 20;
y0 = 100;
npoints = 50;
dt = 0.1;
y = zeros(npoints,1); % this initializes the vector y to being all zeros
t = zeros(npoints,1);
y(1) = y0; % the initial condition
t(1) = 0.0;
for step=1:npoints-1 % loop over the timesteps
y(step+1) = y(step) + dt*K*(y(step)-s);
t(step+1) = t(step) + dt;
end
plot(t,y,'r'); %plots the numerical solution in red
hold on; %keep the previously plotted lines


My problem is that I have to "reset" the initial condition if the value of V(step+1)>150 with the initial condition (100). Then I have to let the loop continue with the new value.

I tried a bunch of combinations of the if statement in the for loop, but I can't make it work.
Any help would be really appreciated. Thanks!
 
Physics news on Phys.org
  • #2
I'm not sure I understand exactly, but if y(step+1) has to be recalculated with the reset value when it goes over 150:

Code:
for step=1:npoints-1 % loop over the timesteps 
  y(step+1) = y(step) + dt*K*(y(step)-s); 
  if(y(step+1)>150)
    y(step+1) = y0 + dt*K*(y0-s); 
  end
  t(step+1) = t(step) + dt; 
end

If y(step+1) is to be unchanged and only the next datapoint has to be calculated with the reset value, then
Code:
for step=1:npoints-1 % loop over the timesteps 
  if(y(step)>150)
    y(step+1) = y0 + dt*K*(y0-s); 
  else
    y(step+1) = y(step) + dt*K*(y(step)-s); 
  end
  t(step+1) = t(step) + dt; 
end
 
Last edited:

Related to Solving ODE in Matlab with If Statements

1. How do I solve an ODE in Matlab using If Statements?

To solve an ODE in Matlab using If Statements, you can use the ode45 function. This function solves differential equations numerically using a Runge-Kutta method. You can include If Statements in the ODE function to add conditional behavior to the solution.

2. Can I use If Statements to solve only certain parts of the ODE?

Yes, you can use If Statements to solve only certain parts of the ODE. Within the ODE function, you can specify conditions for when the If Statement should be applied. This allows for more flexibility in solving complex ODEs.

3. What is the benefit of using If Statements in solving ODEs?

Using If Statements in solving ODEs allows for more control over the solution. It allows for the inclusion of conditional behavior, which can be useful for modeling real-world systems that may have changing parameters or conditions.

4. Are there any limitations to using If Statements in solving ODEs?

One limitation is that If Statements can make the ODE function more complex and potentially slower to run. It is important to carefully consider the use of If Statements and ensure they are necessary for the problem at hand.

5. Can I use multiple If Statements in one ODE function?

Yes, you can use multiple If Statements in one ODE function. This can be useful for solving ODEs with multiple conditions or for implementing more complex conditional behavior. However, it is important to use If Statements sparingly to avoid unnecessarily complex code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
662
  • Engineering and Comp Sci Homework Help
Replies
2
Views
888
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top