You are on page 1of 10

two-step Adams-Bashfroth method

function [wi, ti] = ab2 ( RHS, t0, x0, tf, N ) %AB2 approximate the solution of the initial value problem % % x'(t) = RHS( t, x ), x(t0) = x0 % % using the second-order Adams-Bashforth method - this routine % will work for a system of first-order equations as well as % for a single equation % % NOTE: the optimal RK2 method is used to initialize the % multistep method % % % calling sequences: % [wi, ti] = ab2 ( RHS, t0, x0, tf, N ) % ab2 ( RHS, t0, x0, tf, N ) % % inputs: % RHS string containing name of m-file defining the % right-hand side of the differential equation; the % m-file must take two inputs - first, the value of % the independent variable; second, the value of the % dependent variable % t0 initial value of the independent variable % x0 initial value of the dependent variable(s) % if solving a system of equations, this should be a % row vector containing all initial values % tf final value of the independent variable % N number of uniformly sized time steps to be taken to % advance the solution from t = t0 to t = tf % % output: % wi vector / matrix containing values of the approximate % solution to the differential equation % ti vector containing the values of the independent % variable at which an approximate solution has been % obtained % neqn = length ti = linspace wi = [ zeros( wi(1:neqn, 1) ( x0 ); ( t0, tf, N+1 ); neqn, N+1 ) ]; = x0';

h = ( tf - t0 ) / N; % % % generate w1 using the optimal RK2 method

fold = feval ( RHS, t0, x0 ); xtilde = x0 + (2*h/3) * fold;

x0 = x0 + (h/4) * fold + (3*h/4) * feval ( RHS, t0 + (2*h/3), xtilde ); t0 = t0 + h; wi(1:neqn,2) = x0'; % % %

perform remaining time steps using 2-step Adams Bashforth method

for i = 2:N fnew = feval ( RHS, t0, x0 ); x0 = x0 + (3*h/2) * fnew - h/2 * fold; t0 = t0 + h; fold = fnew; wi(1:neqn,i+1) = x0'; end;

funcin [wi, ti] = AB2 (RHS, t0, x0, tf, N) % AB2 aproximar la solucin del problema de valor inicial % % X '(t) = RHS (t, x), x (t0) = x0 % % Con el de segundo orden, el mtodo de Adams-Bashforth - esta rutina % Va a trabajar para un sistema de ecuaciones de primer orden, as como % Para una sola ecuacin % NOTA%: el ptimo RK2 mtodo se utiliza para inicializar el % Mtodo multipaso % % Secuencias% de llamadas: % [Wi, ti] = AB2 (RHS, t0, x0, tf, N) AB2% (RHS, t0, x0, tf, N) % Entradas%: % Nombre de la cadena que contiene RHS de archivo-m que define el % Del lado derecho de la ecuacin diferencial, el % M-archivo debe tener dos entradas - en primer lugar, el valor de % De la variable independiente; segundo, el valor del % Variable dependiente % T0 valor inicial de la variable independiente

% X0 valor inicial de la variable dependiente (s) % Si la solucin de un sistema de ecuaciones, esto debe ser una % Vector fila que contiene todos los valores iniciales TF% valor final de la variable independiente % De N nmero de veces de tamao uniforme los pasos que deben adoptarse para % Avanzar en la solucin de t = t0 a t = tf % % De salida: Wi% vector / matriz de los valores que contienen aproximada de la % De solucin de la ecuacin diferencial % De Ti vector que contiene los valores de la independiente % Variable en el que una solucin aproximada ha sido % Obtenida % neqn = longitud (x 0); ti = linspace (t0, tf, N +1); wi = [ceros (neqn, N +1)]; wi (1: neqn, 1) = x 0 '; h = (tf - t0) / N; % % Generar w1 utilizando el mtodo ptimo RK2 % veces = feval (RHS, t0, x0); xtilde = x0 + (2 * h / 3) * redil; x0 = x0 + (h / 4) * veces + (3 * h / 4) * feval (RHS, t0 + (2 * h / 3), xtilde); t0 = t0 + h; wi (1: neqn, 2) = x 0 ';

% % Realizan el resto de pasos de tiempo utilizando 2-step Adams Bashforth mtodo % para i = 2: N fnew = feval (RHS, t0, x0); x0 = x0 + (3 * h / 2) * fnew - h / 2 * redil; t0 = t0 + h; veces = fnew; wi (1: neqn, i +1) = x 0 '; final;

four-step Adams-Bashforth method

function [wi, ti] = ab4 ( RHS, t0, x0, tf, N ) %AB4 approximate the solution of the initial value problem % % x'(t) = RHS( t, x ), x(t0) = x0 % % using the fourth-order Adams-Bashforth method % - this routine will work for a system of first-order % equations as well as for a single equation % % the classical fourth-order Runge-Kutta method is used to % initialize the multistep method % % % calling sequences: % [wi, ti] = ab4 ( RHS, t0, x0, tf, N ) % ab4 ( RHS, t0, x0, tf, N ) % % inputs: % RHS string containing name of m-file defining the % right-hand side of the differential equation; the % m-file must take two inputs - first, the value of % the independent variable; second, the value of the % dependent variable % t0 initial value of the independent variable % x0 initial value of the dependent variable(s) % if solving a system of equations, this should be a % row vector containing all initial values % tf final value of the independent variable % N number of uniformly sized time steps to be taken to % advance the solution from t = t0 to t = tf % % output: % wi vector / matrix containing values of the approximate % solution to the differential equation % ti vector containing the values of the independent % variable at which an approximate solution has been % obtained % neqn = length ti = linspace wi = [ zeros( wi(1:neqn, 1) ( x0 ); ( t0, tf, N+1 ); neqn, N+1 ) ]; = x0';

h = ( tf - t0 ) / N; oldf = zeros(3,neqn); % % % generate starting values using classical 4th order RK method remember to save function values

% for i = 1:3 oldf(i,1:neqn) = feval ( RHS, t0, x0 ); k1 = h * oldf(i,:); k2 = h * feval ( RHS, t0 + h/2, x0 + k1/2 ); k3 = h * feval ( RHS, t0 + h/2, x0 + k2/2 ); k4 = h * feval ( RHS, t0 + h, x0 + k3 ); x0 = x0 + ( k1 + 2*k2 + 2*k3 + k4 ) / 6; t0 = t0 + h; wi(1:neqn,i+1) = x0'; end; % % % continue time stepping with 4th order Adams Predictor / Corrector

for i = 4:N fnew = feval ( RHS, t0, x0 ); x0 = x0 + (h/24) * ( 55*fnew - 59*oldf(3,:) + 37*oldf(2,:) 9*oldf(1,:) ); oldf(1,1:neqn) = oldf(2,1:neqn); oldf(2,1:neqn) = oldf(3,1:neqn); oldf(3,1:neqn) = fnew; t0 = t0 + h; wi(1:neqn,i+1) = x0'; end;

funcin [wi, ti] = AB4 (RHS, t0, x0, tf, N) % AB4 aproximar la solucin del problema de valor inicial % % X '(t) = RHS (t, x), x (t0) = x0 % % Con el cuarto orden de Adams-Bashforth mtodo % - Esta rutina va a trabajar para un sistema de primer orden Ecuaciones%, as como para una sola ecuacin % % El mtodo clsico de cuarto orden de Runge-Kutta se utiliza para % Inicializar el mtodo multipaso % % Secuencias% de llamadas: % [Wi, ti] = AB4 (RHS, t0, x0, tf, N) AB4% (RHS, t0, x0, tf, N) % Entradas%:

% Nombre de la cadena que contiene RHS de archivo-m que define el % Del lado derecho de la ecuacin diferencial, el % M-archivo debe tener dos entradas - en primer lugar, el valor de % De la variable independiente; segundo, el valor del % Variable dependiente % T0 valor inicial de la variable independiente % X0 valor inicial de la variable dependiente (s) % Si la solucin de un sistema de ecuaciones, esto debe ser una % Vector fila que contiene todos los valores iniciales TF% valor final de la variable independiente % De N nmero de veces de tamao uniforme los pasos que deben adoptarse para % Avanzar en la solucin de t = t0 a t = tf % % De salida: Wi% vector / matriz de los valores que contienen aproximada de la % De solucin de la ecuacin diferencial % De Ti vector que contiene los valores de la independiente % Variable en el que una solucin aproximada ha sido % Obtenida % neqn = longitud (x 0); ti = linspace (t0, tf, N +1); wi = [ceros (neqn, N +1)]; wi (1: neqn, 1) = x 0 '; h = (tf - t0) / N; oldf = zeros (3, neqn); % % Generar valores iniciales utilizando el mtodo clsico de cuarto orden de RK % Recuerde guardar valores de la funcin % para i = 1:3 oldf (i, 1: neqn) = feval (RHS, t0, x0); k1 = h * oldf (i, :); k2 = h * feval (RHS, t0 + h / 2, x0 + k1 / 2); k3 = h * feval (RHS, t0 + h / 2, x0 + k2 / 2); k4 = h * feval (RHS, t0 + h, x0 + k3); x0 = x0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6; t0 = t0 + h; wi (1: neqn, i +1) = x 0 '; final;

% % Tiempo de continuar con la intensificacin de 4 orden Predictor Adams / Corrector % para i = 4: N fnew = feval (RHS, t0, x0); x0 = x0 + (h/24) * (55 * fnew - 59 * oldf (3, :) + 37 * oldf (2, :) - 9 * oldf (1, :)); oldf (1,1: neqn) = oldf (2,1: neqn); oldf (2,1: neqn) = oldf (3,1: neqn); oldf (3,1: neqn) = fnew; t0 = t0 + h; wi (1: neqn, i +1) = x 0 '; final;

fourth-order Adams Predictor-Corrector method


function [wi, ti] = adams_pc4 ( RHS, t0, x0, tf, N ) %ADAMS_PC4 approximate the solution of the initial value problem % % x'(t) = RHS( t, x ), x(t0) = x0 % % using the Adams fourth-order predictor / corrector scheme % - this routine will work for a system of first-order % equations as well as for a single equation % % the classical fourth-order Runge-Kutta method is used to % initialize the predictor / corrector scheme % % % calling sequences: % [wi, ti] = adams_pc4 ( RHS, t0, x0, tf, N ) % adams_pc4 ( RHS, t0, x0, tf, N ) % % inputs: % RHS string containing name of m-file defining the % right-hand side of the differential equation; the % m-file must take two inputs - first, the value of % the independent variable; second, the value of the % dependent variable % t0 initial value of the independent variable % x0 initial value of the dependent variable(s) % if solving a system of equations, this should be a % row vector containing all initial values

% % % % % output: % approximate % % % % % neqn = length ti = linspace wi = [ zeros( wi(1:neqn, 1)

tf N

final value of the independent variable number of uniformly sized time steps to be taken to advance the solution from t = t0 to t = tf vector / matrix containing values of the solution to the differential equation vector containing the values of the independent variable at which an approximate solution has been obtained

wi ti

( x0 ); ( t0, tf, N+1 ); neqn, N+1 ) ]; = x0';

h = ( tf - t0 ) / N; oldf = zeros(3,neqn); % % % % generate starting values using classical 4th order RK method remember to save function values

for i = 1:3 oldf(i,1:neqn) = feval ( RHS, t0, x0 ); k1 = h * oldf(i,:); k2 = h * feval ( RHS, t0 + h/2, x0 + k1/2 ); k3 = h * feval ( RHS, t0 + h/2, x0 + k2/2 ); k4 = h * feval ( RHS, t0 + h, x0 + k3 ); x0 = x0 + ( k1 + 2*k2 + 2*k3 + k4 ) / 6; t0 = t0 + h; wi(1:neqn,i+1) = x0'; end; % % % continue time stepping with 4th order Adams Predictor / Corrector

for i = 4:N fnew = feval ( RHS, t0, x0 ); xtilde = x0 + (h/24) * ( 55*fnew - 59*oldf(3,:) + 37*oldf(2,:) 9*oldf(1,:) ); fnew1 = feval ( RHS, t0+h, xtilde ); x0 = x0 + (h/24) * ( 9*fnew1 + 19*fnew - 5*oldf(3,:) + oldf(2,:) ); oldf(1,1:neqn) = oldf(2,1:neqn); oldf(2,1:neqn) = oldf(3,1:neqn); oldf(3,1:neqn) = fnew; t0 = t0 + h; wi(1:neqn,i+1) = x0'; end;

funcin [wi, ti] = adams_pc4 (RHS, t0, x0, tf, N) % ADAMS_PC4 aproximar la solucin del problema de valor inicial % % X '(t) = RHS (t, x), x (t0) = x0 % % Con el Adams de cuarto orden predictor / corrector de esquema % - Esta rutina va a trabajar para un sistema de primer orden Ecuaciones%, as como para una sola ecuacin % % El mtodo clsico de cuarto orden de Runge-Kutta se utiliza para % Inicializar el sistema de prediccin / corrector % % Secuencias% de llamadas: % [Wi, ti] = adams_pc4 (RHS, t0, x0, tf, N) Adams_pc4% (RHS, t0, x0, tf, N) % Entradas%: % Nombre de la cadena que contiene RHS de archivo-m que define el % Del lado derecho de la ecuacin diferencial, el % M-archivo debe tener dos entradas - en primer lugar, el valor de % De la variable independiente; segundo, el valor del % Variable dependiente % T0 valor inicial de la variable independiente % X0 valor inicial de la variable dependiente (s) % Si la solucin de un sistema de ecuaciones, esto debe ser una % Vector fila que contiene todos los valores iniciales TF% valor final de la variable independiente % De N nmero de veces de tamao uniforme los pasos que deben adoptarse para % Avanzar en la solucin de t = t0 a t = tf % % De salida: Wi% vector / matriz de los valores que contienen aproximada de la % De solucin de la ecuacin diferencial % De Ti vector que contiene los valores de la independiente % Variable en el que una solucin aproximada ha sido % Obtenida % neqn = longitud (x 0); ti = linspace (t0, tf, N +1); wi = [ceros (neqn, N +1)]; wi (1: neqn, 1) = x 0 '; h = (tf - t0) / N;

oldf = zeros (3, neqn); % % Generar valores iniciales utilizando el mtodo clsico de cuarto orden de RK % Recuerde guardar valores de la funcin % para i = 1:3 oldf (i, 1: neqn) = feval (RHS, t0, x0); k1 = h * oldf (i, :); k2 = h * feval (RHS, t0 + h / 2, x0 + k1 / 2); k3 = h * feval (RHS, t0 + h / 2, x0 + k2 / 2); k4 = h * feval (RHS, t0 + h, x0 + k3); x0 = x0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6; t0 = t0 + h; wi (1: neqn, i +1) = x 0 '; final; % % Tiempo de continuar con la intensificacin de 4 orden Predictor Adams / Corrector % para i = 4: N fnew = feval (RHS, t0, x0); xtilde = x0 + (h/24) * (55 * fnew - 59 * oldf (3, :) + 37 * oldf (2, :) - 9 * oldf (1, :)); fnew1 = feval (RHS, t0 + h, xtilde); x0 = x0 + (h/24) * (9 * fnew1 + 19 * fnew - 5 * oldf (3, :) + oldf (2, :)); oldf (1,1: neqn) = oldf (2,1: neqn); oldf (2,1: neqn) = oldf (3,1: neqn); oldf (3,1: neqn) = fnew; t0 = t0 + h; wi (1: neqn, i +1) = x 0 '; final;

You might also like