Reputation: 308
I'm not that familiar with matlab. I solve a first differential equation of the form dS(t)/dt = F(S(t)) (S(0) given) using ode45. But then, I have a second differential equation to solve which is dX(t)/dt = G(X(t),S(t)) (X(0) given). How can I use the results on S to solve the equation on X ?
I want the values S(1) and G(1) of the solutions S (and G). The first idea I had was quite "naive". I first create a function which gives me the value S(t) for t in [0,1] :
function dS=equation1(t,S)
dS=F(S);
end
function S=solve1(S0,t)
if t==0
S=S0;
else
[~,V]=ode45(@equation1,[0 t],S0);
S=V(end,:)
end
And then I create a second function to solve the second equation :
function dX=equation2(t,X)
dX=G(X,solve1(t));
end
function G=solve2(X0,t)
[~,V]=ode45(@equation2,[0 t],X0);
end
and in the end, G(1)=solve2(X0,1) and S(1)=solve1(S0,1). But I feel like there is a much better way to do it ! Thanks for your help !
Upvotes: 1
Views: 210
Reputation: 5359
This is basically expanding a differential equation to include more parameters, which can be done rather simply. So if S:
ds[1] = s[1] - s[2];
ds[2] = 3*s[1] + 0.5*s[2];
Let's now say X is 2-nd order differential equation. An expanded X will also contain S (hereby denoted as x[3] and x[4]):
dx[1] = a1*x[1] + b1*x[2] + c1*x[3] + d1*x[4]
dx[2] = a2*x[1] + b2*x[2] + c2*x[3] + d2*x[4]
dx[3] = x[3] - x[4];
dx[4] = 3*x[3] + 0.5*x[4];
Upvotes: 0