MATLAB: Solving system of ODE using ode45 in matlab

ode45

an example of using 2 to 4 ode's

Best Answer

  • clc
    clear
    n = 30;
    t = [0 0.52 1.04 1.75 2.37 3.25 3.83]';
    x = [153 185 208 249 261 271 273]';
    tt = linspace(t(1),t(end),n);
    xx = spline(t,x,tt);
    v = calc_derv(tt,xx);
    a = calc_derv(tt,v);
    subplot(2,1,1),plot(tt,v)
    title('Jet Fighters Velocity')
    xlabel('Time (s)')
    ylabel('Velocity (m/s)')
    subplot(2,1,2),plot(tt,a)
    title('Jet Fighters Acceleration')
    xlabel('Time (s)')
    ylabel('Acceleration (m/s^2)')
    % calculate 1st order derivative using finite differences
    function dydx = calc_derv(x,y);
    n = length(x);
    h = x(2)-x(1);
    dydx(1) = (-y(3) + 4*y(2) - 3*y(1))/(2*h);
    dydx(n) = (3*y(n) - 4*y(n-1) + y(n-2))/(2*h);
    for i = 2:n-1
    dydx(i) = (y(i+1) - y(i-1))/(2*h);
    end