Solve differential equation for a sequence of k values

1 回表示 (過去 30 日間)
carl Engström
carl Engström 2024 年 8 月 22 日
回答済み: Steven Lord 2024 年 8 月 23 日
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)

回答 (2 件)

Naga
Naga 2024 年 8 月 23 日
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 2024 年 8 月 23 日
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.

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by