Está en la página 1de 25

Códigos Capítulos 4 y 5.

Código para la derivada

clc; clear all;

fprintf('PROGRAMA PARA EFECTUAR LA DERIVACIÓN DE UN SET DE DATOS\n\n');

fprintf('El programa le brindará la 1ª,2ª,3ª y 4ª derivada\n\n');

fprintf('Ingrese datos de variable independiente:\n');

fprintf('(RECUERDE QUE ESTOS DATOS DEBEN SER REGULARMENTE ESPACIADOS\n');

x=input('Ej.[0 1 2 3 4 5 6 7 8 9 10])');

n=length(x);

e=0;

for i=1:n-3%este bucle verificará que el tamaño de paso sea el mismo

if (x(1,i+1)-x(1,i))~=(x(1,i+2)-x(1,i+1))

e=e+1;

break;

end

end

if e>0,error ('Los datos deben ser igualmente espaciados');end

fprintf('Ingrese datos de la variable dependiente: \n');

y=input('(Ej.[1.4 2.1 3.3 4.8 6.8 6.6 8.6 7.5 8.9 10.9 10])');

h=x(1,2)-x(1,1);

n=length(x);

pridrv=zeros(1,n);

%.................................--------...............................
.

%Realizamos la primera derivada de los datos ingresados por diferencias

%finitas.
%primer dato, diferencias finitas hacia adelante

pridrv(1,1)=(y(1,2)-y(1))/h;

%Desde el segundo dato hasta el penúltimo, Direncias finitas centradas

for i=2:n-1

pridrv(1,i)=(y(1,i+1)-y(1,i-1))/(2.*h);

end

%Para el último dato, diferencias finitas hacia atras

pridrv(1,n)=(y(1,n)-y(1,n-1))/h;

fprintf('\n\nRESULTADOS!\n');

fprintf('Las primeras derivadas para los datos ingresados son,


respecticamente :\n');

disp(pridrv);

%Ahora Desarrollaremos la segunda derivada

segdrv=zeros(1,n);

if n<3

fprintf('No se puede calcular la segunda derivada con solo 2 datos\n');

else

segdrv(1,1)=(y(1,3)-2.*y(1,2)+y(1,1))/h.^2;

for i=2:n-1

segdrv(1,i)=(y(1,i+1)-2.*y(1,i)+y(1,i-1))/h.^2;

end

segdrv(1,n)=(y(1,n)-2.*y(1,n-1)+y(1,n-2))/h.^2;

fprintf('Las segundas derivadas para los datos ingresados son,


respectivamente: \n');

disp(segdrv);

end

%Tercera derivada:
if n<5

if n<4

fprintf('No se puede hallar la tercera derivada con menos de 4 datos\n');

else

fprintf('Con cuatro datos solo podemos hallar terceras derivadas en los


extremos');

fprintf('\n');

exterdrv=zeros(1,2);

exterdrv(1,1)=(y(1,4)-3.*y(1,3)+3.*y(1,2)-y(1,1))/h.^3;

exterdrv(1,2)=(y(1,n)-3.*y(1,n-1)+3.*y(1,n-2)-y(1,n-3))/h.^3;

fprintf('Estas son: ');

disp(exterdrv);

end

else

terdrv=zeros(1,n);

terdrv(1,1)=(y(1,4)-3.*y(1,3)+3.*y(1,2)-y(1,1))/h.^3;

terdrv(1,2)=(y(1,5)-3.*y(1,4)+3.*y(1,3)-y(1,2))/h.^3;

for i=3:n-2

terdrv(1,i)=(y(1,i+2)-2.*y(1,i+1)+2.*y(1,i-1)-y(1,i-2))/(2*h.^3);

end

terdrv(1,n-1)=(y(1,n-1)-3.*y(1,n-2)+3.*y(1,n-3)-y(1,n-4))/h.^3;

terdrv(1,n)=(y(1,n)-3.*y(1,n-1)+3.*y(1,n-2)-y(1,n-3))/h.^3;

fprintf('Las terceras derivadas para los datos ingresados son,


respectivamente: \n');

disp(terdrv);

end
%Cuarta derivada

if n<6

if n<5

fprintf('No se puede hallar la cuarta derivada con menos de 5 datos\n');

else

fprintf('Con cinco datos solo podemos hallar cuartas derivadas en los


extremos\n');

excuadrv(1,1)=(y(1,5)-4.*y(1,4)+6.*y(1,3)-4.*y(1,2)+y(1))/h.^4;

excuadrv(1,2)=(y(1,n)-4.*y(1,n-1)+6.*y(1,n-2)-4.*y(1,n-3)+y(n-4))/h.^4;

fprintf('Estas son:');

disp(excuadrv);

end

else

cuadrv=zeros(1,n);

cuadrv(1,1)=(y(1,5)-4.*y(1,4)+6.*y(1,3)-4.*y(1,2)+y(1))/h.^4;

cuadrv(1,2)=(y(1,6)-4.*y(1,5)+6.*y(1,4)-4.*y(1,3)+y(2))/h.^4;

for i=3:n-2

cuadrv(1,i)=(y(1,i+2)-4.*y(1,i+1)+6.*y(1,i)-4.*y(1,i-1)+y(1,i-2))/h.^4;

end

cuadrv(1,n)=(y(1,n)-4.*y(1,n-1)+6.*y(1,n-2)-4.*y(1,n-3)+y(n-4))/h.^4;

cuadrv(1,n-1)=(y(1,n-1)-4.*y(1,n-2)+6.*y(1,n-3)-4.*y(1,n-4)+y(n-5))/h.^4;

fprintf('Las cuartas derivadas para los datos ingresados son,


respectivamente: \n');

disp(cuadrv);

end

% SIMPSON'S COMPOSITE ALGORITHM 4.1


%
% To approximate I = integral ( ( f(x) dx ) ) from a to b:
%
% INPUT: endpoints a, b; even positive integer n.
%
% OUTPUT: approximation XI to I.
syms('OK','A','B','N','H','XI0','XI1','XI2','NN','I','X','XI','s','x');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is Simpsons Method.\n\n');
fprintf(1,'Input the function F(x) in terms of x\n');
fprintf(1,'For example: cos(x)\n');
s = input(' ');
F = inline(s,'x');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input lower limit of integration and ');
fprintf(1,'upper limit of integration\n');
fprintf(1,'on separate lines\n');
A = input(' ');
B = input(' ');
if A > B
fprintf(1,'Lower limit must be less than upper limit\n');
else
OK = TRUE;
end
end
OK = FALSE;
while OK == FALSE
fprintf(1,'Input an even positive integer N.\n');
N = input(' ');
if N > 0 & rem(N,2) == 0
OK = TRUE;
else
fprintf(1,'Input must be even and positive\n');
end
end
if OK == TRUE
% STEP 1
H = (B-A)/N;
% STEP 2
XI0 = F(A) + F(B);
% summation of f(x(2*I-1))
XI1 = 0.0;
% summation of f(x(2*I))
XI2 = 0.0;
% STEP 3
NN = N - 1;
for I = 1:NN
% STEP 4
X = A + I * H;
% STEP 5
if rem(I,2) == 0
XI2 = XI2 + F(X);
else
XI1 = XI1 + F(X);
end
end
% STEP 6
XI = (XI0 + 2.0 * XI2 + 4.0 * XI1) * H / 3.0;
% STEP 7
fprintf(1,'\nThe integral of F from %12.8f to %12.8f is\n', A, B);
fprintf(1,'%12.8f\n', XI);
end

INTEGRACIÓN CUANDO NO SE TIENE UNA FUNCIÓN PERO SI UN CONJUNTO DE PUNTOS.


% Programa para integrar usando interpolación cúbica por segmentos.

clear all

%Entrada de informacion

n=input('numero de datos=');

x=input('vector de abscisas=');

y=input('vector de ordenadas=');

W1=input('segunda derivada al principio=');

WN=input('segunda derivada al final=');

%Calculo de los tamaños de paso

for i=1:n-1

h(i)=x(i+1)-x(i);

end

%calculo de las segundas diferencias divididas

for k=1:n-2

b(k)=6*((y(k+2)-y(k+1))/h(k+1)-(y(k+1)-y(k))/h(k));

end

%Sistema de ecuaciones para hallar las segundas derivadas, desde w(2) hasta w(n-1)

% M*W=B

M(1,1)=2*(h(1)+h(2));

M(1,2)=h(2);

B(1)=b(1)-h(1)*W1;

M(n-2,n-3)=h(n-2);

M(n-2,n-2)=2*(h(n-2)+h(n-1));

B(n-2)=b(n-2)-h(n-1)*WN;

for k=2:n-3

M(k,k-1)=h(k);

M(k,k)=2*(h(k)+h(k+1));
M(k,k+1)=h(k+1);

B(k)=b(k);

end

aux=inv(M)*B';

W(1)=W1;

W(n)=WN;

for j=2:n-1;

W(j)=aux(j-1);

end

%Calculo de las primeras derivadas

for k=1:n-1

U(k)=(y(k+1)-y(k))/h(k)-(h(k)/6)*(2*W(k)+W(k+1));

end

% Calculo de los coeficientes de los polinomios

for k=1:n-1

c(k,1)=(W(k+1)-W(k))/(6*h(k));

c(k,2)=W(k)/2-3*x(k)*c(k,1);

c(k,3)=U(k)-x(k)*W(k)+3*c(k,1)*x(k)^2;

c(k,4)=-c(k,1)*x(k)^3+(x(k)^2)*W(k)/2-x(k)*U(k)+y(k);

end

%Representacion grafica de los polinomios

for i=1:n-1

t(i,:)=x(i):0.01*h(i):x(i+1);

p(i,:)=c(i,1).*(t(i,:).^3)+c(i,2).*(t(i,:).^2)+c(i,3).*t(i,:)+c(i,4);

plot(t(i,:),p(i,:),'k')

grid on

hold on

end

plot (x,y,'o')

for j=1:n-1

for i=1:4

s(i)=c(j,i);
end

disp(strcat('p',num2str(j),'=',poly2str(s,'t')));

end

%Rutina para integrar

I=0;

for i=1:n-1

I=I+c(i,1)*(x(i+1)^4-x(i)^4)/4+c(i,2)*(x(i+1)^3-x(i)^3)/3+c(i,3)*(x(i+1)^2-x(i)^2)/2+c(i,4)*(x(i+1)-x(i));

end

% ADAPTIVE QUADRATURE ALGORITM 4.3 %


% To approximate I = integral ( ( f(x) dx ) ) from a to b to within
% a given tolerance TOL:
%
% INPUT: endpoints a, b; tolerance TOL; limit N to number of levels
%
% OUTPUT: approximation APP or message that N is exceeded.
syms('OK','AA','BB','EPS','N','CNT','APP','I','TOL','A','H','FA');
syms('FC','FB','S','L','FD','FE','S1','S2','V','LEV','s','x');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is Adaptive Quadrature with Simpsons Method.\n\n');
fprintf(1,'Input the function F(x) in terms of x\n');
fprintf(1,'For example: cos(x)\n');
s = input(' ');
F = inline(s,'x');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input lower limit of integration and ');
fprintf(1,'upper limit of integration\n');
fprintf(1,'on separate lines\n');
AA = input(' ');
BB = input(' ');
if AA > BB
fprintf(1,'Lower limit must be less than upper limit\n');
else
OK = TRUE;
end
end
OK = FALSE;
while OK == FALSE
fprintf(1,'Input tolerance.\n');
EPS = input(' ');
if EPS > 0
OK = TRUE;
else
fprintf(1,'Tolerance must be positive.\n');
end
end
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the maximum number of levels.\n');
N = input(' ');
if N > 0
OK = TRUE;
else
fprintf(1,'Number must be positive\n');
end
end
if OK == TRUE
CNT = 0;
OK = TRUE;
% STEP 1
APP = 0;
I = 1;
TOL = zeros(1,N);
A = zeros(1,N);
H = zeros(1,N);
FA = zeros(1,N);
FC = zeros(1,N);
FB = zeros(1,N);
S = zeros(1,N);
L = zeros(1,N);
FD = zeros(1,N);
FE = zeros(1,N);
V = zeros(1,7);
TOL(I) = 10*EPS;
A(I) = AA;
H(I) = 0.5*(BB-AA);
FA(I) = F(AA);
CNT = CNT+1;
FC(I) = F((AA+H(I)));
CNT = CNT+1;
FB(I) = F(BB);
CNT = CNT+1;
% Approximation from Simpson's method for entire interval.
S(I) = H(I)*(FA(I)+4*FC(I)+FB(I))/3;
L(I) = 1;
% STEP 2
while I > 0 & OK == TRUE
% STEP 3
FD = F((A(I)+0.5*H(I)));
CNT = CNT+1;
FE = F((A(I)+1.5*H(I)));
CNT = CNT+1;
% Approximations from Simpson's method for halves of intervals
S1 = H(I)*(FA(I)+4*FD+FC(I))/6;
S2 = H(I)*(FC(I)+4*FE+FB(I))/6;
% Save data at this level
V(1) = A(I);
V(2) = FA(I);
V(3) = FC(I);
V(4) = FB(I);
V(5) = H(I);
V(6) = TOL(I);
V(7) = S(I);
LEV = L(I);
% STEP 4
% Delete the level
I = I-1;
% STEP 5
if abs(S1+S2-V(7)) < V(6)
APP = APP+(S1+S2);
else
if LEV >= N
OK = FALSE;
% procedure fails
else
% Add one level
% Data for right half subinterval
I = I+1;
A(I) = V(1)+V(5);
FA(I) = V(3);
FC(I) = FE;
FB(I) = V(4);
H(I) = 0.5*V(5);
TOL(I) = 0.5*V(6);
S(I) = S2;
L(I) = LEV+1;
% Data for left half subinterval
I = I+1;
A(I) = V(1);
FA(I) = V(2);
FC(I) = FD;
FB(I) = V(3);
H(I) = H(I-1);
TOL(I) = TOL(I-1);
S(I) = S1;
L(I) = L(I-1);
end
end
end
if OK == FALSE
fprintf(1,'Level exceeded. Method did not produce an\n');
fprintf(1,'accurate approximation.\n');
else
fprintf(1,'\nThe integral of F from %12.8f to %12.8f is\n', AA, BB);
fprintf(1,'%12.8f to within %14.8e\n', APP, EPS);
fprintf(1,'The number of function evaluations is: %d\n', CNT);
end
end

% DOUBLE INTEGAL ALGORITHM 4.4


%
% To approximate I = double integral ( ( f(x,y) dy dx ) ) with limits
% of integration from a to b for x and from c(x) to d(x) for y:
%
% INPUT: endpoints a, b; even positive integers m, n.
%
% OUTPUT: approximation J to I.
syms('OK', 'A', 'B', 'N', 'M', 'NN', 'MM', 'H', 'AN', 'AE', 'AO');
syms('I', 'X', 'YA', 'YB', 'HX', 'BN', 'BE', 'BO', 'J', 'Y', 'Z');
syms('A1', 'AC','x','s','y');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is Simpsons Method for double integrals.\n\n');
fprintf(1,'Input the functions F(X,Y), C(X), and D(X) in terms of x\n');
fprintf(1,'and y on separate lines.\n');
fprintf(1,'For example: cos(x+y)\n');
fprintf(1,' x^3 \n');
fprintf(1,' x \n');
s = input(' ');
F = inline(s,'x','y');
s = input(' ');
C = inline(s,'x');
s = input(' ');
D = inline(s,'x');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input lower limit of integration and ');
fprintf(1,'upper limit of integration\n');
fprintf(1,'on separate lines\n');
A = input(' ');
B = input(' ');
if A > B
fprintf(1,'Lower limit must be less than upper limit\n');
else
OK = TRUE;
end
end
OK = FALSE;
while OK == FALSE
fprintf(1,'Input two even positive integer N and M.\n');
fprintf(1,'N corresponds to the outer integral and M \n');
fprintf(1,'corresponds to the inner integral. Place \n');
fprintf(1,'on separate lines.\n');
N = input(' ');
M = input(' ');
if N > 0 & rem(N,2) == 0 & M > 0 & rem(M,2) == 0
OK = TRUE;
else
fprintf(1,'N and M must both be even and positive\n');
end
end
if OK == TRUE
% STEP 1
H = (B-A)/N;
% use AN, AE, AO, for J(1), J(2), J(3) resp.
% end terms
AN = 0;
% even terms
AE = 0;
% odd terms
AO = 0;
% STEP 2
for I = 0:N
% STEP 3
% Composite Simpson's Method for X
X = A+I*H;
YA = C(X);
YB = D(X);
HX = (YB-YA)/(M);
% use BN, BE, BO for K(1), K(2), K(3) resp.
% end terms
BN = F(X,YA)+F(X,YB);
% even terms
BE = 0;
% odd terms
BO = 0;
% STEP 4
for J = 1:M-1
% STEP 5
Y = YA+J*HX;
Z = F(X, Y);
% STEP 6
if rem(J,2) == 0
BE = BE+Z;
else
BO = BO+Z;
end
end
% STEP 7
% use A1 for L, which is the integral of F(X(I), Y) from
% C(X(I)) to D(X(I)) by Composite Simpson's Method
A1 = (BN+2*BE+4*BO)*HX/3;
% STEP 8
if I == 0 | I == N
AN = AN+A1;
else
if rem(I,2) == 0
AE = AE + A1;
else
AO = AO + A1;
end
end
end
% STEP 9
% Use AC for J
AC = (AN + 2 * AE + 4 * AO) * H /3;
% STEP 10
fprintf(1,'\nThe double integral of F from %12.8f to %12.8f is\n', A,
B);
fprintf(1,'%12.8f', AC);
fprintf(1,' obtained with N = %3d and M = %3d\n', N, M);
end
% EULER'S ALGORITHM 5.1
%
% TO APPROXIMATE THE SOLUTION OF THE INITIAL VALUE PROBLEM:
% Y' = F(T,Y), A<=T<=B, Y(A) = ALPHA,
% AT N+1 EQUALLY SPACED POINTS IN THE INTERVAL [A,B].
%
% INPUT: ENDPOINTS A,B; INITIAL CONDITION ALPHA; INTEGER N.
%
% OUTPUT: APPROXIMATION W TO Y AT THE (N+1) VALUES OF T.
syms('F','OK','A','B','ALPHA','N','FLAG','NAME','OUP','H');
syms('T','W','I','x','s');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is Eulers Method.\n');
fprintf(1,'Input the function F(t,y) in terms of t and y\n');
fprintf(1,'For example: y-t^2+1\n');
s = input(' ');
F = inline(s,'t','y');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input left and right endpoints on separate lines.\n');
A = input(' ');
B = input(' ');
if A >= B
fprintf(1,'Left endpoint must be less than right endpoint\n');
else
OK = TRUE;
end;
end;
fprintf(1,'Input the initial condition\n');
ALPHA = input(' ');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input a positive integer for the number of subintervals\n');
N = input(' ');
if N <= 0
fprintf(1,'Number must be a positive integer\n');
else
OK = TRUE;
end;
end;
if OK == TRUE
fprintf(1,'Choice of output method:\n');
fprintf(1,'1. Output to screen\n');
fprintf(1,'2. Output to text file\n');
fprintf(1,'Please enter 1 or 2\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\name.ext\n');
fprintf(1,'For example A:\\OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP, 'EULERS METHOD\n\n');
fprintf(OUP, ' t w\n\n');
% STEP 1
H = (B-A)/N;
T = A;
W = ALPHA;
fprintf(OUP, '%5.3f %11.7f\n', T, W);
% STEP 2
for I = 1:N
% STEP 3
% Compute W(I)
W = W+H*F(T, W);
% Compute T(I)
T = A+I*H;
% STEP 4
fprintf(OUP, '%5.3f %11.7f\n', T, W);
end;
% STEP 5
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully \n',NAME);
end;
end;

% RUNGE-KUTTA (ORDER 4) ALGORITHM 5.2


%
% TO APPROXIMATE THE SOLUTION TO THE INITIAL VALUE PROBLEM:
% Y' = F(T,Y), A<=T<=B, Y(A) = ALPHA,
% AT (N+1) EQUALLY SPACED NUMBERS IN THE INTERVAL [A,B].
%
% INPUT: ENDPOINTS A,B; INITIAL CONDITION ALPHA; INTEGER N.
%
% OUTPUT: APPROXIMATION W TO Y AT THE (N+1) VALUES OF T.
syms('F', 'OK', 'A', 'B', 'ALPHA', 'N', 'FLAG', 'NAME', 'OUP');
syms('H', 'T', 'W', 'I', 'K1', 'K2', 'K3', 'K4','t','d');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is the Runge-Kutta Order Four Method.\n');
fprintf(1,'Input the function F(t,y) in terms of t and y\n');
fprintf(1,'For example: y-t^2+1 \n');
s = input(' ');
F = inline(s,'t','y');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input left and right endpoints on separate lines.\n');
A = input(' ');
B = input(' ');
if A >= B
fprintf(1,'Left endpoint must be less than right endpoint\n');
else
OK = TRUE;
end;
end;
fprintf(1,'Input the initial condition\n');
ALPHA = input(' ');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input a positive integer for the number of subintervals\n');
N = input(' ');
if N <= 0
fprintf(1,'Number must be a positive integer\n');
else
OK = TRUE;
end;
end;
if OK == TRUE
fprintf(1,'Choice of output method:\n');
fprintf(1,'1. Output to screen\n');
fprintf(1,'2. Output to text file\n');
fprintf(1,'Please enter 1 or 2\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\name.ext\n');
fprintf(1,'For example A:\\OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP, 'RUNGE-KUTTA FOURTH ORDER METHOD\n\n');
fprintf(OUP, ' t w\n\n');
% STEP 1
H = (B-A)/N;
T = A;
W = ALPHA;
fprintf(OUP, '%5.3f %11.7f\n', T, W);
% STEP 2
for I = 1:N
% STEP 3
% use K1, K2, K3, K4 for K(1), K(2), K(3), K(4) RESP.
K1 = H*F(T,W);
K2 = H*F(T+H/2.0, W+K1/2.0);
K3 = H*F(T+H/2.0, W+K2/2.0);
K4 = H*F(T+H,W+K3);
% STEP 4
% compute W(I)
W = W+(K1+2.0*(K2+K3)+K4)/6.0;
% compute T(I)
T = A+I*H;
% STEP 5
fprintf(OUP, '%5.3f %11.7f\n', T, W);
end;
% STEP 6
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully. \n',NAME);
end;
end;
% ADAMS-FOURTH ORDER PREDICTOR-CORRECTOR ALGORITHM 5.4
%
% To approximate the solution of the initial value problem
% y' = f(t,y), a <= t <= b, y(a) = alpha,
% at N+1 equally spaced points in the interval [a,b].
%
% INPUT: endpoints a,b; initial condition alpha; integer N.
%
% OUTPUT: approximation w to y at the (N+1) values of t.
syms('F', 'OK', 'A', 'B', 'ALPHA', 'N', 'FLAG', 'NAME', 'OUP');
syms('H', 'T', 'W', 'I', 'K1', 'K2', 'K3', 'K4', 'T0', 'W0', 'J');
syms('t','y', 's','Part1','Part2');
TRUE = 1;
FALSE = 0;
T = zeros(1,4);
W = zeros(1,4);
fprintf(1,'This is Adams-Bashforth Predictor Corrector Method\n');
fprintf(1,'Input the function F(t,y) in terms of t and y\n');
fprintf(1,'For example: y-t^2+1 \n');
s = input(' ');
F = inline(s,'t','y');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input left and right endpoints on separate lines.\n');
A = input(' ');
B = input(' ');
if A >= B
fprintf(1,'Left endpoint must be less than right endpoint\n');
else
OK = TRUE;
end;
end;
fprintf(1,'Input the initial condition\n');
ALPHA = input(' ');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input an integer > 3 for the number of subintervals\n');
N = input(' ');
if N <= 3
fprintf(1,'Number must be at least 4.\n');
else
OK = TRUE;
end;
end;
if OK == TRUE
fprintf(1,'Choice of output method:\n');
fprintf(1,'1. Output to screen\n');
fprintf(1,'2. Output to text file\n');
fprintf(1,'Please enter 1 or 2\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\name.ext\n');
fprintf(1,'For example A:\\OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP, 'ADAMS-BASHFORTH FOURTH ORDER PREDICTOR CORRECTOR
METHOD\n\n');
fprintf(OUP, ' t w\n');
% STEP 1
H = (B-A)/N;
T(1) = A;
W(1) = ALPHA;
fprintf(OUP, '%5.3f %11.7f\n', T(1), W(1));
% STEP 2
for I = 1:3
% STEP 3 AND 4
% compute starting values using Runge-Kutta method
T(I+1) = T(I)+H;
K1 = H*F(T(I), W(I));
K2 = H*F(T(I)+0.5*H, W(I)+0.5*K1);
K3 = H*F(T(I)+0.5*H, W(I)+0.5*K2);
K4 = H*F(T(I+1), W(I)+K3);
W(I+1) = W(I)+(K1+2.0*(K2+K3)+K4)/6.0;
% STEP 5
fprintf(OUP, '%5.3f %11.7f\n', T(I+1), W(I+1));
end;
% STEP 6
for I = 4:N
% STEP 7
% T0, W0 will be used in place of t, w resp.
T0 = A+I*H;
% predict W(I)
Part1 = 55.0*F(T(4),W(4))-59.0*F(T(3),W(3))+37.0*F(T(2),W(2));
Part2 = -9.0*F(T(1),W(1));
W0 = W(4)+H*(Part1+Part2)/24.0;
% correct W(I)
Part1 = 9.0*F(T0,W0)+19.0*F(T(4),W(4))-5.0*F(T(3),W(3))+F(T(2),W(2));
W0 = W(4)+H*(Part1)/24.0;
% STEP 8
fprintf(OUP, '%5.3f %11.7f\n', T0, W0);
% STEP 9
% prepare for next iteration
for J = 1:3
T(J) = T(J+1);
W(J) = W(J+1);
end;
% STEP 10
T(4) = T0;
W(4) = W0;
end;
end;
% STEP 11
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully \n',NAME);
end;
% ADAMS VARIABLE STEP-SIZE PREDICTOR-CORRECTOR ALGORITHM 5.5
%
% To approximate the solution of the initial value problem
% y' = f( t, y ), a <= t <= b, y(a) = ALPHA,
%
% with local truncation error within a given tolerance:
%
% INPUT: endpoints a, b; initial condition ALPHA; tolerance TOL;
% maximum step size HMAX; minimum step size HMIN.
%
% OUTPUT: I, T(I), W(I), H where at the Ith step W(I) approximates
% y(T(I)) and step size H was used or a message that the
% minimum step size was exceeded.
syms('OK', 'A', 'B', 'ALPHA', 'TOL', 'HMIN', 'HMAX');
syms('FLAG', 'NAME', 'OUP', 'DONE', 'KK', 'NFLAG');
syms('I', 'WP', 'WC', 'SIG', 'K', 'J', 'Q');
syms('W','T','t','y','s','TT','WW','K1','K2','K3','K4');
syms('P1','P2');
TRUE = 1;
FALSE = 0;
% STEP 1 Runge-Kutta Order 4 Method is implemented within the
% following code.
fprintf(1,'This is the Adams Variable Step-size Predictor-');
fprintf(1,'Corrector Method\n');
fprintf(1,'Input the function F(t,y) in terms of t and y\n');
fprintf(1,'For example: y-t^2+1 \n');
s = input(' ');
F = inline(s,'t','y');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input left and right endpoints on separate lines.\n');
A = input(' ');
B = input(' ');
if A >= B
fprintf(1,'Left endpoint must be less than right endpoint.\n');
else
OK = TRUE;
end;
end;
OK = FALSE;
fprintf(1,'Input the initial condition.\n');
ALPHA = input(' ');
while OK == FALSE
fprintf(1,'Input tolerance.\n');
TOL = input(' ');
if TOL <= 0
fprintf(1,'Tolerance must be positive.\n');
else
OK = TRUE;
end;
end;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input minimum and maximum mesh spacing ');
fprintf(1,'on separate lines.\n');
HMIN = input(' ');
HMAX = input(' ');
if HMIN < HMAX & HMIN > 0
OK = TRUE;
else
fprintf(1,'Minimum mesh spacing must be a ');
fprintf(1,'positive real number and less than\n');
fprintf(1,'the maximum mesh spacing.\n');
end;
end;
if OK == TRUE
fprintf(1,'Choice of output method:\n');
fprintf(1,'1. Output to screen\n');
fprintf(1,'2. Output to text file\n');
fprintf(1,'Please enter 1 or 2\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\name.ext\n');
fprintf(1,'For example A:\\OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP, 'ADAMS VARIABLE STEP-SIZE PREDICTOR CORRECTOR METHOD\n\n');
fprintf(OUP, ' t w h sigma\n');
% STEP 2
T = zeros(1,100);
W = zeros(1,100);
T(1) = A;
W(1) = ALPHA;
H = HMAX;
% OK is used in place of FLAG to exit the loop in Step 4.
OK = TRUE;
% DONE is used in place of last to indicate when last value
% is calculated
DONE = FALSE;
% STEP 3
for KK = 1:3
X = T(KK);
Y = W(KK);
K1 = H*F(X,Y);
K2 = H*F(X+0.5*H,Y+0.5*K1);
K3 = H*F(X+0.5*H,Y+0.5*K2);
K4 = H*F(X+H,Y+K3);
WW = Y+(K1+2.0*(K2+K3)+K4)/6.0;
TT = X+H;
T(KK+1) = TT;
W(KK+1) = WW;
end;
% NFLAG indicates the computation from RK4
NFLAG = 1;
I = 5;
% use TT in place of t
TT = T(4) + H;
% STEP 4
while DONE == FALSE
% STEP 5
% predict W(I)
P1 = 55.0*F(T(I-1),W(I-1))-59.0*F(T(I-2),W(I-2));
P2 = 37.0*F(T(I-3),W(I-3))-9.0*F(T(I-4),W(I-4));
WP = W(I-1)+H*(P1+P2)/24.0;
% correct W(I)
P1 = 9.0*F(TT,WP)+19.0*F(T(I-1),W(I-1))-5.0*F(T(I-2),W(I-2));
P2 = F(T(I-3),W(I-3));
WC = W(I-1)+H*(P1+P2)/24.0;
SIG = 19.0*abs(WC-WP)/(270.0*H);
% STEP 6
if SIG <= TOL
% STEP 7
% result accepted
W(I) = WC;
T(I) = TT;
% STEP 8
if NFLAG == 1
K = I-3;
KK = I-1;
% Previous results are also accepted.
for J = K:KK
fprintf(OUP, '%12.8f %11.8f %11.8f %11.8f\n', T(J), W(J), H, SIG);
end;
fprintf(OUP, '%12.8f %11.8f %11.8f %11.8f\n', T(I), W(I), H, SIG);
else
% Previous results were already accepted.
fprintf(OUP, '%12.8f %11.8f %11.8f %11.8f\n', T(I), W(I), H, SIG);
end;
% STEP 9
if OK == FALSE
% Next step is 20.
DONE = TRUE;
else
% STEP 10
I = I+1;
NFLAG = 0;
% STEP 11
if SIG <= 0.1*TOL | T(I-1)+H > B
% Increase H if more accuracy than required has been obtained,
% or decrease H to include b as a mesh point.
% STEP 12
% to avoid underflow
if SIG <= 1.0e-20
Q = 4.0;
else
Q = (0.5*TOL/SIG)^(1/4);
end;
% STEP 13
if Q > 4.0
H = 4.0*H;
else
H = Q * H;
end;
% STEP 14
if H > HMAX
H = HMAX;
end;
% STEP 15
if T(I-1)+4.0*H > B
H = 0.25*(B-T(I-1));
if H < TOL
DONE = TRUE;
end;
OK = FALSE;
end;
% STEP 16
for KK = I-1:I+2
X = T(KK);
Y = W(KK);
K1 = H*F(X,Y);
K2 = H*F(X+0.5*H,Y+0.5*K1);
K3 = H*F(X+0.5*H,Y+0.5*K2);
K4 = H*F(X+H,Y+K3);
WW = Y+(K1+2.0*(K2+K3)+K4)/6.0;
TT = X+H;
T(KK+1) = TT;
W(KK+1) = WW;
end;
NFLAG = 1;
I = I+3;
end;
end;
else
% FALSE branch for Step 6 - result rejected.
% STEP 17
Q = (0.5*TOL/SIG)^(1/4);
% STEP 18
if Q < 0.1
H = 0.1 * H;
else
H = Q * H;
end;
% STEP 19
if H < HMIN
fprintf(OUP, 'HMIN exceeded\n');
DONE = TRUE;
else
if T(I-1)+4.0*H > B
H = 0.25*(B-T(I-1));
end;
if NFLAG == 1
% Previous results also rejected.
I = I-3;
end;
for KK = I-1:I+2
X = T(KK);
Y = W(KK);
K1 = H*F(X,Y);
K2 = H*F(X+0.5*H,Y+0.5*K1);
K3 = H*F(X+0.5*H,Y+0.5*K2);
K4 = H*F(X+H,Y+K3);
WW = Y+(K1+2.0*(K2+K3)+K4)/6.0;
TT = X+H;
T(KK+1) = TT;
W(KK+1) = WW;
end;
I = I+3;
NFLAG = 1;
end;
end;
% STEP 20
TT = T(I-1) + H;
end;
% STEP 21
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully \n',NAME);
end;
end;

% RUNGE-KUTTA FOR SYSTEMS OF DIFFERENTIAL EQUATIONS ALGORITHM 5.7


%
% TO APPROXIMATE THE SOLUTION OF THE MTH-ORDER SYSTEM OF FIRST-
% ORDER INITIAL-VALUE PROBLEMS
% UJ' = FJ( T, U1, U2, ..., UM ), J = 1, 2, ..., M
% A <= T <= B, UJ(A) = ALPHAJ, J = 1, 2, ..., M
% AT (N+1) EQUALLY SPACED NUMBERS IN THE INTERVAL (A,B).
%
% INPUT: ENDPOINTS A,B; NUMBER OF EQUATIONS M; INITIAL
% CONDITIONS ALPHA1, ..., ALPHAM; INTEGER N.
%
% OUTPUT: APPROXIMATION WJ TO UJ(T) AT THE (N+1) VALUES OF T.
syms('OK', 'M', 'I', 'A', 'B', 'ALPHA', 'N', 'FLAG');
syms('NAME', 'OUP', 'H', 'T', 'J', 'W', 'L', 'K','ss');
syms('K1','K2','K3','K4','Z','kk');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is the Runge-Kutta Method for Systems of m
equations\n');
fprintf(1,'This program uses the file F.m. If the number of
equations\n');
fprintf(1,'exceeds 7, then F.m must be changed.\n');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the number of equations\n');
M = input(' ');
if M <= 0 | M > 7
fprintf(1,'Number must be a positive integer < 8\n');
else
OK = TRUE;
end;
end;
ss = cell(M,1);
for I = 1:M
fprintf(1,'Input the function F_(%d) in terms of t and y1 ... y%d\n',
I,M);
fprintf(1,'For example: y1-t^2+1 \n');
kk = input(' ');
ss{I} = kk;
end;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input left and right endpoints on separate lines.\n');
A = input(' ');
B = input(' ');
if A >= B
fprintf(1,'Left endpoint must be less than right endpoint\n');
else
OK = TRUE;
end;
end;
ALPHA = zeros(1,M);
for I = 1:M
fprintf(1,'Input the initial condition alpha(%d)\n', I);
ALPHA(I) = input(' ');
end;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input a positive integer for the number of subintervals\n');
N = input(' ');
if N <= 0
fprintf(1,'Number must be a positive integer\n');
else
OK = TRUE;
end;
end;
if OK == TRUE
fprintf(1,'Choice of output method:\n');
fprintf(1,'1. Output to screen\n');
fprintf(1,'2. Output to text file\n');
fprintf(1,'Please enter 1 or 2\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\name.ext\n');
fprintf(1,'For example A:\\OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP,'RUNGE-KUTTA METHOD FOR SYSTEMS OF DIFFERENTIAL
EQUATIONS\n\n');
fprintf(OUP, ' T');
for I = 1:M
fprintf(OUP, ' W%d', I);
end;
% STEP 1
W = zeros(1,M);
V = zeros(1,M+1);
K1 = zeros(1,M);
K2 = zeros(1,M);
K3 = zeros(1,M);
K4 = zeros(1,M);
H = (B-A)/N;
T = A;
% STEP 2
for J = 1:M
W(J) = ALPHA(J);
end;
% STEP 3
fprintf(OUP, '\n%5.3f', T);
for I = 1:M
fprintf(OUP, ' %11.8f', W(I));
end;
fprintf(OUP, '\n');
% STEP 4
for L = 1:N
% STEP 5
V(1) = T;
for J = 2:M+1
V(J) = W(J-1);
end;
for J = 1:M
Z = H*F(J,M,V,ss);
K1(J) = Z;
end;
% STEP 6
V(1) = T+H/2;
for J = 2:M+1
V(J) = W(J-1)+K1(J-1)/2;
end;
for J = 1:M
Z = H*F(J,M,V,ss);
K2(J) = Z;
end;
% STEP 7
for J = 2:M+1
V(J) = W(J-1)+K2(J-1)/2;
end;
for J = 1:M
Z = H*F(J,M,V,ss);
K3(J) = Z;
end;
% STEP 8
V(1) = T + H;
for J = 2:M+1
V(J) = W(J-1)+K3(J-1);
end;
for J = 1:M
Z = H*F(J,M,V,ss);
K4(J) = Z;
end;
% STEP 9
for J = 1:M
W(J) = W(J)+(K1(J)+2.0*K2(J)+2.0*K3(J)+K4(J))/6.0;
end;
% STEP 10
T = A+L*H;
% STEP 11
fprintf(OUP, '%5.3f', T);
for I = 1:M
fprintf(OUP, ' %11.8f', W(I));
end;
fprintf(OUP, '\n');
end;
% STEP 12
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully \n',NAME);
end;
end;

También podría gustarte