[英]Solve 4 coupled differential equations in MATLAB
我有一組耦合的ODE,希望通過MATLAB解決。 公式如下。
我有4個邊界條件:x(0),y(0),v(0),theta(0)。 如果嘗試使用dsolve
解決此問題, dsolve
收到警告,提示找不到明確的解決方案。 這是我使用的代碼。
syms x(t) y(t) v(t) theta(t)
% params
m = 80; %kg
g = 9.81; %m/s^2
c = 0.72; %
s = 0.5; %m^2
theta0 = pi/8; %rad
y0 = 0; %m
rho = 0.94; %kg/m^3
% component velocities
xd = diff(x,t) == v*cos(theta);
yd = diff(y,t) == v*sin(theta);
% Drag component
D = c*rho*s/2*(xd^2+yd^2);
% Acceleration
vd = diff(v,t) == -D/m-g*sin(theta);
% Angular velocity
thetad = diff(theta,t) == -g/v*cos(theta);
cond = [v(0) == 10,y(0) == 0, x(0) == 0, theta(0) == theta0];
dsolve([xd yd vd thetad],cond)
這看起來有點像某種鍾擺...
您的方程式中有一項
dθ(t)/dt = C·cos(θ(t))
至少與擺的ODE相似,它具有相同的問題:該方程的閉式解是未知的。 我相信它甚至不存在,但我不確定100%。
無論如何,從數字上來說這簡直是小菜一碟。 這是使用ode45
:
function my_ode()
% parameters
m = 80; % kg
g = 9.81; % m/s²
c = 0.72; % -
s = 0.5; % m²
rho = 0.94; % kg/m³
theta0 = pi/8; % rad
v0 = 10; % m/s
x0 = 0; % m
y0 = 0; % m
tspan = [0 10]; % s
% function to compute derivative of
% Z = [x, y, th, v]
function dZdt = odefcn(~,Z)
% rename for clarity (NOTE: x and y are not used)
th = Z(3); cth = cos(th);
v = Z(4); sth = sin(th);
% Compute derivatives
dxdt = v * cth;
dydt = v * sth;
dthdt = -g/v * cth;
dvdt = -c*rho*s*v^2/(2*m) - g*sth;
% assign to ouptut respecting either row or columnvector inputs
dZdt = Z;
dZdt(:) = [dxdt dydt dthdt dvdt];
end
% Integrate the ODE
Z0 = [x0 y0 theta0 v0];
[t,Z] = ode45(@odefcn, tspan, Z0);
% Example outputs
x = Z(:,1); th = Z(:,3);
y = Z(:,2); v = Z(:,4);
F = figure; hold on
P = get(F, 'position');
set(F, 'position', [P(1:2) 3*P(3) P(4)]);
subplot(1,3,1)
plot(x,y, 'b'), grid on
xlabel('x [m]'), ylabel('y [m]')
subplot(1,3,2)
plot(t,v, 'b'), grid on
xlabel('t'), ylabel('v [m/s]')
subplot(1,3,3)
plot(t,th, 'b'), grid on
xlabel('t'), ylabel('\theta [rad]')
end
請注意,與確切的解決方案不同,您必須指定開始時間和結束時間(捕獲在tspan
)。 還要注意,我使用恆等式cos²θ + sin²θ = 1
來簡化D
輸出示例:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.