1. Implement the Improved Euler's Method (ie the pseudo-code on pe 127 of the textbook) ning the MATLAB/Octave bles prov
-
- Site Admin
- Posts: 899559
- Joined: Mon Aug 02, 2021 8:13 am
1. Implement the Improved Euler's Method (ie the pseudo-code on pe 127 of the textbook) ning the MATLAB/Octave bles prov
1 00 OWN -function [x,y] = ma550_euler-.m( a, b, N, f, y_init) %---Compute and output the h value from N, a, x=b 1 = (-a) / 10; fprintf("Your value of h is: %.5f \n \n', h); 7 8 9 10 11 12 13 14 15 16 17 18 %--- Define our discrete x values X = a:1:6; %--- Incorporate the initial condition y(1) = 1; %---Construct the Euler loop for n=1:1 y(n+1) = y(n) + h * f(x(n), y(n)); end
1 2 3 4 5 6 ----Standard header close all clear clc mc 7 8 9 10 11 12 13 14 15 16 - This is the function corresponding to the right hand side f = @(x,y) y; %---This is the initial condition y_init = 1; *---Time interval on which we wish to solve the problem a = @ b = 1; -- Define the number of subintervals we use for approximation N = 10; 17 18 19 1 %---Call Euler's Method to obtain the approximate solution [x,y] = ma55e_euler-.m( a, b, N, f, y_init); 20 21 22 23 24 25 26 27 28 29 error = *--- Define the exact solution if it's available yexact - @(x) exp(1); %---Grab the error at the right endpoint abs(yexact(1) - y(end) ); fprintf("Your error at the endpoint is: %.5f \n \n', error); %---- Construct an array of exact solution values for plotting xexact = a: 0.001:b; for i=1:length(xexact) yexact_full(1) = yexact( xexact(i)); end 30 31 32 33 34 35 36 37 38 %---Plot the exact vs the approximate figure (1) plot( xexact, yexact_full, 'l', X, Y, 'r.-'); legend('Exact', 'Approx); 39 48 41 42