ode45 in matlab

The ode45 function in MATLAB is used to solve ordinary differential equations (ODEs) numerically. It uses the Runge-Kutta 4th/5th order method to approximate the solution to the ODE.

Here is the basic syntax for using ode45:

main.m
[t,y] = ode45(odefun,tspan,y0)
31 chars
2 lines

where odefun is a function handle for the ODE to be solved, tspan is a vector of the form [t0 tf] where t0 is the initial time and tf is the final time, and y0 is a vector of the initial conditions for the ODE. The output t is a vector of the time steps used in the solution, and y is a matrix where each row is the solution of the ODE at the corresponding time step in t.

Here is a simple example using ode45 to solve the ODE y' = -y:

main.m
odefun = @(t,y) -y;  % define the function handle for the ODE
tspan = [0 10];      % define the time interval to solve the ODE
y0 = 1;              % define the initial condition for y
[t,y] = ode45(odefun, tspan, y0);  % solve the ODE using ode45
plot(t,y)            % plot the solution
xlabel('t')
ylabel('y')
title('Solution to y'' = -y')
343 chars
9 lines

This should produce a plot of the solution to the ODE, which decays exponentially to zero as t increases.

gistlibby LogSnag