Solving ODE in Matlab with If Statements

  • Context: MATLAB 
  • Thread starter Thread starter sristi89
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

This discussion focuses on solving a first-order ordinary differential equation (ODE) in MATLAB using an iterative approach with conditional statements. The user implements a numerical solution for the equation dy/dt = K*(y-s) with initial conditions set for K, s, and y0. The challenge arises when the solution exceeds a threshold of 150, requiring a reset of the initial condition to 100. The proposed solutions involve modifying the for loop to incorporate if statements that adjust the calculation of y based on its value at each timestep.

PREREQUISITES
  • Understanding of ordinary differential equations (ODEs)
  • Proficiency in MATLAB programming
  • Familiarity with numerical methods for solving ODEs
  • Knowledge of conditional statements in programming
NEXT STEPS
  • Explore MATLAB's built-in functions for solving ODEs, such as ode45
  • Learn about MATLAB's vectorization techniques to optimize performance
  • Investigate advanced numerical methods for ODEs, like Runge-Kutta methods
  • Study error handling and debugging techniques in MATLAB for iterative algorithms
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those working with numerical simulations of ODEs, as well as students and professionals in engineering and applied mathematics looking to enhance their coding skills and problem-solving techniques in MATLAB.

sristi89
Messages
8
Reaction score
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
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K