close all; main.m step_num = 1000; [t_e,y_0_e,y_1_e] = euler_test ( 6, step_num ); [t,y_0,y_1] = adam_bashforth_2_test (6, step_num ); figure(1),test_ode_test04; figure(2), plot ( t_e, y_0_e, 'b-', 'Linewidth', 3 ),hold on; plot ( t_e, y_1_e, 'g-', 'Linewidth', 3 ); title ('Euler Method, 1000 step' ); grid on xlabel ( '<--- T --->' ); ylabel ( '<--- Y(T) --->' ); figure(3), plot ( t, y_0, 'b-', 'Linewidth', 3 ),hold on; plot ( t, y_1, 'g-', 'Linewidth', 3 ); title ('2nd order Adam Bashforth, 1000 step' ); grid on xlabel ( '<--- T --->' ); ylabel ( '<--- Y(T) --->' ); step_num = 2^5; error_e = zeros(7,2); error_a = zeros(7,2); step = zeros(7,1); %y_0_exact = y_0(end); %y_1_exact = y_1(end); y_0_exact = 3.144351304416428; y_1_exact = 0.348822692323950; for i = 1:10 [~,y_0_e,y_1_e] = euler_test ( 6, step_num ); [~,y_0,y_1] = adam_bashforth_2_test (6, step_num ); error_e(i,1) = y_0_e(end) - y_0_exact; error_e(i,2) = y_1_e(end) - y_1_exact; error_a(i,1) = y_0(end) - y_0_exact; error_a(i,2) = y_1(end) - y_1_exact; step(i) = step_num; step_num = step_num*2; end figure(4), plot(log10(step),log10(abs(error_e(:,1))),'b-', 'Linewidth', 3 ),hold on; plot(log10(step),log10(abs(error_e(:,2))),'g-', 'Linewidth', 3 ); title ('Euler Method, error' ); grid on xlabel ( '<--- log10(nSteps) --->' ); ylabel ( '<--- log10(error(end)) --->' ); figure(5), plot(log10(step),log10(abs(error_a(:,1))),'b-', 'Linewidth', 3 ),hold on; plot(log10(step),log10(abs(error_a(:,2))),'g-', 'Linewidth', 3 ) title ('2nd Order Adam Bashforth, error' ); grid on xlabel ( '<--- log10(nSteps) --->' ); ylabel ( '<--- log10(error(end)) --->' );
function y2 = p00_adam_bashforth_2_step ( test, neqn, t0, y0, t1, y1, t2 ) %*****************************************************************************80 % % % Parameters: % % Input, integer TEST, the problem number. % % Input, integer NEQN, the number of equations. % % Input, real T0, Y0(NEQN), the arguments of the derivative function. % % Input, real T1, the point at which an estimate of the solution % is desired. % % Output, real Y1(NEQN), the estimated solution at T1. % dt = t1 - t0; yp0 = p00_fun ( test, neqn, t0, y0 ); yp1 = p00_fun ( test, neqn, t1, y1 ); y2(1:neqn,1) = y1(1:neqn,1) + dt * (1.5*yp1(1:neqn,1)-0.5*yp0(1:neqn,1)); return end
function [t,y_0,y_1] = adam_bashforth_2_test ( test, step_num ) % % Adam_Bashforth_2_test uses 2nd order Adam_Bashforth Method on a test problem. % if ( nargin < 1 ) test = 1; fprintf ( 1, '\n' ); fprintf ( 1, ' Since the user did not choose a test,\n' ); fprintf ( 1, ' the default test %d was chosen.\n', test ); end if ( nargin < 2 ) step_num = 25; fprintf ( 1, '\n' ); fprintf ( 1, ' Since the user did not choose a number of steps,\n' ); fprintf ( 1, ' the default value %d was chosen.\n', step_num ); end fprintf ( 1, '\n' ); fprintf ( 1, 'Adam_Bashforth_2_test\n' ); fprintf ( 1, ' Problem number = %d\n', test ); title = p00_title ( test ); fprintf ( 1, ' %s\n', title ); % % Initialize any problem parameters. % value = p00_param_default ( test ); if ( 0 < value ) fprintf ( 1, ' This problem has %d parameters.\n', value ); end % % Autonomous? % if ( p00_autonomous ( test ) ) fprintf ( 1, ' The system is autonomous.\n' ); else fprintf ( 1, ' The system is not autonomous.\n' ); end % % Get the number of equations. % neqn = p00_neqn ( test ); fprintf ( 1, ' Number of equations is %d\n', neqn ); if ( 8 < neqn ) fprintf ( 1, ' The system is large.\n' ); fprintf ( 1, ' Print only MIN, AVERAGE, MAX and L2NORM.\n' ); end % % Get the starting point. % [ t_start, y_start ] = p00_start ( test, neqn ); % % Get the stopping point. % [ t_stop, y_stop ] = p00_stop ( test, neqn ); % % Print the stepsize; % fprintf ( 1, ' Stepsize H = %f\n', ( t_stop - t_start ) / step_num ); fprintf ( 1, '\n' ); if ( neqn <= 4 ) fprintf ( 1, ' %12f', t_start ); for i = 1 : neqn fprintf ( 1, ' %12f', y_start(i) ); end fprintf ( 1, '\n' ); elseif ( neqn <= 8 ) fprintf ( 1, ' %12f', t_start ); for i = 1 : 4 fprintf ( 1, ' %12f', y_start(i) ); end fprintf ( 1, '\n' ); fprintf ( 1, ' ' ); for i = 5 : neqn fprintf ( 1, ' %12f', y_start(i) ); end fprintf ( 1, '\n' ); else y_min = min ( y_start(1:neqn) ); y_ave = sum ( y_start(1:neqn) ) / neqn; y_max = max ( y_start(1:neqn) ); y_norm = sqrt ( sum ( y_start(1:neqn).^2 ) ); fprintf ( 1, ' %12f %12f %12f %12f %12f\n', ... t_start, y_min, y_ave, y_max, y_norm ); end y0(1:neqn,1) = y_start(1:neqn,1); y1(1:neqn,1) = y_start(1:neqn,1); % store y(t) for plot t = zeros(step_num,1); y_0 = zeros(step_num,1); y_1 = zeros(step_num,1); y_0(1) = y0(1,1); y_1(1) = y0(2,1); for step = 1 : step_num t0 = ( ( step_num - step + 2 ) * t_start ... + ( step - 2 ) * t_stop ) ... / ( step_num ); t1 = ( ( step_num - step + 1 ) * t_start ... + ( step - 1 ) * t_stop ) ... / ( step_num ); t2 = ( ( step_num - step ) * t_start ... + ( step ) * t_stop ) ... / ( step_num ); y2 = p00_adam_bashforth_2_step ( test, neqn, t0, y0, t1,y1,t2); if ( mod ( 10 * step, step_num ) == 0 || step == step_num ) if ( neqn <= 4 ) fprintf ( 1, ' %12f', t2 ); for i = 1 : neqn fprintf ( 1, ' %12f', y2(i) ); end fprintf ( 1, '\n' ); elseif ( neqn <= 8 ) fprintf ( 1, ' %12f', t2 ); for i = 1 : 4 fprintf ( 1, ' %12f', y2(i) ); end fprintf ( 1, '\n' ); fprintf ( 1, ' ' ); for i = 5 : neqn fprintf ( 1, ' %12f', y2(i) ); end fprintf ( 1, '\n' ); else y_min = min ( y2(1:neqn) ); y_ave = sum ( y2(1:neqn) ) / neqn; y_max = max ( y2(1:neqn) ); y_norm = sqrt ( sum ( y2(1:neqn).^2 ) ); fprintf ( 1, ' %12f %12f %12f %12f %12f\n', ... t1, y_min, y_ave, y_max, y_norm ); end end y0(1:neqn,1) = y1(1:neqn,1); y1(1:neqn,1) = y2(1:neqn,1); t(step) = t2; y_0(step) = y2(1,1); y_1(step) = y2(2,1); end fprintf ( 1, '\n' ); fprintf ( 1, ' Expected final conditions:\n' ); fprintf ( 1, '\n' ); if ( neqn <= 4 ) fprintf ( 1, ' %12f', t_stop ); for i = 1 : neqn fprintf ( 1, ' %12f', y_stop(i) ); end fprintf ( 1, '\n' ); elseif ( neqn <= 8 ) fprintf ( 1, ' %12f', t_stop ); for i = 1 : 4 fprintf ( 1, ' %12f', y_stop(i) ); end fprintf ( 1, '\n' ); fprintf ( 1, ' ' ); for i = 5 : neqn fprintf ( 1, ' %12f', y_stop(i) ); end fprintf ( 1, '\n' ); else y_min = min ( y_stop(1:neqn) ); y_ave = sum ( y_stop(1:neqn) ) / neqn; y_max = max ( y_stop(1:neqn) ); y_norm = sqrt ( sum ( y_stop(1:neqn).^2 ) ); fprintf ( 1, ' %12f %12f %12f %12f %12f\n', ... t_stop, y_min, y_ave, y_max, y_norm ); end return end
Last modified: