MATLAB has a built-in function called ode45
that can be used to solve first order differential equations.
The syntax for using ode45
is as follows:
29 chars2 lines
Here, odefun
is a function handle to the function that describes the differential equation, tspan
is a vector of time points at which the solution will be computed, and y0
is the initial condition of the equation.
For example, suppose we want to solve the following first order differential equation:
main.m22 chars2 lines
We can define the function that describes the differential equation as follows:
46 chars4 lines
Then, we can use ode45
to solve the equation as follows:
132 chars4 lines
The variable t
will contain the time points at which the solution was computed, and the variable y
will contain the corresponding values of y
.
We can plot the results using the following code:
34 chars4 lines
This will produce a plot of the solution to the differential equation.
gistlibby LogSnag