Lab 5

( Preparation for Project 1 )

Implement the Euler scheme for the scalar IVP:

The code should read (and print out): the initial point:

The evaluation of

Calculate the time-step

with timesteps:

NOTE: No arrays are actually needed...

1. Debug your code on the trivial problem:

by calculating

At each step, output

Output ERRmax at the end of the run.

Before you type in the code, you should write it on

to make sure the logic is right. Then enter the code and check that it finds the same values. Then run it with Nsteps=10.

Note that since the RHS of this ODE is just a function of t, the solution is simply the integral of f(t)=2t,

so in fact your code is performing

2. Test your code on:

Only need to change the FCN, and the exact solution; comment out previous f and enter this one.

Then COMMENT OUT PRINTING of tn,Yn,... and try Nsteps = 100, 1000, 5000, 10000 to see how the error decreases.

Here the RHS contains the unknown function y(t) itself, so this is no longer just a quadrature problem!

This example is discussed in the CSEP e-book, chapter: ODE, 2.2.1 Taylor series methods, Euler method (Example 8).

a. ERRmax for Nsteps=10 and for Nsteps=1000 from 1.

=============================================================

b. ERRmax for Nsteps= 1000 and Nsteps=5000 from 2.

=============================================================

c. Your code.

You can use gnuplot to plot several curves on the same plot, from columns of a file, as follows:

should begin with # (e.g. input values, column labels, etc).

# Input values: t0=0.0000 y0= 1.0000 # tend= 5.000 Nsteps= 10 # tn Yn Yexactn ERRn .0 1.0 1.0 .0 .2 1.043 1.04 .3e-02 .4 1.159 1.16 -.1e-02 . . .The pairs (tn, Yn) represent (t, Y(t)) i.e. numerical approximation,

and the pairs (tn, Yexactn) are points of the curve y=Yexact(t).

We can easily plot both curves on the same plot:

gnuplot>Better yet, with different colors (lt=linetype, lw=linewidth):plot "out" using 1:2, "out" using 1:3

gnuplot>Linetypes: 1 = red, 2 = green, 3 = blue, 4 = magenta, 7 = black, 0 = black, -1 = solid blackplot "out" using 1:2 lt 1 lw 2, "out" using 1:3 lt 3 lw 2

To set labels:

gnuplot>You can save your settings in a file: gnuplot>set xlabel "time"gnuplot>set ylabel "y(t)"gnuplot>set title "Euler scheme & exact solution: example1"gnuplot>replot

Next time you enter gnuplot, do: gnuplot>

On linux, you can just do, at the linux prompt: gnuplot < example1.gplot

Better yet, get the template, edit it, and load it.