MATH 210 Practice Final Exam Questions
December 2019
1. Find all 12 errors in the following code. Indicate the line number and describe the error.
1 def euler(f,y0,t)
2 N = len(t)
3 y = np.zeros(N)
4 y[0] = y0
5 for n in range(0,N-1):
6 y[n+1] = y[n] + f(y[n],t[n])*h
7 print y
8 def f(y,t)
9
print y^2 – t
linspace(0,1,100)
euler(f,T,y0)
10 T=
11 Y=
12 plt.plot(T,Y)
13 plt.show()
2. Determine the correct order of the line numbers such that the following code plots the solutions of y′ = tsin(y), y(0) = 1, for t ∈ [0,1].
1 plt.plot(t,y)
2 def f(y,t):
3 import numpy as np
4 y = spi.odeint(f,y0,t)
5 t = np.linspace(0,1,100)
6 return t*np.sin(y)
7 plt.show()
8 import scipy.integrate
9 y0=1
3. Approximate all solutions of the equations x4 + x − 1 = 0 to 2 decimal places.
4. Write a Python function called k_sum which takes 2 input parameters x and N and returns the
sum
N x k
k
k=1
Do not import any packages. Use only builtin Python functions and datatypes.
5. Predict the value y(1) for y′ = y2 − t for y(0) = 1 using Euler’s method with h = 0.5.
6. Consider the system of equations
x ̈ = x − xy y ̈ = xy − y2
Complete the code below to plot the solution x(t) versus y(t) for t ∈ [0, 2] with initial conditions x(0) = 1,x ̇(0) = −1,y(0) = 2,y ̇(0) = 0.
import numpy as np
import scipy.integrate as spi
importmatplotlib.pyplot as plt
def odefun(u,t):
# Code required here
return dudt
# Code required here
plt.show()
7. Consider the integrals
1 −x2 1 e dx and
00
sin(x)dx
If we approximate each integral using the trapezoid rule with the same number of subintervals N, which approximation do we expect to have the smallest error? Justify your answer.