Solving Models with Euler’s Method In MatLab James K. Peterson Department of Biological Sciences and Department of Mathematical Sciences Clemson University March 26, 2014 Outline Euler’s Method Review Euler’s Method: Writing a Function To Do the Job Using Euler’s Method and Plotting Abstract This lecture is about Euler’s Method for solving our models again. Now we add plots. I Here is Euler’s Algorithm to approximate the solution to x 0 = f (x) with x(0) = x0 using step size h for as many steps as we want. I I I I I I I x̂0 = x0 so E0 = 0. x̂1 = x̂0 + f (x̂0 ) h; E1 = |x1 − x̂1 |. x̂2 = x̂1 + f (x̂1 ) h; E2 = |x2 − x̂2 |. x̂3 = x̂2 + f (x̂2 ) h; E3 = |x3 − x̂3 |. x̂4 = x̂3 + f (x̂3 ) h; E4 = |x4 − x̂4 |. Continue as many steps as you want. Recursively: I I x̂0 = x0 so E0 = 0. x̂n+1 = x̂n + f (x̂n ) h; En+1 = |xn+1 − x̂n+1 | for n = 0, 1, 2, 3, .... We have written this function to handle our Euler approximation calculations. f u n c t i o n [ x h a t , x , e ] = D o E u l e r ( f , t r u e , N, x0 , h ) % N = number o f a p p r o x i m a t i o n s t o f i n d % f = t h e model d y n a m i c s f u n c t i o n % true = the true s o l u t i o n % x0 = t h e i n i t i a l c o n d i t i o n % h = the step s i z e % define euler step function e u l e r = @( x , h ) x + f ( x ) ∗ h ; % i n i t i a l i z e x , x h a t and e x h a t ( 1 ) = x0 ; x ( 1 ) = x0 ; e ( 1 ) = 0 ; f o r i =1:N−1 x h a t ( i +1) = e u l e r ( x h a t ( i ) , h ) ; x ( i +1) = t r u e ( i ∗ h ) ; e ( i +1) = a b s ( x ( i +1) − x h a t ( i +1) ) ; end end We can also plot the true solution versus the Euler approximates. Here is the code with annotations. We solve x 0 = .5x(60 − x); x(0) = 20. % Define the dynamics >> f = @( x ) . 5 ∗ x . ∗ ( 6 0 − x ) ; % Define the true s o l u t i o n >> t r u e = @( t ) 6 0 . / ( 1 + ( 6 0 / 2 0 − 1 ) ∗ e x p ( − . 5 ∗ 6 0 ∗ t ) ) ; % s e t up f i n a l t i m e and s t e p s i z e >> T = . 6 ; h = .01; % f i n d number o f s t e p s >> N = c e i l (T/ h ) ; % s e t up a t i m e v e c t o r f o r t h e t r u e s o l u t i o n >> t i m e = l i n s p a c e ( 0 , T , 3 1 ) ; % f i n d the e u l e r approximates >> [ x h a t , x , e ] = D o E u l e r ( f , t r u e , N, 2 0 , h ) ; % s e t up a t i m e v a r i a b l e f o r t h e a p p r o x i m a t e s >> h t i m e = ( 0 : h : N∗ h ) ; % p l o t them b o t h >> p l o t ( t i m e , t r u e ( t i m e ) , h ti m e , x h a t , ’ ∗ ’ ) ; Now set up labels and so forth. >> >> >> >> x l a b e l ( ’ Time ’ ) ; ylabel ( ’x ’) ; t i t l e ( ’ True S o l u t i o n v s . E u l e r A p p r o x i m a t e ’ ) ; l e g e n d ( ’ True ’ , ’ E u l e r ’ , ’ L o c a t i o n ’ , ’ Best ’ ) ; The line where we use N=ceil(T/h) does the division of T by h and rounds it up to the nearest integer. That way we get the right value of N even if h does not divide T evenly. We set up the time points for the euler approximate values using the command htime =(0:h:N*h); which is a new one. It sets up time points every h units from 0 to the final one which is Nh. Sometimes it is convenient to use this command. When we run this code, we generate the plot you see the figure below and you can see visually that the Euler Approximates are doing a good job. It is also easy to plot just the Euler approximations alone by simply changing what we plot. For example, the Matlab session below does just that. We don’t define a time variable as we are not plotting the true solution. >> f = @( x ) . 5 ∗ x . ∗ ( 6 0 − x ) ; >> t r u e = @( t ) 6 0 . / ( 1 + ( 6 0 / 2 0 − 1 ) ∗ e x p ( −.5∗60∗ t ) ) ; >> T = . 6 ; >> h = . 0 1 ; >> N = c e i l (T/ h ) ; % f i n d the e u l e r approximates >> [ x h a t , x , e ] = D o E u l e r ( f , t r u e , N, 2 0 , h ) ; % s e t up a t i m e v a r i a b l e f o r t h e a p p r o x i m a t e s >> h t i m e = ( 0 : h : N∗ h ) ; % p l o t them b o t h >> p l o t ( h t i m e , x h a t , ’ ∗ ’ ) ; >> x l a b e l ( ’ Time ’ ) ; >> y l a b e l ( ’ x ’ ) ; >> t i t l e ( ’ E u l e r A p p r o x i m a t e ’ ) ; We can also plot euler approximates for different step sizes at the same time, with or without the true solution. As an example, the Matlab session would look like this: >> f = @( x ) . 5 ∗ x .∗(60 − x ) ; >> t r u e = @( t ) 6 0 . / ( 1 + ( 6 0 / 2 0 − 1 ) ∗ e x p ( −.5∗60∗ t ) ) ; >> T = . 6 ; % s e t up a t i m e v e c t o r f o r t h e t r u e s o l u t i o n p l o t >> t i m e = l i n s p a c e ( 0 , T , 3 1 ) ; % Do s t e p s i z e 1 >> h1 = . 0 4 ; >> N1 = c e i l (T/ h1 ) ; % f i n d t h e e u l e r a p p r o x i m a t e s f o r h1 >> [ x h a t 1 , x1 , e1 ] = D o E u l e r ( f , t r u e , N1 , 2 0 , h1 ) ; % s e t up a t i m e v a r i a b l e f o r t h e a p p r o x i m a t e s >> h t i m e 1 =(0 : h1 : N1∗h1 ) ; % Do s t e p s i z e 2 >> h2 = . 0 2 ; >> N2 = c e i l (T/ h2 ) ; % f i n d t h e e u l e r a p p r o x i m a t e s f o r h2 >> [ x h a t 2 , x2 , e2 ] = D o E u l e r ( f , t r u e , N2 , 2 0 , h2 ) ; % s e t up a t i m e v a r i a b l e f o r t h e a p p r o x i m a t e s >> h t i m e 2 =(0 : h2 : N2∗h2 ) ; % plot a l l three >> p l o t ( t i me , t r u e ( t i m e ) , h t i m e 1 , x h a t 1 , ’ ∗ ’ , h t i m e 2 , x h a t 2 , ’ o ’ ) ; >> x l a b e l ( ’ Time ’ ) ; >> y l a b e l ( ’ x ’ ) ; >> t i t l e ( ’ True S o l u t i o n v s . E u l e r A p p r o x i m a t e s ’ ) ; >> l e g e n d ( ’ True ’ , ’ E u l e r h1 = . 0 4 ’ , ’ E u l e r h2 = . 0 2 ’ , ’ L o c a t i o n ’ , ’ Best ’ ) ; Here is the resulting plot. Homework 56 For these problems, use the Matlab session fragments above to find the plots of the true solution vs. the Euler Approximates for the list of step sizes given but put all the plots in the same graph. 56.1 x 0 = 1.58x with x(0) = 4 for h = .4 and h = .2. 56.2 x 0 = −1.03x with x(0) = 8 for h = .6 and h = .3. 56.3 x 0 = .4x(105 − x) with x(0) = 19 for h = .06 and h = .03. 56.4 x 0 = −.2x + 15 with x(0) = 10 for h = .5 and h = .25.
© Copyright 2026 Paperzz