Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

and here is the original code in Python: def euler (fcn, y0, t0,tend,dt): Yn = y

ID: 3109136 • Letter: A

Question

and here is the original code in Python:

def euler (fcn, y0, t0,tend,dt):
Yn = y0
tn = t0
ERRmax = 0
  
for n in range (0, MaxN+1):
Yn = Yn + dt*fcn(tn,Yn)
tn = tn + dt
yexact = exp(tn)
ERRn = abs(yexact - Yn)
ERRmax = max(ERRmax,ERRn)
print ('{:4.0f} {:12.6f} {:12.4f} {:18.15f} {:18.15f} {:18.15f} '.format(n, tn, Yn, yexact, ERRn, ERRmax))

It is easy to modify your Euler scheme code for a system of (two) ODEs y1 (t) f1 (t, y1(t), y2(t)) y1(to) 10 y2(to) 20 Copy the file with your scalar Euler scheme code to a file with a new name (e.g. Euler2.m), and make the necessary changes You can simply rename the single old unknown to y1n, and introduce the 2nd unknown y2n (and y20, and f2), and a 2nd Euler update for y2. he functions f1 and f2 should be calculated in a FCN subprogram Debug your code on the easy problem, for 0 sts 1 y1(0) 1 y2 y1 This can be solved exactly: Setting zay1, we have z y1 Ey2, and z'' y2 y1 z, so this system is equivalent to the 2nd order linear ODE z" z 0, with ICs: z(0) 1, z (0) 0. Solve it to show that the unique solution is: y1(t)Ez(t) cosh(t), y2(t) z'(t) sinh (t) For debugging, calculate Y1exact(tn) cosh(tn) and the error ERRn -ABS (Y1exact -y1no At each time step, output: tn y1n Y1exactna ERRn and calculate the worst overall error: ERRmax MAX( ERRmax, ERRn). Output ERRmax at the end. After debugging, comment out the printing Make runs with N-1000, N-5000 N-10000 time-steps. Observe how the error decreases

Explanation / Answer

clc;
clear all;
f1=@(y1,y2,t) y2;
f2=@(y1,y2,t) y1;
n=input('n=')
h=1/n;
t=0:h:1;
y1(1)=1;
y2(1)=0;
for i=1:n
y1(i+1)=y1(i)+h*f1(y1(i),y2(i),t(i));
y2(i+1)=y2(i)+h*f2(y1(i),y2(i),t(i));
end

for i=1:n
y_exact(i)=cosh(t(i));
end
for i=1:n
y_error(i)=abs(y_exact(i)-y1(i));
end
max_error=0;
%for i=1:n
max_error=max(max_error,y_error);
%end
for i=1:n
fprintf('%f %f %f %f %f ',t(i),y1(i),y_exact(i),y_error(i),max_error(i))
end