Dear Matlab, My Professor showed us in class today how to use ode45 to solve a differential equation numerically. I would like to use it on my homework but I have a second order differential equation and the example I saw in class was only first order. Can ode45 be used for second order diffeqs?

Respuesta :

Answer:

Yes

Explanation:

  • You can solve the second order differential equation in MATLAB using ode45 for that you just need to first convert you second order differential equation into two first order differential equations. The only difference will be that instead of a scalar now a vector will be used.

Answer:

Yes, ode45 function can be used for solving 2nd order differential equations.

Explanation:

The ode45 function can be used to solve 2nd order differential equations if we convert the 2nd order differential equation into a system of first order differential equations.

Suppose we have the following 2nd order differential equation and we want to plot its solution for 0 ≤ t ≤ 10 with initial conditions y(0) = 2, and y'(0) = 1

d²y/dt² + 6dy/dt + 4y = -sin(3t)

make d²y/dt² subject of the equation

d²y/dt² = - 6dy/dt - 4y - sin(3t)

define variable x such that

dy/dt = x    eq. 1

Now the above 2nd order differential equation can be written in terms of x as

dx/dt = -6x - 4y + sin(3t)  eq. 2

in vector form

z = [x; y]

dz = [dx/dt; dy/dt]

where z and dz are column vectors

So we have successfully converted the 2nd order differential equation into two 1st order differential equations.

Now we have to translate it into a Matlab function.

Matlab Function:

function dz = func(t, z)

dz=[-6*z(1) -4*z(2) -sin(3*t); z(1)];

end

Where z(1) means x and z(2) means y

Save this function as func.m file in the Matlab directory.

Now we need driver code to call this function and plot its solution

Driver Code:

initial = [2; 1];  % given initial conditions of differential equation

range = [0 10]; % plot the solution in the range 0 ≤ t ≤ 10

[t, z]=ode45(@func,range,initial);   % call 'func' in the ode45 function

plot(t, z(:,2))   % plot the results

grid on

Output:

Please refer to the attached image

Ver imagen nafeesahmed
ACCESS MORE
EDU ACCESS
Universidad de Mexico