Is there a way to control the length of t in the output of ode45?

25 views (last 30 days)
Hello,
I am trying to implement a numerical integration using ode45.
I have the following function that I want to integrate:
Where and are vectors of size (1x199) that change depending on the time.
Also, is a vector of size (1x199) that is constant and each of its elements contain the value 9.81 .
So, I created the ode45 problem as follows:
1 - In the main script, I have the following:
%% integrate ydd to obtain yd
gt = linspace(0,1.99,199);
g = -tan(phi).*(zdd + gravity);
tspan = [0 1.99]; % time span
ic = 0; % initial condition
opts = odeset('RelTol',1e-2,'AbsTol',1e-4); % ode options
[T_yd,yd] = ode45(@(T_yd,yd) myode_ydd(T_yd,gt,g), tspan, ic, opts);
2 - Then, the function myode_ydd has the following form:
function dydt = myode(t,gt,g)
g = interp1(gt,g,t); % Interpolate the data set (gt,g) at time t
dydt = g; % Evaluate ODE at time t
When I run the code, the length of the outputs T_yd and yd is 41.
Is there a way to keep the outputs of length 199?
Thank you for taking the time to read my question

Accepted Answer

Cris LaPierre
Cris LaPierre on 2 Jun 2021
Edited: Cris LaPierre on 2 Jun 2021
Yes. Instead of using a time span, use a time vector. The equation will still be solved the same way (variable time step), but the results will be interpolated to the timepoints specified in your vector. Your output would then have the same number of points as your time vector, which you created.
From the documentation:
  • If tspan has two elements, [t0 tf], then the solver returns the solution evaluated at each internal integration step within the interval.
  • If tspan has more than two elements [t0,t1,t2,...,tf], then the solver returns the solution evaluated at the given points. However, the solver does not step precisely to each point specified in tspan. Instead, the solver uses its own internal steps to compute the solution, then evaluates the solution at the requested points in tspan. The solutions produced at the specified points are of the same order of accuracy as the solutions computed at each internal step.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!