To calculate a second order differential equation in matlab, you first need to convert it into a system of first order differential equations. You can define the second derivative as a new variable and then write two first order differential equations that link it with the original function and its first derivative.
Assuming you have a second order differential equation of the form:
main.m39 chars2 lines
where p(t)
, q(t)
and f(t)
are functions of t
, you can define two new variables z(t) = y'(t)
and w(t) = y(t)
, and write the following system of first order differential equations:
main.m60 chars3 lines
Once you have the system of first order differential equations, you can use the ode45
function in matlab to solve it numerically. You will need to define a function handle that evaluates the right-hand side of the system for a given value of t
, z(t)
and w(t)
.
Here's an example code that solves the second order differential equation y''(t) + y(t) = 0
with initial conditions y(0) = 0
and y'(0) = 1
:
main.m284 chars12 lines
This will produce a plot of the solution y(t)
as a function of time t
.
gistlibby LogSnag