1. dy ---- = f(t,y) Let f(t,y) = t . The y(t) = t^2 /2 + c1. dt In MATLAB for f(t,y)=t we can solve symbolically this equation as follows. >> y = dsolve('Dy = t ', 't') %D is d % ---- for MATLAB % dt y = 1/2 t^2 + c1 >> eq1 = 'Dy=t'; >> y = dsolve (eq1,'t'); % If initial condition is y(0)=1 % then >> y = dsolve(eq1,'y(0)=1', 't') y = 1/2 t^2 + 1 >> init1 = 'y(0)=1'; >> y = dsolve(eq1,init1,'t'); >> t = linspace(0,1,20); >> z = eval(vectorize(y)); >> plot(t,z); f(t,y) is then dy/dt= f(t,y) has solution y(t) equal to (for y(0)=1) 0 1 t t^2 /2 +1 y exp(t) -y exp(-t) 2. dy ya - ya --- = ------- = f(t,y) = -y . For this example we use f(t,y)=y i.e. solution will be y(t)=exp(-t) dt ta - tb Numerical approximation >> t = linspace(0,1,20); >> y = exp(-t); >> plot(t,y); hold on; >> dyoverdt = diff(y) ./ diff(t) % for y=[y(1) .... y(n-1) y(n)] % diff(y)=[y(2)-y(1) ... y(n)-y(n-1)] >> plot(t,-y); >> plot(t(1:end-1),dyoverdt,'r--'); In other words dy yn+1 - yn ' ---- = ------------- = y = f(t,y) = f(tn,yn) dt tn+1 - tn from which we get that yn+1 = yn + (tn+1 - tn) f(tn,yn) = yn + h f(tn,yn) if we denote by h = tn+1 - tn 3. Solver [ t y ] = solver ( @function , span, initialy0 ) solver can be ode45 ode23 oder113 etc >> f = @(t, y) (t) % f(t,y) =t i.e. y(t) = t^2 /2 +1 >> g = @ (t) ( 0.5 .* t .^ 2 +1 ) % g(t) is thus y(t) exact solution! >> ta = [ 0: 0.1 : 1]; >> ya = g(ta) % compute exact solution ! >> [ t y ] = ode45 ( f, [ 0 1], 1) % approximate solution >> plot(ta,ya,'r--'); hold on ; % plot exact solution >> plot(t,y, 'b--'); % plot approximate solution Note If you change >> ta = [ 0: 0.1 : 1]; into say >> ta = [ 0: 0.1 : 5]; then change >> [ t y ] = ode45 ( f, [ 0 1], 1) % approximate solution into >> [ t y ] = ode45 ( f, [ 0 5], 1) % approximate solution If initial condition is not y(0)=1, then g(t) needs to be recomputed! (use section 1 ) and of course this -------V-and-V will need to be adjusted appropriately V V >> [ t y ] = ode45 ( f, [ 0 1], 1) 4. ------------------------------------- Last fall semester , some students found this code useful % math2.m y=0.5; for t=0.01:0.01:25 n = round(t/0.01); y = y + 0.01*(3.0-5*sqrt(y)); fprintf('t= %5.2f n= %3d y= %8.4f\n',t,n,y); end % math0.m T=25; H=0.25; STEP=H; Y0=0.5; y=Y0; for t=H:STEP:T n = floor(t/0.01); y = y + 0.01*(3.0-5*sqrt(y)); if (mod(n,100) == 0) fprintf('t= %5.2f n= %6d y= %8.4f\n',t,n,y); end end %% math1.m T=25; H=0.25; STEP=H; y=0.5; for t=H:STEP:T n = floor(t/0.01); y = y + 0.01*(3.0-5*sqrt(y)); if (mod(n,100) == 0) fprintf('t= %5.2f n= %6d y= %8.4f\n',t,n,y); end end %%