Solving diffeerntial equations numerically

I need to solve a set of ODEs from [Ts, Tend]. I want the solution to be output every dT. Within that interval dT, a vaiable step method should be used, but I need the solution every dT. I can use the codes in Matlab but this output every dT (without interpolation) is what I need.
Atique Malik

4 Comments

I need this as I am simulating a controller system where the controller algorithm runs every dT. So I need the solution every dT as it happens, not after the fact.
It sounds as if you need a fixed-step ode instead of a variable-step ode.
You can reduce the effect of interpolation by setting MaxStep in odeset() to some fraction of dT, at the expense of slowing down the solver.
The problem is probably not about solving the ODE system in the first place. A controller algorithm has been designed to run at every dT. If this algorithm is implemented in real hardware, dT would be the fixed sampled time. As with a basic PID controller, the controller algorithm needs every measurable state and output signal at each dT in order to compute the compensating control effort.
Have you tried implementing the PID controller within the system and running it with the ode45 solver? If you figure out how to do this, it should provide insight into how to run your controller algorithm in simulation.

Sign in to comment.

Answers (4)

Steven Lord
Steven Lord less than a minute ago
If you use the ode object you can call its solve method to return the solution at specified times.
If you're using a specific ODE solver like ode45 you can specify tspan as a vector with more than 2 values to return the solution at those specified times. Alternately you can call ode45 with one output argument sol then call the deval function with sol and the specified times as input to evaluate that solution at the specified times.
Or do you want to prevent the solver from taking steps that would take it outside the interval it's in, for example you want to solve at times t = 0, t = 1, t = 2, ... and you don't want the solver to be able to step from t = 0.5 to t = 1.5 without also stepping at t = 1? If so call the solver multiple times, solving the ODE on one interval then using the solution at that endpoint as the initial condition for solving on the next interval.
It is not currently possible to solve odes numerically with variable step size, but at the same time get the output at fixed intervals without any interpolation .
If you use ode objects then the solve() method interpolates solutions.
If you use solvers such as ode45() passing in a vector of times, then when it detects that it has crossed one of the times, it does interpolation to output the result at that time.
If you want solutions at multiples of dt without interpolation, the only way I can think of is to call the ode integrator in a loop with the multiples of dt as end times of integration. But even here, I'm not sure if the integrator does not internally integrate past "tend" and interpolates the solution to "tend". Why do you think it's necessary for your application to run without interpolation of the solution at the output times ?
dt = 0.1;
nsteps = 10;
tstart = 0;
tend = dt;
y0 = 1;
fun = @(t,y) y;
sol_t = zeros(nsteps+1,1);
sol_y = zeros(nsteps+1,1);
sol_t(1) = tstart;
sol_y(1) = y0;
for i = 1:nsteps
tspan = [tstart tend];
[T,Y] = ode45(fun,tspan,y0);
tstart = tend;
tend = tend + dt;
y0 = Y(end,1);
sol_t(i+1) = T(end);
sol_y(i+1) = Y(end,1);
end
plot(sol_t,sol_y)
I need this as I am simulating a controller system where the controller algorithm runs every dT. So I need the solution every dT as it happens, not after the fact.

Tags

Asked:

about 16 hours ago

Commented:

about 2 hours ago

Community Treasure Hunt

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

Start Hunting!