Need to do problem 1 a-c in MAT lab. The objective is to do a eulers function. T
ID: 2259845 • Letter: N
Question
Need to do problem 1 a-c in MAT lab. The objective is to do a eulers function. The first two pages is just an intro to the lab. After a-c has been done, the coding has to be published using the command >>publish('euler1.m', pdf). A graph and the function has to be included. The file name has to be euler1.m. Thanks again for the help, I have been working on this all week, but programming is not my strong suit.
MAT 275 Laboratory 3 Numerical Solutions by Euler and Improved Euler Methods scalar equations) In this session we look at basic numerical methods to help us understand the fundamentals of numerical approximations. Our objective is as follows. 1. Implement Euler's method as well as an improved version to numerically solve an IVP 2. Compare the accuracy and efficiency of the methods with methods readily available in MATLAB 3. Apply the methods to specific problens and investigate potential pitfalls of the methods. Instructions: For your lab write-up follow the instructions of LAB 1. Euler's Method To derive Euler's method start from y(to)-o and consider a Taylor expansion at ti-to+h: y(to) + y'(to)(ti-to) + yo + hf(to , y(to)) + y(t) = = For small enough h we get an approximation yi for y(ti) by suppressing the ..., namely Y1 = yo + hf(to,yo) The iteration (L3.1) is repeated to obtain y2y(t2),. .. such that yn+1 tn+1 yn + hf(tn,yn) tn + h = = y(ti) Geometrically, the approximation made is equivalent to replacing the i solution curve by the tangent line at (to, yo). From the figure we have yi-w yo f(to, yo) = f(to, y(to))-y'(to) tan from which (L3.1) follows As an example consider the IVP to t1 y, = 2y=f(t,y) with y(0)= 3. Note that here f does not explicitly depend on t (the ODE is called autonomous), but does implicitly through y y(t). To apply Euler's method we start with the initial condition and select a step size h. Since we are constructing arrays t and y without dimensionalizing them first it is best to clear these names in case they have been used already in the same MATLAB work session. » clear t y % no comma between t and y! type help clear for more info ince 1 is simple enough we may use the inline syntaks te amp s DianExplanation / Answer
% DEFINING THE FUNCTION
function [t,y] = euler (f, tspan, y0, N)
m= length(y0);
t0= tspan(1);
tf= tspan(2);
h= (tf - t0)/N;
t= linespace(t0, tf, N+1);
y= zeros(m, N+1);
y(: ,1)= y0;
for k= 1: N
y(: , k+1)= y(: , k) + h*f( t(k) , y(: , k) );
end
t = t' ;
y = y' ;
end
% ACTUAL MATLAB CODE TO SOLVE THE EQUATION
f = inline('2*y' , 't' , 'y');
t= linespace(0, 0.5, 100);
y= 3* exp(2*t) ; % given equation
[t5, y5] = euler(f, [0,0.5], 3, 5); % calling function with N=5
[t50, y50] = euler(f, [0,0.5], 3, 50); % calling function with N=50
[t500, y500] = euler(f, [0,0.5], 3, 500); % calling function with N=500
[t5000, y5000] = euler(f, [0,0.5], 3, 5000); % calling function with N=5000
% PLOTTING THE RESULTS
plot(t5, y5, 'o-', t50, y50, 'x-', t500, y500, '*-', t5000, y5000, '@-', t, y, 'k-');
legend( 'Euler N= 5', 'Euler N= 50', 'EULER N= 500', 'EULER N= 5000',4);
% CALCULATING THE ERROR
e5= y(end) - y5(end);
e50= y(end)- y50(end);
e500= y(end)- y500(end);
e5000= y(end)- y5000(end);
%CALCULATING THE RATIOS OF THE ERRORS
ratio1= e5/e50;
ratio2= e50/e500;
ratio3= e500/e5000;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.