If statement in Matlab involving time variable

Click For Summary
SUMMARY

The discussion centers on implementing an if statement in MATLAB that utilizes the time variable from the ode45 function. The user seeks guidance on whether a new variable is necessary, but the solution involves using the existing time variable T directly within a for loop. The recommended approach is to iterate through each value of T using a for loop and apply the if statement to check for specific conditions.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of the ode45 function for solving ordinary differential equations
  • Knowledge of for loops and conditional statements in MATLAB
  • Basic understanding of how to manipulate arrays in MATLAB
NEXT STEPS
  • Review MATLAB documentation on the ode45 function for deeper insights
  • Practice writing for loops and if statements in MATLAB
  • Explore array manipulation techniques in MATLAB
  • Learn about event handling in MATLAB ODE solvers
USEFUL FOR

This discussion is beneficial for MATLAB programmers, particularly those working with numerical simulations and ordinary differential equations, as well as educators teaching MATLAB concepts.

Srodrigu
Messages
1
Reaction score
0
Im Having trouble writing an if statement with the time variable of the ode45 function.
I don't know if i need to create a new variable, which is not what i want, but just to create and if statement with the t variable from ODEs.

Hope someone knows how to solve this problem

Thank you
 
Physics news on Phys.org
I'm not sure what your code looks like but going from the example from matlab:
[T,Y] = ode45(odefun,tspan,y0)

Then you want an if statement based on T? Then you probably need to have the if statement inside of a for loop to go through each value of T, like:

for i = 1:length(T)
if T(i) == value
statement
end
end
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K