0

I am trying to solve a differential equation with the ode45 function that involves two degrees of freedom, the x and y directions. When in my oem function below I am trying to store all four variables that I need into a matrix to plug into the ode45, but I cannot get the oem function to recognize ds(3) or ds(4). I've tried working through it but I've hit a wall.

function odeprob2 
%ode45 function [T, S] = ode45(@oem, linspace(0,0.1,100), [0,2]);
fprintf('Final Position in the X Direction = %4.2f \n', S(end,1));
fprintf('Final Velocity in the X Direction= %4.2f \n', S(end,2));
fprintf('Final Position in the Y Direction = %4.2f \n', S(end,3));
fprintf('Final Velocity in the Y Direction= %4.2f \n', S(end,4));
plot(T, S(:,1), '-k')
xlabel('Time, sec')
ylabel('Position, m')

function ds = oem(t, s)
  %Constants: 
  rad = 15; %mm
  dens = 8000; %kg/m^3
  deno = 980; %kg/m^3
  g = 9.81 ; %m/s^2
ds = zeros(4, 1);
ds(1) = s(2);
ds(2) = ((-1.6) * s(2)) / (((4/3)*pi*(rad^3))*dens);
ds(3) = s(4);
ds(4) = ((((4/3) * pi * (rad^3)) * (dens - deno) * g) - (1.6 * s(4))) / (((4/3) *pi*(rad^3))*dens);
  • it will be much simpler to state the actual ode equations. – CroCo Jan 26 '16 at 19:48
  • 2
    It looks like your state dimension is four, but the initial condition you're passing `ode45` is only length two: `[0,2]`... you'll need to specifiy initial positions and velocities, e.g. `[0,0,2,0]` – Geoff Jan 26 '16 at 19:56

0 Answers0