# Math Help - MATLAB ode 45 help

1. ## MATLAB ode 45 help

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?

2. Originally Posted by Troktr
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?
You have a state vector:

$\bold{x}=\left[\begin{array}{c}x\\y\\x'\\y'\end{array} \right]$

then:

$\bold{x}'=\left[\begin{array}{c}x'\\y'\\x''\\y''\end{array} \right]=$ $
\left[\begin{array}{c}\bold{x}_3 \\ \bold{x}_4 \\ \bold{x}_3\sqrt{\bold{x}_3^2+\bold{x}_4^2}\\ \bold{x}_4\sqrt{\bold{x}_3^2+\bold{x}_4^2}\end{arr ay} \right]$

Now you write a derivative function to calculate this last with arguments as required by ode45.

CB

3. 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.

4. Originally Posted by Troktr
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.
[t xy] = ode45('trajxy',0,5,[0 134 0 0])

should be types at the command prompt, maybe it is but here it looks like its in the derivative file.

CB

5. 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.

6. Originally Posted by Troktr
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.
Try making the initial state a column vector (I don't think this matters but we might as well be consistent).

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])

CB