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

 and here is the original code in Python: def euler (fcn,

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 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!