简体   繁体   English

在MATLAB中求解4个耦合的微分方程

[英]Solve 4 coupled differential equations in MATLAB

I have a set of coupled ODE's which I wish to solve with MATLAB. 我有一组耦合的ODE,希望通过MATLAB解决。 The equations are given below. 公式如下。

耦合微分方程

I have 4 boundary conditions: x(0), y(0), v(0), theta(0). 我有4个边界条件:x(0),y(0),v(0),theta(0)。 If I try to solve this with dsolve I get the warning that an explicit solution could not be found. 如果尝试使用dsolve解决此问题, dsolve收到警告,提示找不到明确的解决方案。 Here's the code that I used. 这是我使用的代码。

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)

This looks a little like a pendulum of some sort... 这看起来有点像某种钟摆...

Your equation has the term 您的方程式中有一项

dθ(t)/dt = C·cos(θ(t)) 

which is similar to the ODE of a pendulum, at least, it has the same problem: a closed-form solution for this equation is not known. 至少与摆的ODE相似,它具有相同的问题:该方程的闭式解是未知的。 I believe it's even proven to not exist, but I'm not 100% sure. 我相信它甚至不存在,但我不确定100%。

Anyway, numerically it's a piece of cake. 无论如何,从数字上来说这简直是小菜一碟。 Here's an example of how to do it with ode45 : 这是使用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

Note that unlike an exact solution, you'll have to specify the start and end times (captured in variable tspan ). 请注意,与确切的解决方案不同,您必须指定开始时间和结束时间(捕获在tspan )。 Note also that I've used the identity cos²θ + sin²θ = 1 to simplify D . 还要注意,我使用恒等式cos²θ + sin²θ = 1来简化D

Example output: 输出示例:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM