Clear Filters
Clear Filters

Solve differential equation for a sequence of k values

38 views (last 30 days)
carl Engström
carl Engström on 22 Aug 2024 at 18:46
Answered: Steven Lord on 23 Aug 2024 at 15:29
My task is write a function called "compute_intervals" that solves the differential equation for multiple values of k each lasting for a specified time. The function should take in three inputs (see below) and return two outputs that specify solutions to the differential equation. In order to properly assess the function, the output vector of times should be separated by 0.1 and no values of time should be duplicated. I have access to a working copy of "compute_ode". The function "compute_intervals" will be assessed by running
[tsolref,csolref]=compute_intervals(k_values,t_intervals,init_cond);
for different vectors.
Inputs:
  1. a vector of k values
  2. a vector of time durations that is the same size as the first input and expresses how long each k-value should be enacted
  3. a scalar for the initial condition for C
Outputs:
  1. a vector of times
  2. a vector of C solutions
I have so far:
function [tsol,csol]=compute_intervals(ks,ts,init_cond)
for i1=1:length(ts)
%t_range=[tstart+.1:.1:tend]; % this is the kind of time range to be used with ode45---note you would have to determine what tstart and tend are each iteration
end
And i need to use the following to call my function:
k_values=[.2 -.5 .5];
t_intervals=[1 5 2];
init_cond=20;
[tsol,csol]=compute_intervals(k_values,t_intervals,init_cond);
plot(tsol,csol,'LineWidth',3)
xlabel('time','FontSize',18)
ylabel('C','FontSize',18)

Answers (2)

Naga
Naga on 23 Aug 2024 at 2:27
Hi Carl,
Sure, I can help you complete the 'compute_intervals' function. Here’s the complete function:
function [tsol, csol] = compute_intervals(ks, ts, init_cond)
%Initialize tsol and csol as empty vectors. Set tstart to 0 and C to the initial condition.
tsol = [];
csol = [];
tstart = 0;
C = init_cond;
for i = 1:length(ts)
% Calculate tend and create t_range with a step of 0.1.
tend = tstart + ts(i);
t_range = tstart:0.1:tend;
% Ensure the last time point is included
if t_range(end) < tend
t_range = [t_range, tend];
end
% Solve the differential equation for the current interval
[t, C_sol] = compute_ode(ks(i), t_range, C);
% Append the results, excluding the first time point to avoid duplicates
if isempty(tsol)
tsol = t;
csol = C_sol;
else
tsol = [tsol; t(2:end)];
csol = [csol; C_sol(2:end)];
end
% Update the initial condition for the next interval
tstart = tend;
C = C_sol(end);
end
end
Hope this Helps!

Steven Lord
Steven Lord on 23 Aug 2024 at 15:29
The ballode example included in MATLAB does something like this, though instead of dividing the interval that the ball is in motion into segments based on time it divides it based on when the event of the ball striking the ground occurs. You should be able to use the same general principle, though:
  1. Solve the ODE for the first time interval (or in the case of ballode, until the first event occurs.)
  2. While there are any more intervals to solve
  3. Use the solution for the last time in the current interval as the first pass at the initial conditions for the next time interval.
  4. Update the initial conditions and/or ODE function for use in the next interval.
  5. Solve the ODE for the new current interval.
  6. Return to step 2
In the ballode example, at step 4 the ODE function remains the same but the new initial conditions (the position and speed of the bouncing ball) are modified to reflect the physics of the collision between ball and ground.

Community Treasure Hunt

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

Start Hunting!