M371 - Alexiades
Lab 7: Solving ODEs
1. Implement the Explicit Euler Scheme for Initial Value Problems of the form:
y'(t) = F(t, y(t)) , t0 ≤ t ≤ tend
y(t0) = y0
The function F(t,y) should be coded in a function subprogram FCN(...).
Input data: t0, y0, tend, Nsteps. Thus the time-step will be h=(tend-t0)/Nsteps.
Your code should print out the input data and then the pairs:
tn Yn
At the end, it should print out the final n, tn, Yn
(appropriately labelled, of cource).
2. Solve, on paper, the (exponential growth) problem: y' = y , 0 ≤ t ≤ 1 , y(0) = −1
3. To debug your code, run it on the problem above.
Compare the numerical solution Yn with the exact solution yEXACT(tn),
i.e. modify your output to print out:
tn Yn yEXACTn ERRn
where ERRn = |Yn - yEXACT(tn)|, and keep track of the maximum error.
At the end of the run, print out the above values (at time tend)
and the maximum overall error ERRmax. Test with N=10 and N=100.
Turn off printing of tn Yn ... and test with N = 1000, 10000 and larger.
Once the code is debugged, only FCN(...) and input data need to be changed to solve other IVPs.
4. Now solve the IVP:
y' = −t/y , 0 ≤ t ≤ 1
y(0) = 1
Find the exact solution at t = 1 (by hand),
and compare the numerical and exact values at t = 1.
Try small (N=10) and larger (N=1000, 100000, ... ) number of time-steps.
At which time does the worst error occur in this problem ?
Plot the exact solution. Do you see why it occurs there?
5. Solve it using a Matlab / Octave / Python solver, each contains several:
Matlab: ode45 ode78 ode113 , and for stiff: ode15s ode23s ...
Octave: ode45 lsode ode15s ...
Python: scipy.integrate.solve_ivp , diffeqpy ...
Syntax for Matlab / Octave: ode45( @FCN, t_range, y0 )
e.g for the IVP in 2.: [t, y] = ode45( @(t,y) y , [0,1], -1 )
Try ode45 (adaptive 4th order) and some other(s) on the IVP in 4.
Submit, in a plain text file Lab7.txt:
a. output from 3. for N=10.
=================================================================
b. result from 4. for N=100000 (only the final result!!!)
=================================================================
c. How many correct digits did you get ? Is there a reason for the large errors ?
=================================================================
d. result from 5: show command you used and solution for t=1.
Did any do better than in b. ?
=================================================================
e. your Euler code, including FCN for problem in 4.