I have been given two second order ode's:
d^2x/dt^2 = dx/dt * sqrt( (dx/dt)^2 + (dy/dt)^2 )
d^2y/dt^2 = dy/dt * sqrt( (dx/dt)^2 + (dy/dt)^2 )
with initial conditions x(0)=0; y(0)=0; dx/dt(t=0) = 134; dy/dt(t=0) = 0
Now, my homework problem asks me to reduce the two second-order ode's to a system of first order ode's then write a script to solve the trajectory (i.e. x and y) for the first 5 seconds using the Matlab built-in function ode45.
To reduce the two 2nd order odes i set for the first ode x=x1 and dx/dt=x2 and for second ode y=y1 and dy/dt=y2 to get the system of first order ode's:
dx/dt = x2 ; x1(0) = 0
dy/dt = y2 ; y1(0) = 0
dx2/dt = x2 sqrt( (x2)^2 + (y2)^2 ) ; x2(0)= 134
dy2/dt = y2 sqrt( (x2)^2 + (y2)^2 ); y2(0)= 0
I have no idea however how to set this up in matlab using the ode45 function. Could anyone help please?
I ended up with this:
function dxy = trajxy(t, xy)
j = 5.38; m = 80; g = 9.81;
x = xy(1);
xdot = xy(2);
y = xy(3);
ydot = xy(4);
xdot = xdot;
ydot = ydot;
xdoubledot = -(j/m)*xdot*sqrt((xdot)^2+(ydot)^2);
ydoubledot = -g - (j/m)*ydot*sqrt((xdot)^2 + (ydot)^2);
dxy = [xdot; xdoubledot; ydot; ydoubledot];
[t xy] = ode45('trajxy',0,5,[0 134 0 0])
I get the error:
??? Input argument "xy" is undefined.
Error in ==> trajxy at 3
x = xy(1);
Although it still outputs a matrix. The values seem to be off though.
I just copied [t xy] = ode45('trajxy',0,5,[0 134 0 0]) in there right after it but yeah it isn't in the derivative file. After i run the derivative file i get the error. Then when i put [t xy] = ode45('trajxy',0,5,[0 134 0 0]) in the command window it still works but I dont seem to get right values.
You also might be better off using a function handle rather than the function name in quotes (again I don't think this will make any difference IIRC quoted function names still work):
[t, xy] = ode45(@trajxy,0,5,[0;134;0;0])