Clear Filters
Clear Filters

Weird bugs when solving three coupled second order differential equations using ode45

45 views (last 30 days)
Hi guys, I am new to ode45 so I am very confused about its results. I have good results when solving two coupled differential equations (say, x1 and y ), but when I add one more equation (say x2), then the results are very different and even if I set x2 equation the same as x1, which doesn't make much sense for me. For example, if eq3 is changed to eq2, and also deval(~,2), the answer for y is different.
Here are the codes I used:
clear
close all
syms x1(t) x2(t) y(t)
dx1=diff(x1,t);
dx2=diff(x2,t);
dy=diff(y,t);
omega_ir1 = 0.52*2*pi;%
omega_ir2 = 0.28*2*pi;
omega_r = 0.425*2*pi;%
gamma_ir = 0;%
gamma_r = 0;%
t0 = [-40 50];%
tNum = 2000;
L = length(t0);
eq1 = diff(x1,2) == -2*gamma_ir*omega_ir1*dx1 - omega_ir1^2*x1 + y + 2*x2*y;
eq2 = diff(x2,2) == -2*gamma_ir*omega_ir2*dx2 - omega_ir2^2*x2 + y + 2*x1*y;
eq3 = diff(y,2) == -2*gamma_r*omega_r*dy - omega_r^2*y + x1*x2;
V = odeToVectorField(eq1,eq2,eq3);
M = matlabFunction(V,'vars', {'t','Y'});
interval = t0;
ySol = ode45(M,interval,y0);
tValues = linspace(interval(1),interval(2),tNum);
sol = deval(ySol,tValues,3);
Thanks in advance!

Accepted Answer

Torsten
Torsten on 26 Jul 2024 at 12:17
Edited: Torsten on 26 Jul 2024 at 12:19
syms x1(t) x2(t) y(t)
t0 = [0 1];
% y0 must be given in the order as prescribed by S (see below), thus
% y0 = (x2(0),dx2/dt(0),x1(0),dx1/dt(0),y(0),dy/dt(0))
% The solution is obtained in the same order.
y0 = [1 2 4 7 8 4];
tNum = 100;
gamma_ir = 1;
omega_ir1 = 1;
omega_ir2 = 0.5;
gamma_r = 2;
omega_r = 3;
eq1 = diff(x1,2) == -2*gamma_ir*omega_ir1*diff(x1) - omega_ir1^2*x1 + y + 2*x2*y;
eq2 = diff(x2,2) == -2*gamma_ir*omega_ir2*diff(x2) - omega_ir2^2*x2 + y + 2*x1*y;
eq3 = diff(y,2) == -2*gamma_r*omega_r*diff(y) - omega_r^2*y + x1*x2;
[V,S] = odeToVectorField(eq1,eq2,eq3)
V = 
S = 
M = matlabFunction(V,'vars', {'t','Y'})
M = function_handle with value:
@(t,Y)[Y(2);Y(1).*(-1.0./4.0)-Y(2)+Y(5)+Y(3).*Y(5).*2.0;Y(4);-Y(3)-Y(4).*2.0+Y(5)+Y(1).*Y(5).*2.0;Y(6);Y(5).*-9.0-Y(6).*1.2e+1+Y(1).*Y(3)]
interval = t0;
ySol = ode45(M,interval,y0);
tValues = linspace(interval(1),interval(2),tNum);
sol = deval(ySol,tValues,3);
plot(tValues,sol)
grid on
  3 Comments
Torsten
Torsten about 24 hours ago
Edited: Torsten about 24 hours ago
Yes, the fifth row is y. The third row is x1. As said, you must be careful when supplying the initial values and evaluating the solution.
I'm not sure whether you can somehow prescribe the ordering, but I doubt there is a direct way to do so. Of course, you could permute the rows of V according to your personal S after the odeToVectorField command.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!