Euler's Method p. 110 #4

3136 days ago by dwatson

Draw a directional field to get an idea of what the solution is:

t,y=var('t,y') df = 3*cos(t)-2*y v=plot_slope_field(df,(t,0,3),(y,-3,3)) plot(v) 
       

#4a. Find approximate values of the solution of the given initial value problem at t = 0.1, 0.2, 0.3, and 0.4 using the Euler method with h = 0.1

# differential equation f(t,y) = 3*cos(t)-2*y # initial value t = 0 y = 0 # step size h = 0.1 # Euler's method y_n+1 = y_n + f_n*h for j in range(4) : y = y + f(t,y)*h t = t + h print t.n(digits=2), y.n(digits=4) 
       
0.10 0.3000
0.20 0.5385
0.30 0.7248
0.40 0.8665
0.10 0.3000
0.20 0.5385
0.30 0.7248
0.40 0.8665

#4b. Repeat part (a) with h = 0.05.  Compare the results with those found in (a).

f(t,y) = 3*cos(t)-2*y t = 0 y = 0 h = 0.05 for i in range(4) : for j in range(2) : y = y + f(t,y)*h t = t + h print t.n(digits=2), y.n(digits=5) 
       
0.10 0.28481
0.20 0.51334
0.30 0.69345
0.40 0.83157
0.10 0.28481
0.20 0.51334
0.30 0.69345
0.40 0.83157

#4c. Repeat part (a) with h = 0.025.  Compare the results with those found in (a) and (b).

f(t,y) = 3*cos(t)-2*y t = 0 y = 0 h = 0.025 for i in range(4) : for j in range(4) : y = y + f(t,y)*h t = t + h print t.n(digits=2), y.n(digits=5) 
       
0.10 0.27792
0.20 0.50181
0.30 0.67895
0.40 0.81530
0.10 0.27792
0.20 0.50181
0.30 0.67895
0.40 0.81530

#4d. Find the solution of the given problem and evaluate at t = 0.1, 0.2, 0.3, and 0.4.  Compare these values with the results of (a), (b), and (c).

x = var('t') y = function('y', t) a = desolve(diff(y,t)-3*cos(t)+2*y, y, ics=[0,0]) a.expand() 
       
6/5*cos(t) - 6/5*e^(-2*t) + 3/5*sin(t)
6/5*cos(t) - 6/5*e^(-2*t) + 3/5*sin(t)
t = 0.1 h = 0.1 for j in range(4) : a(t) t = t + h 
       
0.271428144628150
0.490897436643760
0.665141947634699
0.799729441247987
0.271428144628150
0.490897436643760
0.665141947634699
0.799729441247987
d = contour_plot(C, (x,-2, 2), (y,-2,2),fill = False, linewidths = 5, contours = 30, cmap = 'hsv')