How to solve this second order linear homogeneous ODE?

235 Views Asked by At

I'm trying to solve

$\frac{d^2y}{dt^2}+\frac{2dy}{dt}+10y=0$

Initial values $t=0$, $y=1$, $\frac{dy}{dt}=0$, $h=\frac{1}{1000}$.

Problem: Find $y$ when $t=1$ using the Heun Method to approximate it.

First I try to calculate it exactly using math, then compare it to the python program I made. The results were different so I am not sure where I got lost.

This is how I did it using math.

Let $\frac{dy}{dt}=z$.

Then I get two equations

$\frac{dy}{dt}=z=f_1(t,y,z),y(0)=1$

$\frac{dz}{dt}=-2z-10y=f_2(t,y,z),z(0)=0$

Since $1y''+2y'+10y=0$ doesn't have real roots, I get this form

$y=e^{-t}(c_1cos(3t)+c_2sin(3t))$

From that I get two equations

$y=e^{-t}(c_1cos(3t)+c_2sin(3t))$

$\frac{dy}{dt}=-e^{-t}((c_2+3c_1)sin(3t)+(c_1-3c_2)cos(3t))$

If I sub in $y=1, t=0$ and $\frac{dy}{dt}=0$, I get $c_1=1, c_2=\frac{1}{3}$

Then if I solve for $y$ with $t=1$, $c_1=1, c_2=\frac{1}{3}$, I get ~$0.37$.

In my python program, I have:

def f1(t,y,z):
    return z
def f2(t,y,z):
    return -2*z - 10*y

t=0
y=1
z=0
h=1/1000.

while t<1:
    k1y=f1(t,y,z)
    k1z=f2(t,y,z)

    k2y=f1(t+h,y+h*k1y,z+h*k1z)
    k2z=f2(t+h,y+h*k1y,z+h*k1z)

    t+=h
    y+=h*0.5*(k1y+k2y)
    z+=h*0.5*(k1z+k2z)

print y

And it prints $-0.346891856552$

I would expect them to be pretty much the same number. Does anyone know whats wrong?

Thanks

1

There are 1 best solutions below

8
On

When you converted the 2nd order ODE into the equivalent 1st order system of two ODE's, you put a typo in the second equation. Where you wrote $\frac{dz}{dt}=-2z-y=f_2(t,y,z),z(0)=0$, you should actually have $\frac{dz}{dt}=-2z-\color{#ff0000}{10}y=f_2(t,y,z),z(0)=0$. You made the same omission in your python code.