Having problem to change the value of extra parameters at each time step passed in ODE45 Solver

5 views (last 30 days)
Hello, I have a set of two diffrential equations having some constants i.e. A, B, C and D and i have passed these extra parametres successfully in ODE45 solver too. I'm getting my results as well.
As i'm solving these equations for specific time i.e. at t= 0, 1, 2,.......10.
Now what I want to do is that I want to change the values of A, B, C and D at each time i.e at t=0 I have some values of A,B,C and D and then at t=1 I need to use different values of these constants and so on.
Here's code and a picture of my two differential equations.
clc
clear all
close all
A=1;
B=-4;
C=-2;
D=3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% d1 w1 d2 w2
[t,y] = ode45(@(t,y) aa(t,y,A,B,C,D),[0:1:10], [5 -3]);
plot(t,y(:,1),t,y(:,2))
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
function dydt1 = aa(t,y,A,B,C,D)
dydt1 = zeros(2,1);
dydt1(1) = A.*y(1)+B.*y(2).*(2.*y(1));
dydt1(2) = C.*y(1)+D.*(2.*y(1));
end

Accepted Answer

Thiago Henrique Gomes Lobato
You can do it accurately only by doing partial integrations (make a loop in ode45 for the time steps) and changing the variable either between those partial steps or with an time dependence in your function. Take a look at this answer (and commentary) to see an example of it that works https://de.mathworks.com/matlabcentral/answers/487643-adding-a-piecewise-time-dependent-term-in-system-of-differential-equation#answer_398394?s_tid=prof_contriblnk

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!