MATLAB: 2 coupled second order equations

2 coupled second order equationsMATLAB

Hello everyone, I have a problem of 2 coupled second order equations of the form that is described in the photo I attached. By the way – W(t) and T(t) are quite simple functions, and I plan calling another external function when using it. The reason I don't include them above is the fact that they have a "steps" form and hence time-interval dependent. As I see the only option is a numeric solution by using the ode45 function. I want to make sure that using the above function is possible at all by doing the manipulation that is also described in the attached photo. I will be happy to hear what is your advice – is there another good way to solve this problem? If not, is the methodic way described above should work? How should I write the command when using the ode45 function? [t,q]=ode45(@(t,q) F(t,q),t_span,q0) While F is a 2×1 vector that consists each of the above eauations.

Best Answer

Bellow is the code with 2 methods.
Observation: piecewise RUNGE-KUTTA method is more accurate whenever the trajectory is orbital around the attractor (0,0)
% S(t) := T(t) / W(t),
% S(t) = s(i) for tbin(i) <= t < tbin(i+1);
%

n = 10; % number of interval where S is constant
tbin = [0 cumsum(0.1+rand(1,n))];
s = rand(1,n);
s(end+1) = s(end);
q0 = rand(4,1);
% Cauchy condition
tspan = tbin([1 end]);
[t,q] = ode15s(@(t,q) odefun(t,q,tbin,s), tspan, q0);
x = q(:,1);
y = q(:,3);
plotsol(1,t,x,y,tbin,'single run ode15s');
%
t = [];
q = [];
for k=1:n
subts = tbin([k k+1]);
[tk,qk] = ode45(@(t,q) odefun(t,q,tbin,s), subts, q0);
t = [t; tk];
q = [q; qk];
q0 = q(end,:);
end
x = q(:,1);
y = q(:,3);
plotsol(2,t,x,y,tbin,'piecewise ode45');
function plotsol(fignum,t,x,y,tbin,str)
% plot the solution
fig = figure(fignum);
clf(fig);
% make figure wider
p = get(fig,'position');
p(3) = 800;
set(fig,'position',p);
subplot(1,2,1),
h = plot(t,[x,y]);
xlabel('t');
hold on
n = length(tbin);
for k=2:n
plot(tbin(k)+[0 0],ylim(gca),'-.k');
end
legend(h,'x','y');
title(str);
subplot(1,2,2),
plot(x,y);
xlabel('x');
ylabel('y');
title(str);
end
function qp = odefun(t,q,tbin,s)
x = q(1);
xp = q(2);
y = q(3);
yp = q(4);
[~,i] = histc(t,tbin);
S = s(i);
a = sqrt(x.^2+y.^2).^3;
b = sqrt(xp.^2+yp.^2);
c = S./b;
xpp = -x./a + xp.*c;
ypp = -y./a + yp.*c;
qp = [xp; xpp; yp; ypp];
end
Related Question