%============================================================================ function euler1(eps,N) % integrates a 1st order ODE via Euler's method % problem 1.5.11, Nagle et al. % to use: % run matlab6 % then type: euler1(.05,20) % it will produce a plot of the solution as well as a table of values disp(' ') disp( ' i t x(t) xe(t) ') disp( '--------------------------------------------') x(1)=0; t(1) = 0; xe(1) = tan(t(1));i = 0; disp(sprintf(' %2.0f %5.2f %8.5f %8.5f',i,t(1),x(1),xe(1))) for i = 1:N x(i+1) = x(i) + eps*(1+x(i)^2); t(i+1) = t(i) + eps; xe(i+1)= tan(t(i+1)); disp(sprintf(' %2.0f %5.2f %8.5f %8.5f',i,t(i+1),x(i+1),xe(i+1))) end disp( '--------------------------------------------') figure; axis([-2, 2 , -2, 2]) plot(t,x,t(1:N/10:N+1),xe(1:N/10:N+1),'ro') title(sprintf('graph of numerical vs. exact (o) solution to dx/dt=1+x^2, x0=0 with eps = %6.2f',eps)); %>> euler1(.05,20) % % i t x(t) xe(t) %-------------------------------------------- % 0 0.00 0.00000 0.00000 % 1 0.05 0.05000 0.05004 % 2 0.10 0.10013 0.10033 % 3 0.15 0.15063 0.15114 % 4 0.20 0.20176 0.20271 % 5 0.25 0.25380 0.25534 % 6 0.30 0.30702 0.30934 % 7 0.35 0.36173 0.36503 % 8 0.40 0.41827 0.42279 % 9 0.45 0.47702 0.48306 % 10 0.50 0.53840 0.54630 % 11 0.55 0.60289 0.61311 % 12 0.60 0.67106 0.68414 % 13 0.65 0.74358 0.76020 % 14 0.70 0.82123 0.84229 % 15 0.75 0.90495 0.93160 % 16 0.80 0.99589 1.02964 % 17 0.85 1.09548 1.13833 % 18 0.90 1.20549 1.26016 % 19 0.95 1.32815 1.39838 % 20 1.00 1.46635 1.55741 %-------------------------------------------- %============================================================================ function euler2(eps,N) % integrates a 1st order ODE via Euler's method % problem 1.5.15, Nagle et al. % to use: % run matlab6 % then type: euler2(.1,20) K = 1; M = 70; disp(' ') disp( ' i t T(t) ') disp( '---------------------------------------') T(1)=100; t(1) = 0; i=0; disp(sprintf(' %2.0f %5.2f %8.5f ',i,t(1),T(1))) for i = 1:N T(i) = T(i) eps*K*(M-T(i)); t(i) = t(i) eps; disp(sprintf(' %2.0f %5.2f %8.5f ',i,t(i),T(i))) end disp( '---------------------------------------') figure; axis([0, 2 , 0, 100]) plot(t,T,t(1:N/2:N),T(1:N/2:N),'ro') title(sprintf('graph of solution to dT/dt=K*(M-T), T0=100 with eps = %12.5f',eps)); %>> euler2(.1,20) % % i t T(t) %--------------------------------------- % 0 0.00 100.00000 % 1 0.10 97.00000 % 2 0.20 94.30000 % 3 0.30 91.87000 % 4 0.40 89.68300 % 5 0.50 87.71470 % 6 0.60 85.94323 % 7 0.70 84.34891 % 8 0.80 82.91402 % 9 0.90 81.62261 % 10 1.00 80.46035 % 11 1.10 79.41432 % 12 1.20 78.47289 % 13 1.30 77.62560 % 14 1.40 76.86304 % 15 1.50 76.17673 % 16 1.60 75.55906 % 17 1.70 75.00315 % 18 1.80 74.50284 % 19 1.90 74.05256 % 20 2.00 73.64730 %---------------------------------------