using ODE45 to solve simultaneously independent ODE's vs. calling the function twice - different results...
이전 댓글 표시
Hi,
i am trying to solve for the displacement of a spring & mass with rotating eccentric mass in X and Y directions.
If i solve it using the following code:
[t,y] = ode45(@odefun, tspan, ic);
function dydt = odefun(t,y)
%SDOF (Uncoupled) Forced (Unbalance), Undamped
dydt = zeros(4,1);
dydt(1) = y(2);
dydt(2) = -(k/M)*y(1)+(m/M)*e*w^2*cos(w*t);
dydt(3) = y(4);
dydt(4) = -(k/M)*y(3)+(m/M)*e*w^2*sin(w*t);
end
I get one result (which seems incorrect for the second equation).
If i solve it using the following code (which effectively separates it into two independent ODEs:
[t,y] = ode45(@odefun, tspan, ic);
function dydt = odefun(t,y)
%SDOF (Uncoupled) Forced (Unbalance), Undamped
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = -(k/M)*y(1)+(m/M)*e*w^2*cos(w*t);
end
[t2,y2] = ode45(@odefun2, tspan, ic2);
function dydt2 = odefun2(t,y2)
dydt2 = zeros(2,1);
dydt2(1) = y2(2);
dydt2(2) = -(k/M)*y2(1)+(m/M)*e*w^2*sin(w*t);
end
I get a different result, which seems more in line with what i expect.
Why would i get different results for these two methods?
See attached images..
Any help would be greatly appreciated!
댓글 수: 2
James Tursa
2018년 6월 1일
편집: James Tursa
2018년 6월 1일
What initial conditions are you using? When I run the code with the ic from your other post and M=1 I get plots that are very nearly right on top of each other (with the exception that the output sample times are different).
Tyler
2018년 6월 1일
채택된 답변
추가 답변 (1개)
Abraham Boayue
2018년 6월 2일
편집: Abraham Boayue
2018년 6월 2일
Answers have been inserted into your codes (I just modified them a bit).
function Plot_odes
T = 10;
N = 1000;
t = 0 :T/(N-1):T;
ic = [0 0];
[t,x]= ode45(@odefun, t,ic );
[t,y]= ode45(@odefun2, t,ic );
figure
plot(t ,x(:,1),'linewidth',2,'color','r')
hold on
plot(t ,y(:,1),'linewidth',2,'color','b')
legend('x','y');
a = title('x(t) y(t)');
set(a,'fontsize',14);
a = ylabel('y');
set(a,'Fontsize',14);
a = xlabel('t [0 T]');
set(a,'Fontsize',14);
xlim([0 T])
grid
% Your two ODEs are completely independent of each other and
% therefore, the solution of one does not interfere with the other. In other
% words, they are not simultineous equations; you actually tricked ode45
% into believing that it was solving simultaneous equations in the first
% case. However, the two functions (odefun and odefun2) below are valid solutions of
% the odes because the vectors dxdt = [x' x''], dydt = [y' y''] and their
% solution vectors, X = [ x1 x2] and Y = [y1 y2] (where x1 = x(t) as the
% required solution for the displacement in the x direction and x2 its
% derivative), are uniform vectors with each element dependent on the other. On the otherhand, in your first method,
% you had dydt = [x' x'' y' y''] and Y = [x1 x2 y1 y2] which can not be right
% because x and y are independent variables.
function dxdt = odefun(t,y)
%SDOF (Uncoupled) Forced (Unbalance), Undamped
k = 104000;
M = 200;
m = 1/16;
w = 1;
e = 1;
dxdt_1 = y(2);
dxdt_2 = -(k/M)*y(1)+(m/M)*e*w^2*cos(w*t);
dxdt =[dxdt_1; dxdt_2];
function dydt = odefun2(t,y2)
k = 104000;
M = 200;
m = 1/16;
w = 1;
e = 1;
dydt_1 = y2(2);
dydt_2 = -(k/M)*y2(1)+(m/M)*e*w^2*sin(w*t);
dydt =[dydt_1; dydt_2];
Here is your first method.
function Plot_odes
T = 10;
N = 1000;
t = 0 :T/(N-1):T;
ic = [0 0 0 0]; % Initial conditions
[t,z]= ode45(@odefun, t,ic );
figure
plot(t ,z(:,1),'linewidth',2,'color','r')
hold on
plot(t ,z(:,3),'linewidth',2,'color','b')
legend('x','y');
a = title('x(t) y(t)');
set(a,'fontsize',14);
a = ylabel('y');
set(a,'Fontsize',14);
a = xlabel('t [0 7]');
set(a,'Fontsize',14);
xlim([0 T])
grid
% Even though you were able to get some results, however,
% they are misleading because ode45 sees
% dydt = [y' y'' y''' y''''] and gives Y = [y1 y2 y3 y4].
% See above for further explanations
function dydt = odefun(t,y)
%SDOF (Uncoupled) Forced (Unbalance), Undamped
k = 104000;
M = 200;
m = 1/16;
w = 1;
e = 1;
dydt_1 = y(2);
dydt_2 = -(k/M)*y(1)+(m/M)*e*w^2*cos(w*t);
dydt_3 = y(4);
dydt_4 = -(k/M)*y(3)+(m/M)*e*w^2*sin(w*t);
dydt =[dydt_1; dydt_2;dydt_3; dydt_4];
댓글 수: 3
A Runge-Kutta integrator like ODE45 does not draw the conclusion that the 3rd component is the derivative of the 2nd one. The relation between the components is defined in the equation to be integrated only. It is valid to integrate independent components, as long as the stepsize control gets sufficient parameters for the tolerances. The only drawback is that the stepsize controller cannot run the integration with the optimal step width for the simultaneous solution in opposite to the separated functions.
Abraham Boayue
2018년 6월 2일
Great job Jan! You have just thought me a valuable lesson.
Chenko DUSHIMIMANA
2020년 11월 4일
Hello, Thanks the detail explanation about ode45. What if the differential equations to be solved by ode45 are interconnected(i.e. they depend on each other, that is, to find the solution of one system of equations(say, system 1) requires the knowledge of the solution from the other system of equations(say, system 2)). Where, the "tspan" for both system may differ or be similar. How Can we solve these two systems? simultaneously or separately?
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

