MATLAB ASSIGNMENT 6 Euler s Improved Euler s In this lab we use numerical methods Euler s Improved Euler s to solve ordinary differential equations and compare the solutions to the exact solutions we show that a reduction in step size in an algorithm reduces the error in the numerical solution First create a function M file called euler m using the following code function tout yout euler f tspan y0 N solves the IVP y f t y y tspan 1 y0 up to t tspan 2 using Euler s method with N time steps use euler f tmin tmax y0 N or f h tspan 2 tspan 1 N t tspan 1 tout t y y0 yout y for n 1 N y y h feval f t y t t h yout yout y tout tout t end Then create the function M file called impeuler m using the following code function tout yout impeuler f tspan y0 N solves the IVP y f t y y tspan 1 y0 up to t tspan 2 using improved Euler s method with N time steps use impeuler f tmin tmax y0 N h tspan 2 tspan 1 N t tspan 1 tout t y y0 yout y for n 1 N f1 feval f t y f2 feval f t h y h f1 y y h f1 f2 2 t t h yout yout y tout tout t end Example Consider the IVP y 2 y f t y y 0 3 Verify that the exact solution is y t 3 exp 2t To apply Euler s Improved Euler s method to solve the problem on the interval 0 2 using step size 0 2 N 10 we first clear the names t and y and define the function f t y 2 y using the following commands clear t y no comma between t and y type help clear for more info f inline 2 y t y Use the inline syntax to define f tE yE euler f 0 2 3 10 use f if defined in separate function tE yE tI yI impeuler f 0 2 3 10 use f if defined in separate function tI yI We plot both approximations on the same figure together with the exact solution y t 3 exp 2t Using the following command lines t linspace 0 2 100 y 3 exp 2 t plot tE yE ro tI yI bx t y k axis tight legend Euler N 10 Impeuler N 10 Exact 2 To compare two approximations different step sizes for the same algorithm say Euler s we plot both approximations on the figure together with the exact solution y t 3 exp 2t using the following command lines t5 y5 euler f 0 0 5 3 5 step size 0 1 t10 y10 euler f 0 0 5 3 10 step size 0 05 plot t5 y5 ro t10 y10 bx t y k axis tight legend Euler N 5 Euler N 10 Exact 2 Exercises In Exercises 1 3 perform the following tasks for the given IVPs on the specified intervals a Find the exact solution b Use MATLAB to plot on a single window the graph of the exact solution together with the plots of the solutions using Euler s and Improved Euler s with the given step size Use distinctive markers and legends in the plots 1 2 3 y y sin 3t y 0 1 on 0 4 h 0 2 y y 2 1 cos t y 0 0 on 0 6 h 0 5 y 1 y t 2 y 0 3 on 0 2 h 0 25
View Full Document