简体   繁体   中英

Solve 4 coupled differential equations in MATLAB

I have a set of coupled ODE's which I wish to solve with MATLAB. The equations are given below.

耦合微分方程

I have 4 boundary conditions: 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. 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. I believe it's even proven to not exist, but I'm not 100% sure.

Anyway, numerically it's a piece of cake. Here's an example of how to do it with 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 ). Note also that I've used the identity cos²θ + sin²θ = 1 to simplify D .

Example output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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