Question

Please write simple python code of y''+2y'-3y = 0, y(0) = 2, y'(0)=1 with modified euler...

Please write simple python code of y''+2y'-3y = 0, y(0) = 2, y'(0)=1 with modified euler method and 4th order Runge-Kutta Kutta method

0 0
Add a comment Improve this question Transcribed image text
Answer #1


'''
y'' + 2y' -3y = 0, y(0) = 2, y'(0)=1
let y' = z
giving 2 ODEs to solve simulteneously
z' + 2z - 3y = 0 and y' = z for y(0) = 2, z(0) = 1

assuming a step size h and time vector tmin<=t<=tmax
'''

import matplotlib.pyplot as plt1;import matplotlib.pyplot as plt2


# function to form and return the two 1st-order ODE simulteneous equations
def f(y,z):
dydt = z;
dzdt = 3*y - 2*z;

return dydt,dzdt

# funcion to map the output of funtion f() to only y
def fy(y,z):
[dydt,dzdt] = f(y,z)
return dydt

# funcion to map the output of funtion f() to only z
def fz(y,z):
[dydt,dzdt] = f(y,z)
return dzdt   


# setting the necessary constants
h = 0.1
tmin = 0; tmax = 1;
n = int((tmax-tmin)/h);
y0 = 2; z0 = 1;


# ---------- SOLVING THE ODEs SIMULTENEOUSLY --------

# modified euler

# setting initial conditions
t = [];y=[];z=[]
t.append(tmin);
y.append(y0);
z.append(z0);

for i in range(n):
'''
# ensure y arguments are always on the left, z arguments on the right of either fy or fz
# the x or t components of modified euler method are left out since they do not originally appear in the ODE
'''
ytemp = y[i] + (h/2)*( fy(y[i],z[i]) + fy( y[i]+ h*fy(y[i],z[i]), z[i]+h*fz(y[i],z[i]) ))
ztemp = z[i] + (h/2)*( fz(y[i],z[i]) + fz( y[i]+ h*fy(y[i],z[i]), z[i]+h*fz(y[i],z[i]) ))

# update the y and z vectors
y.append(ytemp)
z.append(ztemp)

# update the time vector
ttemp = t[i]+ h;
t.append(ttemp)

plt1.figure(1)
plt1.plot(t,y,'g-')
#plt1.plot(t,z,'r-')
plt1.xlabel('t');plt1.ylabel('y(t)'),plt1.title('Modified Euler')
plt1.grid()
#------------------------------------------------------------------

# 4th order Runge-Kutta Kutta method

# setting initial conditions
y = [];z = []; t=[]
t.append(tmin);
y.append(y0);
z.append(z0);

for i in range(n):
'''
# the K's are identified with y or z for each cartegory
# ensure y arguments are always on the left, z arguments on the right of either fy or fz
# the x or t components of 4th order Runge-Kutta Kutta method are left out since they do not originally appear in the ODE
'''
yK1 = h*fy(y[i],z[i])
zK1 = h*fz(y[i],z[i])

yK2 = h*fy(y[i]+yK1/2,z[i]+zK1/2)
zK2 = h*fz(y[i]+yK1/2,z[i]+zK1/2)

yK3 = h*fy(y[i]+yK2/2, z[i]+zK2/2)
zK3 = h*fz(y[i]+yK2/2, z[i]+zK2/2)

yK4 = h*fy(y[i]+yK3,z[i]+zK3)
zK4 = h*fz(y[i]+yK3,z[i]+zK3)

ytemp = y[i] + yK1/6 + yK2/3 + yK3/3 + yK4/6
ztemp = z[i] + zK1/6 + zK2/3 + zK3/3 + zK4/6

  
# update the y and z vectors
y.append(ytemp)
z.append(ztemp)

# update the time vector
ttemp = t[i]+ h;
t.append(ttemp)

plt2.figure(2)
plt2.plot(t,y,'g-')
#plt2.plot(t,z,'r-')
plt2.xlabel('t');plt2.ylabel('y(t)'),plt2.title('4th order Runge-Kutta Kutta')
plt2.grid()
plt1.show();plt2.show()

------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
Please write simple python code of y''+2y'-3y = 0, y(0) = 2, y'(0)=1 with modified euler...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT