Thread: ode45 MATLAB

1. ode45 MATLAB

Hi everyone,

Im trying to solve a system of 4 1st order differential equations using ode45, ode23s and using euler method,

Starting off with ode45, I have a function called solvede and im trying to use that with my main script file to model the behaviour of xdot over 10 seconds - but I still havent been able to run the model correctly

If you could spot why the code won't work Id appreciate any input
> my code is:

The function>>

function xdot = solvede(x, t)

a = 1.33;
b = 0.5*cos(x(1) - x(3));
c = 0.5*sin(x(1) - x(3))*x(3)^2 + 73.575*sin(x(1));
d = 0.5*cos(x(1) - x(3));
e = 0.333;
f = 24.525*sin(x(3));

xdot(1) = x(2);
xdot(2) = ((b*f - c*e)/(a*e - b*d));
xdot(3) = x(4);
xdot(4) = ((f*a - c*d)/(b*d - e*a));

return

The main scipt file that calls the function:

%Setting up time range
tspan(1) = 0;
tspan(2) = 10;

%Setting up initial conditions
x = [pi 0 pi 0];

%Calling ODE solver
[xdot,t] = ode45(@solvede,tspan,x);

2. Try changing the first line of the derivative definition from:

function xdot = solvede(x, t)

to

function xdot = solvede(t, x)

RonL

3. ode45 MATLAB

Thanks CaptainBlack, I tried that and it worked, Ive developed my programme far enough so that it delivers the two graphs desired for x(:,1) and x(:,3) against time using the ODE45 method;

Does anyone have any ideas how to form a solution of the system of equations with the same output but using the Euler method instead the euler method had the step size 'h' as a variable so it's a bit more complicated

Below is the code I used for solving it with ode45
>>Coding that calls function pendulumODE to solve equations

%This computer routine will solve the differential non linear system of
%equations and plot the angles phi1 and phi2 against time.

%Calling the ODE solver function pendulumODE
[t,x] = ode45(@pendulumODE,[0 10],[pi; 0; -pi; 0]);

%Plot the curves for X1 and X3 which represent phi1 and phi2 respectively
plot(t,(pi/180)*x(:,1), t,(pi/180)*x(:,3));

%Labelling the x and y axis and features of the graph
title('Solutions for phi1 and phi2 for initial conditions x(0) = [pi 0 -pi 0]')
xlabel(
'Time, seconds');
ylabel(
'Angles in radians')
legend(
'phi1', 'phi2')
grid
on