Solve differential equation for a sequence of k values
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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:
- a vector of k values
- a vector of time durations that is the same size as the first input and expresses how long each k-value should be enacted
- a scalar for the initial condition for C
Outputs:
- a vector of times
- 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)
0 Kommentare
Antworten (2)
Naga
am 23 Aug. 2024
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!
0 Kommentare
Steven Lord
am 23 Aug. 2024
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:
- Solve the ODE for the first time interval (or in the case of ballode, until the first event occurs.)
- While there are any more intervals to solve
- Use the solution for the last time in the current interval as the first pass at the initial conditions for the next time interval.
- Update the initial conditions and/or ODE function for use in the next interval.
- Solve the ODE for the new current interval.
- 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!