This is about using the Matlab functions and scripts discussed in class. First script: ShowEuler.m while input('Another Step Size Choice? (1=yes, 0=no). '); fname = input('Enter Function Name: '); h = input('Input Step Size '); t0 = input('Enter initial time '); tmax = input('Enter final time: '); y0 = input('Enter initial state: '); freq = input('Input print frequency '); s = ['Euler(' fname sprintf(',%6.3f,%6.3f,%6.3f,%6.3f)',y0,t0,tmax,h)]; disp([' tvals ' s]) disp(' ') [tvals,yvals] = FixedEuler(fname,y0,t0,tmax,h); N = size(tvals,1); disp(sprintf(' %5.2f %20.16f',tvals(1),yvals(1))) for m = freq+1:freq:N disp(sprintf(' %5.2f %20.16f',tvals(m),yvals(m))) end disp(sprintf(' %5.2f %20.16f',tvals(N),yvals(N))) end and the script PlotEuler.m while input('Another Step Size Choice? (1=yes, 0=no). '); fname = input('Enter Function Name: '); truename = input('Enter True Solution Name '); h = input('Input Step Size '); t0 = input('Enter Initial Time '); tmax = input('Enter Final Time: '); y0 = input('Enter Initial State: '); s = ['Euler(' fname sprintf(',%6.3f,%6.3f,%6.3f,%6.3f)',y0,t0,tmax,h)]; disp([' tvals ' s]) disp(' ') [tvals,yvals] = FixedEuler(fname,y0,t0,tmax,h); N = size(tvals,1); truevals = zeros(N,1); for n=1:N truevals(n) = feval(truename,t0,y0,tvals(n)); end plot(tvals,truevals,tvals,yvals,'*'); print -dpng eulerplot end Now let’s do an example problem. u’ = 2u u(0) = 4 We know the true solution is u(t) = 4 e2t Let’s approximate solution using Euler’s method: uhat(1) = 4 uhat(new) = uhat(old) + 2 uhat(old) h where h is our chosen stepsize. We can apply this using the MatLab script ShowEuler. We use the MatLab function func.m seen below: function y = func(t,x) % % right hand side dynamics % t = time % x = ODE variable % y = 2*x; and we use the MatLab code for the trae solution in treufunc.m seen below: function y = truefunc(t0,y0,t) % % true solution to y' = 2y; y(t_0) = y0 % % t = time % t0 = initial time % y0 = initial condition % y = y0*exp(2*(t-t0)); The we run ShowEuler.m as follows: >> ShowEuler Another Step Size Choice? (1=yes, 0=no). 1 Enter Function Name: 'func' Input Step Size .05 Enter initial time 0 Enter final time: 2 Enter initial state: 4 Input print frequency 5 tvals Euler(func, 4.000, 0.000, 2.000, 0.050) 0.00 4.0000000000000000 0.25 6.4420400000000004 0.50 10.3749698404000000 0.75 16.7089926776626070 1.00 26.9099997973024050 1.25 43.3388237735534930 1.50 69.7976090755456510 1.75 112.4097473922570200 2.00 181.0370222727038400 2.00 181.0370222727038400 Another Step Size Choice? (1=yes, 0=no). To see how the Euler approximate solution compares to the true solution, We use the MatLab script PlotEuler.m. >> PlotEuler Another Step Size Choice? (1=yes, 0=no). 1 Enter Function Name: 'func' Enter True Solution Name 'truefunc' Input Step Size .05 Enter Initial Time 0 Enter Final Time: 2 Enter Initial State: 4 tvals Euler(func, 4.000, 0.000, 2.000, 0.050) Another Step Size Choice? (1=yes, 0=no). The resulting plot is seen below: Now let’s do a logistics problem. u’ = .05 u ( 200 – u) u(0) = 280 The new func.m file is function y = func(t,x) % % right had side dynamics % t = time % x = ODE variable % alpha = .05 % L = 200 alpha = .05; L = 200; y = alpha*x.*(L-x); and the new truefunc.m is function y = truefunc(t0,y0,t) % % true solution to % y' = alpha y(L-y); % y(t0) = y0 % % t = time % t0 = initial time % y0 = initial condition % L = 200; alpha = .05; B = L/y0; y = L./(1 + (B-1)*exp(-alpha*L*(t-t0))); % 1 2 2 2 3 321 We approximate using ShowEuler.m. >> ShowEuler Another Step Size Choice? (1=yes, 0=no). 1 Enter Function Name: 'func' Input Step Size .05 Enter initial time 0 Enter final time: 2 Enter initial state: 280 Input print frequency 2 tvals Euler(func,280.000, 0.000, 2.000, 0.050) 0.00 280.0000000000000000 0.10 210.5600000000000000 0.20 202.4380775963033700 0.30 200.5984640080211500 0.40 200.1489457912178400 0.50 200.0371948718575400 0.60 200.0092961243013000 0.70 200.0023238690467300 0.80 200.0005809571360700 0.90 200.0001452386511900 1.00 200.0000363096232500 1.10 200.0000090774033500 1.20 200.0000022693506900 1.30 200.0000005673376500 1.40 200.0000001418343900 1.50 200.0000000354585800 1.60 200.0000000088646500 1.70 200.0000000022161500 1.80 200.0000000005540200 1.90 200.0000000001385000 2.00 200.0000000000346200 2.00 200.0000000000346200 Another Step Size Choice? (1=yes, 0=no). 1 Enter Function Name: 'func' Input Step Size .2 Enter initial time 0 Enter final time: 2 Enter initial state: 280 Input print frequency 1 tvals Euler(func,280.000, 0.000, 2.000, 0.200) 0.00 280.0000000000000000 0.20 56.0000000000000000 0.40 136.6400000000000100 0.60 223.2151040000000000 0.80 171.3954854626918500 1.00 220.4223320181574200 1.20 175.4069515312440000 1.40 218.5448681388901900 1.60 178.0160105182215500 1.80 217.1510315464290100 2.00 179.9073896225049500 2.00 179.9073896225049500 Another Step Size Choice? (1=yes, 0=no). 0 >> We now use PlotEuler.m to see results graphically. >> PlotEuler Another Step Size Choice? (1=yes, 0=no). 1 Enter Function Name: 'func' Enter True Solution Name 'truefunc' Input Step Size .2 Enter Initial Time 0 Enter Final Time: 2 Enter Initial State: 280 tvals Euler(func,280.000, 0.000, 2.000, 0.200) Another Step Size Choice? (1=yes, 0=no). With graph generated below: