ODE45 how can I format this system of equations?
조회 수: 1 (최근 30일)
이전 댓글 표시
How can these four equations be arranged for ODE45 solution?
xd1 = v1;
xd2 = v2;
vd1 = (1/m1) * (P - R1*(xd1 - xd2) - K1*(x1-x2));
vd2 = (1/m2) * (-R2*xd2 - K2*x2 + R1*(xd1-xd2) + K1*(x1-x2));
I tried following the documentation, but couldn't apply it.
댓글 수: 0
답변 (2개)
Adriano Filippo Inno
2019년 6월 17일
편집: Adriano Filippo Inno
2019년 6월 17일
Assuming your parameters are constants, create the dynamics function as follows:
function dY = DinFun(~,Y,parameters)
% constants
P = parameters.P;
m1 = parameters.m1;
m2 = parameters.m2;
R1 = parameters.R1;
R2 = parameters.R2;
K1 = parameters.K1;
K2 = parameters.K2;
% states
x1 = Y(1);
x2 = Y(2);
v1 = Y(3);
v2 = Y(4);
% state derivatives
dY(1) = v1;
dY(2) = v2;
dY(3) = (1/m1) * (P - R1*(v1 - v2) - K1*(x1-x2));
dY(4) = (1/m2) * (-R2*v2 - K2*x2 + R1*(v1-v2) + K1*(x1-x2));
dY = dY';
end
than you need a script like the following:
clc; close all; clear
%% defining the parameters involved
parameters.P = 1;
parameters.m1 = 10;
parameters.m2 = 1;
parameters.R1 = 0.1;
parameters.R2 = 0.3;
parameters.K1 = 100;
parameters.K2 = 80;
%% Initial state
% Assuming you want to start from 0,0 with null velocities
x1_0 = 0;
x2_0 = 0;
v1_0 = 0;
v2_0 = 0;
Y_0 = [x1_0; x2_0; v1_0; v2_0];
%% ode settings
% assuming your time starts from 0
t0 = 0;
tf = 10;
%% ODE
[T,Y] = ode45(@DinFun, [t0 tf], Y_0, [], parameters);
%% What ever else you need
You just need to change the value of the constants and the final time
댓글 수: 0
Star Strider
2019년 6월 17일
Try this:
function vd = YourODE(t,v,K1,K2,m1,m2,P,R1,R2,x1,x2)
xd1 = v(1);
xd2 = v(2);
vd(1,:) = (1/m1) * (P - R1*(xd1 - xd2) - K1*(x1-x2));
vd(2,:) = (1/m2) * (-R2*xd2 - K2*x2 + R1*(xd1-xd2) + K1*(x1-x2));
end
I created some values for the constants, then tested it with:
[T,V] = ode45(@(t,v)YourODE(t,v,K1,K2,m1,m2,P,R1,R2,x1,x2), tspan, v0);
where ‘v0’ is a (2x1) vector of initial conditions.
That should get you started
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!