Question

In: Computer Science

1. Write a python code that uses the Runge Kutta Method method to approximate the solutions...

1. Write a python code that uses the Runge Kutta Method method to approximate the solutions to each of the following initial-value problems and compare/plot the results to the actual values.

a) y′=te^(3t) − 2y, 0 < t < 1, y(0) = 0

with h = 0.5; actual solution y(t)=1/5te^(3t) − 1/25e^(3t) + 1/25e^(−2t).

- Use the Runge Kutta method to approximate/plot the solutions to each of the following initial-value

b) ?′=1+(?−?)2,2<?<3,?(2)=1y′=1+(t−y)2,2

c) ?′=1+??,1<?<1,?(1)=2y′=1+yt,1

Solutions

Expert Solution

# Some basic functional programming libraries
from functools import reduce
from operator import mul

def runge_kutta(f, t0, y0, h):
'''
Classical Runge-Kutta method for dy/dt = f(t, y), y(t0) = y0,
with step h, and the specified tolerance and max_steps.
This function is a generator which can give infinitely many points
of the estimated solution, in pairs (t[n], y[n]).
To get only finitely many values of
the solution we can for example do,
>>> from itertools import islice
>>> list(islice(runge_kutta(f, t0, h), n))
[(t[0], y[0]), (t[1], y[1]), ..., (t[n], y[n])]
and infact, we could define another function to do this like,
>>> runge_kutta_N = lambda f, t0, y0, h, N: list(islice(
... runge_kutta(f, t0, y0, h), N))
It would also be easy to change this function to take an extra
parameter N and then return a list of the first N, (t_n, y_n),
directly (by replacing the while loop with for n in range(N)).
Note also that whilst the question asks for a solution, this
function only returns an approximation of the solution at
certain points. We can turn use this to generate a continuous
function passing through the points specified using either of
interpolation methods specified lower down the file.
'''
# y and t represent y[n] and t[n] respectively at each stage
y = y0
t = t0

# Whilst it would be more elegant to write this recursively,
# in Python this would be very inefficient, and lead to errors when
# many iterations are required, as the language does not perform
# tail call optimisations as would be the case in languages such
# as C, Lisp, or Haskell.
#
# Instead we use a simple infinite loop, which will yield more values
# of the function indefinitely.
while True:
# Generate the next values of the solution y
yield t, y

# Values for weighted average (compare with Wikipedia)
k1 = f(t, y)
k2 = f(t + h/2, y + (h/2)*k1)
k3 = f(t + h/2, y + (h/2)*k2)
k4 = f(t + h/2, y + h*k3)

# Calculate the new value of y and t as per the Runge-Kutta method
y = y + (h/6)*(k1 + 2*k2 + 2*k3 + k4)
t = t + h

def linear_interpolation(tys):
'''Takes a list of (t, y(t)) values (presumed to be in increasing
order of t), and interpolates to a piecewise linear function
passing through each point.
Heavily inspired by: http://en.wikipedia.org/wiki/Linear_interpolation
'''
# The t values
ts = [t for t, y in tys]
# The y values
ys = [y for t, y in tys]
# The number of values
n = len(tys) # = len(ts) = len(ys)

if n < 2:
raise ValueError("Not enough points to interpolate!")

def z(x):
# If x is beyond the lower end of the data, we extrapolate
# based on the first two values.
if x <= min(ts):
i = 0
return ys[0] + (ys[1] - ys[0]) * (x - ts[0])/(ts[1] - ts[0])
# If x is beyond the upper end of the data, we extrapolate
# based on the last two values.
elif x >= max(ts):
i = n-2
else:
# Find the interval i, that x lies within
i = next(i for i in range(n - 1) if ts[i] <= x <= ts[i+1])

# Interpolate within our chosen interval
return ys[i] + (ys[i+1] - ys[i]) * (x - ts[i])/(ts[i+1] - ts[i])

# Return a continous function z which interpolates between the given values
return z

# A concise definition of the product of a list/iterable of numbers
# (sum is built in but product is not)
product = lambda xs: reduce(mul, xs, 1)

def polynomial_interpolation(tys):
'''
Takes a list of n (t, y(t)) values (presumed to be in increasing
order of t), and interpolates to a nth order polynomial
passing through each point.
Uses the Lagrange polynomial:
- http://en.wikipedia.org/wiki/Lagrange_polynomial
- http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html
Note that this implementation could easily be made much faster
at the expense of clarity (and similarity to the Wikipedia
article).
'''
# The t values
ts = [t for t, y in tys]
# The y values
ys = [y for t, y in tys]
# The number of values
n = len(tys) # = len(ts) = len(ys)

# Return the interpolation function
return lambda x: sum(ys[j]*product((x - ts[k])/(ts[j] - ts[k])
for k in range(n) if j != k) for j in range(n))

if __name__ == '__main__':
# A demonstration of the method for,
# dy/dt = (1/3)*y + e^t*y^4, y(0) = 1
# (chosen to test the method, as an explict solution does exist)
print('dy/dt = (1/3)*y + e^t*y^4, y(0) = 1')
from itertools import islice
from math import exp
# Evaluate the solution at some points
tys = list(islice(runge_kutta(
lambda t, y: (1/3)*y + exp(t)*y**4, # f(t, y)
0, # t0
1, # y0
0.001,
), 200))
# Print some values
print('\n'.join('y({:.3f}) = {}'.format(t, y) for t, y in tys))

# The explicit solution of the equation
z = lambda t: ((-2)**(1/3)*exp(t/3))/(3*exp(2*t) - 5)**(1/3)
# Calculate maximum absolute error
e_abs_max = max(abs(y - z(t)) for t, y in tys)
# Calculate maximum relative error
e_rel_max = max(abs((y - z(t))/z(t)) for t, y in tys)
# Print the errors
print('Maximum absolute error: ', e_abs_max)
print('Maximum relative error:', e_rel_max)

# To check the interpolation is not completely messed up, check it
# agrees on the actual data points
z_linear = linear_interpolation(tys)
z_polynomial = polynomial_interpolation(tys)
print('Linear interpolation agrees?',
'Yes' if all(y == z_linear(t) for t, y in tys) else 'No')
print('Polynomial interpolation agrees?',
'Yes' if all(y == z_polynomial(t) for t, y in tys) else 'No')

try:
# Plot some graphs to compare the results, this time with only
# 20 points to compare interpolation methods more clearly
import matplotlib.pyplot as plt

# Evaluate the solution at some points
tys2 = list(islice(runge_kutta(
lambda t, y: (1/3)*y + exp(t)*y**4, # f(t, y)
0, # t0
1, # y0
0.02,
), 10))
ts = [t for t, y in tys2]
ys = [y for t, y in tys2]
z_linear_2 = linear_interpolation(tys2)
z_polynomial_2 = polynomial_interpolation(tys2)
ts_dense = [n*0.001 for n in range(200)]
plt.plot(ts_dense, [z_linear_2(t) for t in ts_dense], 'g',
label='Linear')
plt.plot(ts_dense, [z_polynomial_2(t) for t in ts_dense], 'r',
label='Polynomial')
plt.plot(ts_dense, [z(t) for t in ts_dense], 'y',
label='Exact solution')
plt.plot(ts, ys, 'b^', label='Runge-Kutta')
plt.legend()
plt.ylabel("y")
plt.xlabel("t")
plt.show()
except ImportError:
print("Matplotlib required for graphs")

Is also Try:

import numpy as np
from math import sqrt

import matplotlib.pyplot as plt

# Parameters
mr = 2
m = 1
c = 5
k = 36
F = 3

# Equations:

def V(u,t):
x1, x2, v1, v2 = u
dx = x2-x1;
dv = v2-v1;

return np.array([ v1, v2, (c*dv+k*dx)/mr, -(F+c*dv+k*dx)/m ])

def rk4(f, u0, t0, tf , n):
t = np.linspace(t0, tf, n+1)
u = np.array((n+1)*[u0])
h = t[1]-t[0]
for i in range(n):
k1 = h * f(u[i], t[i])
k2 = h * f(u[i] + 0.5 * k1, t[i] + 0.5*h)
k3 = h * f(u[i] + 0.5 * k2, t[i] + 0.5*h)
k4 = h * f(u[i] + k3, t[i] + h)
u[i+1] = u[i] + (k1 + 2*(k2 + k3) + k4) / 6
return u, t


u, t = rk4(V, np.array([0., 0., 1., 1.]) , 0. , 1. , 10)
x1, x2, v1, v2 = u.T
plt.plot(t, x1, t, x2)
plt.grid('on')
plt.show()


Related Solutions

Write a python code that uses the Euler Implicit method to approximate/plot the solutions to each...
Write a python code that uses the Euler Implicit method to approximate/plot the solutions to each of the following initial-value y′=−ty+4t/y, 0 ≤ t ≤ 1, y(0)=1, with h=0.1 y′=y2+yt, 1 ≤ t ≤ 3, y(l)=−2, with h=0.2
Use the Runge-Kutta method and the Runge-Kutta semilinear method with the indicated step sizes to find...
Use the Runge-Kutta method and the Runge-Kutta semilinear method with the indicated step sizes to find approximate values of the solution of the given initial value problem at 11 equally spaced points (including the endpoints) in the interval. This question is from the differential equation. y'-4y = x/y^2(y+1) , y(0) = 1; h=0.1, 0.05 , 0.025, on [0, 1]
Use the Runge-Kutta method with step sizes h = 0.1, to find approximate values of the...
Use the Runge-Kutta method with step sizes h = 0.1, to find approximate values of the solution of y' + (1/x)y = (7/x^2) + 3 , y(1) = 3/2 at x = 0.5 . And compare it to thee approximate value of y = (7lnx)/x + 3x/2
Using Runge-Kutta method of order 4 to approximate y(1) with step size h = 0.1 and...
Using Runge-Kutta method of order 4 to approximate y(1) with step size h = 0.1 and h = 0.2 respectively (keep 8 decimals): dy/dx = x + arctan y, y(0) = 0. Solutions: when h = 0.1, y(1) = 0.70398191. when h = 0.2, y(1) = 0.70394257.
Problem Four (12 Marks) Use Runge Kutta method of order four to approximate the solution of...
Problem Four Use Runge Kutta method of order four to approximate the solution of the initial value problem ?′ + 2? = ??3?, 0 ≤ ? ≤ 1, ?(0) = 0, ???ℎ ℎ = 0.5 Hint: Compute ?(0.5) ??? ?(1)
Write a user-defined MATLAB function that uses classical fourth order Runge-Kutta method to solve a first...
Write a user-defined MATLAB function that uses classical fourth order Runge-Kutta method to solve a first order ODE problem dydx = f(x, y) in a given interval a ? x ? b with initial condition y(a) = y0 and step size of h. For function name and arguments, use [x,y] = myrk4(f, a, b, h, y0) Check your function to find the numerical solution for dydx=?1.2y+7e^(?0.3x) in the interval 0 ? x ? 4 with initial condition y(0)=3. Run your...
Prompt: Produce a 4th order Runge Kutta code in PYTHON that evaluates the following second order...
Prompt: Produce a 4th order Runge Kutta code in PYTHON that evaluates the following second order ode with the given initial conditions, (d^2y/dt^2) +4(dy/dt)+2y=0, y(0)=1 and y'(0)=3. After your code can evaluate the 2nd order ode, add a final command to plot your results. You may only use python.
Use Classic Runge-Kutta method with h = 1 to solve the system y” - y’ -...
Use Classic Runge-Kutta method with h = 1 to solve the system y” - y’ - 6y = 0, y(0) = 2, y’(0) = 3 on [0,1]
Use 3 steps of the Runge-Kutta (fourth order) method to solve the following differential equation to...
Use 3 steps of the Runge-Kutta (fourth order) method to solve the following differential equation to t = 2.4, given that y(0) = 2.3. In your working section, you must provide full working for the first step. To make calculations easier, round the tabulated value of y at each step to four decimal places. a) Provide the four K-values that are calculated at the first step, with four decimal places. b) Provide your answer for y(2.4) with four decimal places....
1)Select all that applies to the Fourth-order Runge-Kutta (RK4) method K subscript 1 equals f left...
1)Select all that applies to the Fourth-order Runge-Kutta (RK4) method K subscript 1 equals f left parenthesis t subscript k comma y subscript k right parenthesis K subscript 2 equals f left parenthesis t subscript k plus h over 2 comma space y subscript k plus h over 2 space K subscript 1 right parenthesis K subscript 3 equals f left parenthesis t subscript k plus h over 2 comma space y subscript k plus h over 2 space K...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT