# fixed time step for ODE45

20 views (last 30 days)
aakash d on 24 Oct 2020
Commented: aakash d on 27 Oct 2020
Hi,
aakash here.
I have written the code below to solve the second order ODE using ODE45. There is an event condition in my code. Because of presence of this event condition, I am not (i dont know) able to use fixed time step in ODE45. Can somebody please help me with this. I request all to please help me to add the fixed time step in this code.
Waiting for the responses.
Thank you
~aakash
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear all
close all
global k Th0 e m c t
%% Input parameters (Length (L), co-efficient of restitution (e), acceleration due to gravity (g))
e=0.9; m=1; c=0; k=246.46;
%% Initial condition
Thid = 8*40/(5*pi)^3; % position from where the mass is released
Thiv = 0; % inital velocity (at t=0)
Th0 = 0.05; % Position where the wall is there
tfi = 5; % time final time
%% ode45 (function call)
eta0=[Thid Thiv]; % Initial conditions
tspan=[0 tfi];
options = odeset('Events',@aakash_function);
t_vec = [];
eta_vec = [];
tcon=0;
ii=1;
while tcon<tfi %% to check whether final time is reached or not
tspan=[tcon tfi];
[t,eta] = ode45(@aakash_mass,tspan,eta0,options);
eta0=[eta(end,1) -(e*eta(end,2))]; %% modified inital conditions after impact
tcon=t(end,1);
t_vec = [t_vec;t];
eta_vec = [eta_vec;eta];
end
t_vec = [t_vec;t];
eta_vec = [eta_vec;eta];
%%
syms u5 x
u5 = sin(5*pi*x)*eta_vec(:,1)
%% Plot
% close all
for i = 1:length(u5)
fig = ezplot(u5(i))
set(fig,'Linewidth',1.5)
xlim([0 1])
ylim([-0.25 0.25])
grid on
title('only third mode')
pause(0.0000000000001)
end

aakash d on 24 Oct 2020
Hi,
aakash here.
I have written the above code to solve the second order ODE using ODE45. There is an event condition in my code. because of presence of this event condition, I am not (i dont know) able to use fixed timr step in ODE45. Can somebody please help me with this.
Waiting for the responses.
Thank you
~aakash
Ameer Hamza on 24 Oct 2020
ode45() does not use a fixed time-step for solving ODE. It uses an adaptive algorithm to adjust the step-size. Why do you want to use a fixed time-step?
aakash d on 24 Oct 2020
Thanks Ameer for response. I have to check the output at fixed time intervals (say 0.01seconds). Therefore, I want to run code at fixed time steps.
Is there any way to get output at known time intervals. Example: Time duration to run the code is 5 seconds So t_span is from 0 to 5. I want to get response/output at each step of 0.01 second. i.e., 0,. 0.01,0.02,0.03,....5. Please help me if there is any way to get this..

Ameer Hamza on 24 Oct 2020
If you are only concerned about output at fixed time-step, then you can pass tspan as a vector of time-values
tspan= tcon:0.01:tfi;
[t,eta] = ode45(@aakash_mass,tspan,eta0,options);

#### 1 Comment

aakash d on 27 Oct 2020

Thankyou so much Ameer Hamza. That really helped. Your "tspan" related comment worked. Thanks

Walter Roberson on 24 Oct 2020
has code for fixed step ode such as ode4.
ode45 is inherently variable time step and cannot be used in fixed step.
Event functions are triggered whenever they need to be according to the condition you program in. The ode45 solver will automatically take steps on both sides of the event to find the zero of the event function so as to give as accurate a crossing time as feasible.
If you were to use an event function with a fixed step solver then it would be unlikely that the event would happen to occur exactly at a multiple of the step size. If you were modelling a ball bouncing between two plates, should the fixed step solver fire the event before the ball reaches the plate or after it has passed through it?

aakash d on 24 Oct 2020
Thanks Walter Roberson. May I request you something? Could you please help me if there is any way to get the response/output at fixed time intervals. Not necessarily using ODE45. But by using any other ODE solver. I want to get solution /output at those fixed intervals (say0.01seconds) that I will define.
Please help me if there is anything that you can do or if you can please give me the modified version of this code.
Walter Roberson on 24 Oct 2020
No. Your requirements for using the event functions are incompatible with using a fixed time step. You can do what Ameer suggested to pass in a vector of time values, but if the event function fires and is terminal then the last entry will be at the event time rather than at the fixed time.
aakash d on 27 Oct 2020

Okay thanks for response.