Sei sulla pagina 1di 22

UNIVERSIDAD NACIONAL DE

SAN AGUSTIN
ESCUELA PROFESIONAL DE
INGENIERIA CIVIL
METODOS NUMERICOS Y
PROGRAMACION
RAICES DE ECUACIONES
SISTEMAS DE ECUACIONES
LINEALES Y NO LINEALES
INTERPOLACION, INTEGRACION Y
DIFERENCIACION NUMERICA
PROFESOR:
ALUMNO: LUCIO FUTURI MOSCOSO
HORARIO:
GRUPO: B

AREQUIPA PERU
2014
RAICES DE ECUACIONES
PROBLEMA 1

Resolver la siguiente ecuacin:


0=1/X.^0.5+2*log10(E/(3.7*D)
+2.51/(RE*X.^0.5)), donde E es la rugosidad, D el dimetro, RE
el numero de Reynolds, RO es la densidad del fluido, V es la
velocidad, y MU la viscosidad dinmica.
METODO DEL PUNTO FIJO:
function Y = G1(X)
E=0.0000015;
D=0.005;
RO=1.23;
V=40;
MU=1.79*10.^-5;
RE=RO*V*D/MU;
Y =0.25/(log10(E/(3.7*D)+2.51/(RE*X.^0.5))).^2;
return;
PROGRAMA
function [Xp, ITER, FLAG] = puntofijo(G, X0, EPS1, MAXIT)
% Sintaxis
% [Xp, ITER, FLAG] = PUNTOFIJO(G, X0, EPS1, MAXIT)
% Entrada
% - G es la funcin, almacenada como un archivo G.m
% - X0 es el valor inicial
% - EPS1 es el criterio de convergencia
% - MAXIT es el nmero mximo de iteraciones
% Salida
% - Xp es el valor de la raz aproximada
% - ITER es el nmero de iteraciones realizadas
for K = 1 : MAXIT
X = feval(G, X0);
if (abs(X - X0)) < EPS1
Xp = X;
ITER = K;
return;
end
X0 = X;
end
disp('Se ha excedido el mximo nmero de
iteraciones...');
ITER = MAXIT;
end
SOLUCION :
>> [Xp, ITER, FLAG] = puntofijo('G1', 3, 1e-6, 25)
Xp = 0.0290
ITER = 7
METODO DE NEWTON
function Y = f(X)
E=0.0000015;
D=0.005;

RO=1.23;
V=40;
MU=1.79*10.^-5;
RE=RO*V*D/MU;
Y =1/X.^0.5+2*log10(E/(3.7*D)+2.51/(RE*X.^0.5));
return;
function y =df(x)
h=0.0000001;
y=(f(x+h)-f(x))/h;
end
PROGRAMA
function [x,k]=newton(x,t1,t2)
% Sintaxis
% [x,k] = newton(x,t1,t2)
% Entrada
% - f es la funcin, almacenada como un archivo f.m
% - df es la derivada de de la funcin, almacenada como un
archivo df.m
% - X es el valor inicial
% - t1 es el criterio de tolerancia
% - t2 es el criterio de tolerancia
% - x es el valor de la raz aproximada
k=0; y=x+1;
while abs(f(x))>=t1 && abs(y-x)>=t2
y=x;
x=x-f(x)/df(x);
k=k+1;
end
Notese que al usar newton se debe graficar la ecuacin
previamente para visualizar un punto adecuado a partir del
cual se comenzara la iteracin:
>> [ x
6,10^-7)
x = Inf k =
7

k]=

newton(1,10^-

Infi

>> [ x k]= newton(0.03,10^6,10^-7)


x =
0.0290
k =
3

>> [ x k]= newton(0.001,10^6,10^-7)


x =
0.0290
k =
8
>> fplot('f',[-10 20])
Warning: Imaginary parts of
complex X and/or Y arguments
ignored

-2

-4

-6

-8

-10
-10

-5

10

15

20

PROBLEMA 2
La velocidad de un paracaidista que cae esta dada por:
v
=(g*x/c)*(1-exp(-(c/x)*t),donde
g=9.81m/s2
.Para
un
paracaidista con coeficiente de arrastre de c=15kg/s, calcule
la masa de modo que la velocidad sea v=35m/s en t=9seg.
METODO DE LA BISECCION
function y = f(x)
g=9.81;
c=15;
v=35;
t=9;
y=(g*x/c)*(1-exp(-(c/x)*t))-v;
return;
Antes de aplicar la iteracin es recomendable graficar la
ecuacin para tener puntos de inicializacin y no caer en
intervalos intiles.

15
10
5
0
-5
-10
-15
-20
-25
-30
-35

10

20

30

40

50

60

70

80

90

100

PROGRAMA
function [c,k]=biseccion(a,b,e)
% Sintaxis
% [c,k] = biseccion(a, b, e )
% Entrada
% - f es la funcin, almacenada como un archivo f.m
% - a, b son los extremos del intervalo inicial
% e es el criterio de exactitud o tolerancia
% Salida
% - c es el valor de la raz aproximada
k=0;
while b-a>=e
c=(a+b)/2;
if f(a)*f(c)<0
b=c;
else
a=c;
end
k=k+1;
end
SOLUCION
La_masa_del_individuo_es= biseccion(50,70,10^-3)
La_masa_del_individuo_es = 59.7577
METODO DE LA SECANTE
PROGRAMA
function [Xp, ITER, FLAG] = SECANTE(f, X0, X1, EPS1, EPS2,
MAXIT)
% Sintaxis
% [Xp, ITER, FLAG] = SECANTE(f, X0, X1, EPS1, EPS2, MAXIT)
% Entrada
% - f es la funcin, almacenada como un archivo f.m

% - X0, X1 son los valores iniciales


% - EPS1 es el criterio de convergencia
% - EPS2 es el criterio de exactitud
% - MAXIT es el nmero mximo de iteraciones
% Salida
% - Xp es el valor de la raz aproximada
% - ITER es el nmero de iteraciones realizadas
for K = 1 : MAXIT
Xp = X0 - (X1 - X0) * feval(f, X0) / (feval(f, X1) feval(f, X0));
if (abs(Xp - X1) <= EPS1) || (abs(feval(f, Xp)) <=
EPS2)
ITER = K;
return;
end
X0 = X1;
X1 = Xp;
end
disp('Se ha excedido el mximo nmero de iteraciones..');
ITER = MAXIT;
SOLUCION
>> [Xp, ITER, FLAG] = SECANTE('f', 65, 75, 1e-6, 1e-6, 10)
Xp = 59.7582
ITER = 4

SISTEMA DE ECUACIONES LINEALES


PROBLEMA3
Resolver el sistema lineal:
>>
A=[3
-.1
-.2;0.1
-0.3;0.3 -0.2 10]

0.3000
10.0000
>> b=[7.85;-19.3;71.4]

A =
b =
3.0000

-0.1000

-0.2000
0.1000

7.0000

-0.3000
METODO GAUSS
PROGRAMA
function X =gauss(A,b)
% ENTRADAS
%
A : Matriz nxn
%
B : Vector nx1
% SALIDAS
%
X : Vector solucin
[~,n]=size(A);
X=zeros(n,1);

7.8500
-19.3000
71.4000

-0.2000

for k=1:1:n-1
for i=k+1:1:n
m=A(i,k)/A(k,k);
A(i,k)=0;
for j=k+1:1:n
A(i,j)=A(i,j)-m*A(k,j);
end
b(i)=b(i)-m*b(k);
end
end
X(n)=b(n)/A(n,n);
for k=n-1:-1:1
S=0;
for j=k+1:1:n
S=S+A(k,j)*X(j);
end
X(k)=(b(k)-S)/A(k,k);
end
end

SOLUCION
>> EL_VECTOR_SOLUCION_ES = gauss(A,b)
EL_VECTOR_SOLUCION_ES
X =
3.0000
-2.5000
7.0000
METODO FACTORIZACION LU
PROGRAMA
function descomp
a=input('ingrese la matriz A=');
b=input('ingrese el vector b=');
n=length(b);
l=zeros(n);
u=zeros(n);
for k=1:n
for j=k:n
s=0;
p=1;
while p<=k-1
s=s+l(k,p)*u(p,j);
p=p+1;
end
u(k,j)=a(k,j)-s;
end
l(k,k)=1;
for i=k+1:n
sum=0;
q=1;
while q<=k-1

sum=sum+l(i,q)*u(q,k);
q=q+1;
end
l(i,k)=(a(i,k)-sum)/u(k,k);
end
end
l
u
y(1)=b(1);
for i=2:n
s=0;
for k=1:i-1
s=s+l(i,k)*y(k);
end
y(i)=b(i)-s;
end
y
x(n)=y(n)/u(n,n);
for i=n-1:-1:1
s=0;
for k=i+1:n
s=s+u(i,k)*x(k);
end
x(i)=(y(i)-s)/u(i,i);
end
x
SOLUCION
>> descomp
ingrese la matriz A=[3 -.1 -.2;0.1 7 -0.3;0.3 -0.2 10]
ingrese el vector b=[7.85;-19.3;71.4]
l =
1.0000
0.0333
0.1000

0
1.0000
-0.0271

0
0
1.0000

3.0000
0
0

-0.1000
7.0033
0

-0.2000
-0.2933
10.0120

y = 7.8500

-19.5617

70.0843

x = 3.0000

-2.5000

7.0000

u =

PARA UN SISTEMA TRIDIAGONAL


PROBLEMA 4

Resolver el siguiente sistema de ecuaciones:


M =
2.0147
-0.0209
0
0

-0.0209
2.0147
-0.0209
0

0
-0.0209
2.0147
-0.0209

0
0
-0.0209
2.0147

B =
4.1750
0.0000
0.0000
2.0875
PROGRAMA
function X = tridiagonalsistema
A=input('ingrese EL VECTOR A=');
D=input('ingrese EL VECTOR D=');
C=input('ingrese EL VECTOR C=');
B=input('ingrese EL VECTOR B=');
%Este programa resuelve un sistema lineal MX=B donde M es
una matriz
%triagonal, en este caso consideraremos la matriz M
compuesto por los
%vectores D,C y A
% ENTRADAS
%
A
vector (n-1)*1 diagonal inferior
%
D
vector n*1 diagonal principal
%
C
vector (n-1)*1 diagonal superior
%
B
vector independiente nx1
% SALIDA
%
X
vector solucin
n = length(B);
for k = 2:n,
mult = A(k-1)/D(k-1);
D(k) = D(k) - mult*C(k-1);
B(k) = B(k) - mult*B(k-1);
end
X(n) = B(n)/D(n);
for k = (n-1):-1:1,
X(k) = (B(k) - C(k)*X(k+1))/D(k);
end
SOLUCION
>> tridiagonalsistema
ingrese EL VECTOR A=[-0.020875 -0.020875 -0.020875 0]
ingrese EL VECTOR D=[2.01475 2.01475 2.01475 2.01475]
ingrese EL VECTOR C=[-0.020875 -0.020875 -0.020875 0]
ingrese EL VECTOR B=[4.175; 0,;0;2.0875]
ans =

2.0724

0.0216

0.0110

1.0362

SISTEMAS DE ECUACIONES NO LINEALES


PROBLEMA 5
RESOLVER EL SISTEMA:
function G = FG_2EC(X)
G(1) = (4 - sqrt(X(2)))^3 / 8;
G(2) = sqrt(17 - X(1)^2);
return;
METODO DEL PUNTO FIJO
PROGRAMA
function [Xp, ITER, FLAG] = PUNTOFIJO_MV(G, X0, EPS,
MAXIT)
% Sintaxis
% PUNTOFIJO_MV(G, X0, EPS, MAXIT)
% Entrada
% - G es el sistema de ecuaciones no lineales, almacenado
como G.m
% - X0 es el vector de valores iniciales
% - EPS es el criterio de convergencia
% - MAXIT es el nmero mximo de iteraciones
% Salida
% - Xp es el vector solucin
% - ITER es el nmero de iteraciones realizadas
for K = 1 : MAXIT
X = feval(G, X0);
if norm(X - X0) <= EPS
Xp = real(X);
ITER = K;
return;
end
X0 = X;
end
disp('Se ha excedido el mximo nmero de iteraciones...');
ITER = MAXIT;
SOLUCION
>> [Xp, ITER, FLAG] =PUNTOFIJO_MV('FG_2EC', [0.5, 0.5], 1e-6,
25)
Xp = 1.0000

4.0000

ITER = 17
PROBLEMA 6
RESOLVER EL SIGUIENTE SISTEMA:
function Y = F1_2EC(X)
Y(1) = 4 - (8 * X(1))^(1/3) - X(2)^(1/2);
Y(2) = 17 - X(1)^2 - X(2)^2;
return;
function dY = JF1_2EC(X)
dY(1) = -(8/3) * (8 * X(1))^(-2/3);
dY(2) = -2 * X(2);
return;
PROGRAMA
function [Xp, ITER, FLAG] = NEWTON_MODIF(F, D, X, EPS1,
MAXIT)
% Sintaxis
% [Xp, ITER, FLAG] = NEWTON_MODIF(F, D, X, EPS1, MAXIT)
% Entrada
% - F es el sistema de ecuaciones no lineales, almacenado
como F.m
% - D son las derivadas, almacenadas como D.m
% - X es el vector de valores iniciales
% - EPS1 es el criterio de convergencia
% - MAXIT es el nmero mximo de iteraciones
% Salida
% - Xp es el vector solucin
% - ITER es el nmero de iteraciones realizadas
% - FLAG es una variable lgica, es 1 si hubo
convergencia, caso
% contrario es 0
for K = 1 : MAXIT
XAUX = X;
for I = 1 : 2
FX = feval(F, X);
DX = feval(D, X);
X(I) = X(I) - FX(I) / DX(I);
end
if norm(XAUX - X) < EPS1
ITER = K;
FLAG = 1;
Xp = X;
return;
end
end
disp('Se ha excedido el mximo nmero de iteraciones...');
ITER = MAXIT;
FLAG = 0;
SOLUCION

>> [Xp, ITER, FLAG] = NEWTON_MODIF('F1_2EC', 'JF1_2EC', [4,


3], 1e-6, 25)
Xp =
1.0
4.0000
2.0000
ITER = 9
FLAG = 1

INTERPOLACION NUMERICA
PROBLEMA 7
OBTENER LOS COEFICIENTES DE INTERPOLACION DE NEWTON DEl
SIGUIENTE CONJUNTO DE PARES ORDENADOS MOSTRADOS EN LA
SIGUIENTE FORMA:
X= [0 8 16 24 32 40]
Y= [14.621 11.483 9.87 8.418 7.305 6.413]
METODO DE DIFERENCIAS FINITAS
PROGRAMA
function diferenciafinitas
x=input('ingrese las variables independientes x= ');
y=input('ingrese las variables dependientes y= ');
%
---------------------------------------------------------% Este programa realiza la interpolacin de un conjunto de
puntos(x,y)
% por el metodo de diferencias finitas.
%---------------------------------------------------------n=length(x);
for i=1:n
A(i,1)=y(i);
end
for j=2:n
for k=1:n-j+1
A(k,j)=A(k+1,j-1)-A(k,j-1);
end
end
A
SOLUCION
Para este caso solo se obtendr el cuadro general de dicho
mtodo:
>> diferenciafinitas
ingrese las variables indepedientes
x= [0 8 16 24 32 40]
ingrese las variables dependientes
y= [14.621 11.483 9.87 8.418 7.305 6.413]
A =

14.6210
11.4830
9.8700
8.4180
7.3050
6.4130

-3.1380
-1.6130
-1.4520
-1.1130
-0.8920
0

1.5250
0.1610
0.3390
0.2210
0
0

-1.3640
0.1780
-0.1180
0
0
0

1.5420
-0.2960
0
0
0
0

-1.8380
0
0
0
0
0

METODO DE DIFERENCIAS DIVIDIDAS HACIA ARRIBA


PROGRAMA
function difdivarriba
X=input('ingrese el vector X=');
Y=input('ingrese el vector Y=');
%
---------------------------------------------------------% Este programa realiza la interpolacin de un conjunto de
puntos (x,y)
% por el metodo de diferencias divididas hacia arriba.
%---------------------------------------------------------% ENTRADAS
%
X : Conjunto de abcisas
%
Y : Conjunto de ordenadas
% SALIDAS:
%
C : Coeficientes del polinmio interpolante de
Newton
%
D :Coeficientes de la tabla por el metodo de
diferencias
%
divididas hacia abajo
%---------------------------------------------------------n=length(X);
D=zeros(n,n);
D(:,1)=Y';
%Formula de la tabla de diferencia divididas hacia abajo.
for j=2:n
for k=j:n
D(k,j)=(D(k,j-1)-D(k-1,j-1))/(X(k)-X(k-j+1));
end
end
D
SOLUCION
>> difdivarriba
ingrese el vector X=[0 8 16 24 32 40]
ingrese el vector Y=[14.621 11.483 9.87 8.418 7.305 6.413]
D =
14.6210

11.4830
9.8700
8.4180
7.3050
6.4130

-0.3922
-0.2016
-0.1815
-0.1391
-0.1115

0
0.0119
0.0013
0.0026
0.0017

0
0
-0.0004
0.0001
-0.0000

0
0
0
0.0000
-0.0000

0
0
0
0
-0.0000

A partir del cuadro obtenido se extrae los coeficientes y se


compone la siguiente funcin:
function y = f(x)
y=14.6210-0.3922*(x-0)+0.0119*(x-8)*(x-0)-0.0004*(x0)*(x-8)*(x-16);
>> f(27)
ans = 7.8791
METODO DE DIFERENCIAS DIVIDIDAS HACIA ABAJO
PROGRAMA
function diferenciasdivididas
x=input('ingrese el vector x=');
y=input('ingrese el vector y=');
n=length(x);
a=zeros(n);
for i=1:n
a(i,1)=y(i);
end
for j=2:n
for k=1:n-j+1
a(k,j)=(a(k+1,j-1)-a(k,j-1))/(x(k+j-1)-x(k));
end
end
%a representa que contiene los coeficientes del polinmio
interpolante de Newton
a
%C representa los coeficientes del polinmio interpolante
de Newton
C=a(n,n);
for k=(n-1):-1:1
C=conv(C,poly(x(k)));
m=length(C);
C(m)=C(m)+a(k,k);
end
C
SOLUCION
>> diferenciasdivididas
ingrese el vector x=[0 8 16 24 32 40]
ingrese el vector y=[14.621 11.483 9.87 8.418 7.305 6.413]
a =

14.6210
11.4830
9.8700
8.4180
7.3050
6.4130

-0.3922
-0.2016
-0.1815
-0.1391
-0.1115
0

0.0119
0.0013
0.0026
0.0017
0
0

-0.0004
0.0001
-0.0000
0
0
0

0.0000
-0.0000
0
0
0
0

-0.0000
0
0
0
0
0

A partir de la tabla obtenida anteriormente se obtiene los


coeficientes para el polinomio interpolador:
function y = f(x)
y=14.6210-0.3922*(x-0)+0.0119*(x-8)*(x-0)-0.0004*(x-0)*(x8)*(x-16);
>> f(27)
ans = 7.8791
METODO DE LAGRANGE
El problema planteado es el mismo que el anterior, pero esta
vez por otro mtodo.
>> X= [0 8 16 24 32 40]
X = 0
8
16

24

32

40

>> Y= [14.621 11.483 9.87 8.418 7.305 6.413]


Y =
14.6210
11.4830
9.8700
6.4130

8.4180

7.3050

PROGRAMA
function LAGRANGE1
X=input('ingrese el vector X=');
Y=input('ingrese el vector Y=');
XINT=input('ingrese el vector XINT=');
% Sintaxis
% YINT = LAGRANGE1(X, Y, XINT)
% Entrada
% - X es un vector que contiene la lista de abscisas
% - Y es un vector que contiene la lista de ordenadas
% - XINT es el valor para el que se desea la interpolacin
% Salida
% - YINT es el valor de la funcin en XINT
N = length(X) - 1;
YINT = 0;
for I = 1 : N + 1
L = 1;
for J = 1 : N + 1
if I ~= J
L = L * (XINT - X(J)) / (X(I) - X(J));
end
end
YINT = YINT + L * Y(I);
end
YINT

SOLUCION
>> LAGRANGE1
ingrese el vector X= [0 8 16 24 32 40]
ingrese el vector Y= [14.621 11.483 9.87 8.418 7.305 6.413]
ingrese el vector XINT= 27
YINT = 7.9417

METODOS DE INTEGRACION NUMERICA


PROBLEMA 8
Integrar la funcin dada en forma tabular:
>> X= [0, 1, 2, 3, 4, 5, 6]; Y = [-5, 1, 9, 25, 55, 105, 181];
METODO DEL TRAPECIO
PROGRAMA
function S = TRAPECIO(X, FX)
% Sintaxis
% I = TRAPECIO(X, FX)
% Entrada
% - X es un vector que contiene la lista de variables
independientes
% - FX es un vector que contiene la lista de variables
dependientes o
% integrando
% - a y b son los extremos inferior y superior del
intervalo de
% integracin
% Salida
% - S es la aproximacin a la integral con la regla
compuesta del
% trapecio
N = length(X);
a = X(1);
b = X(N);
H = (b - a) / (N - 1);
S = 0;
for K = 2 : (N - 1)
S = S + FX(K);
end
S = (H / 2) * (FX(1) + 2 * S + FX(N));
>> TRAPECIO(X,Y)
ans = 283
METODO DE SIMPSON 1/3
PROGRAMA

function S = SIMPSON13(X, FX)


% Sintaxis
% I = SIMPSON13(X, FX)
% Entrada
% - X es un vector que contiene la lista de variables
independientes
% - FX es un vector que contiene la lista de variables
dependientes o integrando
% - a y b son los extremos inferior y superior del
intervalo de integracin
% Salida
% - S es la aproximacin a la integral con la regla
compuesta del Simpson 1/3
N = length(X);
a = X(1);
b = X(N);
H = (b - a) / (N - 1);
S1 = 0;
S2 = 0;
for K = 2 : 2 : (N - 1)
S1 = S1 + FX(K);
end
for K = 3 : 2 : (N - 2)
S2 = S2 + FX(K);
end
S = (H / 3) * (FX(1) + 4 * S1 + 2 * S2 + FX(N));
SOLUCION
>> X=[0, 1, 2, 3, 4, 5, 6]; Y = [-5, 1, 9, 25, 55, 105, 181];
>> SIMPSON13(X, Y)
ans = 276
METODO DE SIMPSON 3/8
PROGRAMA
function S = SIMPSON38(X, FX)
% Sintaxis
% I = SIMPSON38(X, FX)
% Entrada
% - X es un vector que contiene la lista de variables
independientes
% - FX es un vector que contiene la lista de variables
dependientes o integrando
% - a y b son los extremos inferior y superior del
intervalo de integracin
% Salida
% - S es la aproximacin a la integral con la regla
compuesta del Simpson 3/8
N = length(X);
a = X(1);
b = X(N);
H = (b - a) / (N - 1);

S1 = 0;
S2 = 0;
for K = 2 : (N - 1)
if mod(K - 1, 3) == 0
S1 = S1 + FX(K);
else
S2 = S2 + FX(K);
end
end
S = (3 * H / 8) * (FX(1) + 2 * S1 + 3 * S2 + FX(N));
SOLUCION
>> X=[0, 1, 2, 3, 4, 5, 6]; Y = [-5, 1, 9, 25, 55, 105, 181];
>> SIMPSON38(X,Y)
ans = 276
PROBLEMA 9
Integrar la funcin en forma tabular:
x =[0 1 2 3 4 5]

y = f(x)=[ -5 1 9 25 55 105]

Como el numero de subintervalos es impar (en este caso 5)


aplicamos una combinacion de las formulas de Simpson 1/3 y
Simpson 3/8, veamos:
>>
>>
>>
>>
>>

X1 = [0, 1, 2, 3]
Y1 = [-5, 1, 9, 25]
X2 = [3, 4, 5]
Y2 = [25, 55, 105]
I = SIMPSON38(X1, Y1) + SIMPSON13(X2, Y2)
I = 135.4167

METODO DE ROMBERG
PROBLEMA 10
Encontrar la integral de la funcin de x en el intervalo de 0
a 1.
function y = f(x)
y = 4 * x^3 - 3 * x^2 + 2 * x + 1;
return;
PROGRAMA
function [R, Q, EPS, H] = ROMBERG(f, a, b, K, TOL)
% Sintaxis
% [R, Q, EPS, H] = ROMBERG(F, a, b, K, TOL)
% Entrada
% - f es la funcin integrando, almacenada como un archivo
f.m
% - a y b son los extremos inferior y superior del
intervalo de
% integracin
% - K es el nmero mximo de fi las de la tabla

% - TOL es la tolerancia
% Salida
% - R es el esquema de Romberg
% - Q es la aproximacin a la integral
% - EPS es una estimacin del error
% - H es el menor de los incrementos utilizados
N = 1;
H = b - a;
EPS = 1;
J = 0;
R = zeros(4, 4);
R(1, 1) = H * (feval(F, a) + feval(F, b)) / 2;
while ((EPS > TOL) && (J < K)) || (J < 4)
J = J + 1;
H = H / 2;
S = 0;
for P = 1 : N
X = a + H * (2 * P - 1);
S = S + feval(F, X);
end
R(J + 1, 1) = R(J, 1) / 2 + H * S;
N = 2 * N;
for M = 1 : J
R(J + 1, M + 1) = R(J + 1, M) + (R(J + 1, M) R(J, M)) / (4^M - 1);
end
EPS = abs(R(J, J) - R(J + 1, M + 1));
end
Q = R(J + 1, J + 1);
SOLUCION
>> [R, I, EPS, H]= ROMBERG('f',0, 1, 4, 1e-2)
R =
2.5000
2.1250
2.0313
2.0078
2.0020
I
=
EPS =
H =

0
2.0000
2.0000
2.0000
2.0000

0
0
2.0000
2.0000
2.0000

0
0
0
2.0000
2.0000

0
0
0
0
2.0000

2
0
0.0625

METODOS DE DIFERENCIACION NUMERICA


METODO DE EULER
PROBLEMA 11
Resolver el siguiente problema de valor inicial:

function y = f(X, Y)
y = (Y + 1) / (X - 1);
return;

y(0)=1

PROGRAMA
function [t, Y] = EULER(F, a, b, Y0, N)
% Sintaxis
% [t, Y] = EULER(F, a, b, Y0, N)
% Entrada
% - F es la funcin, almacenada como un archivo F.m
% - a y b son los extremos del intervalo
% - Y0 es la condicin inicial YO = Y(a)
% - N es ell nmero de pasos
% Salida
% - t es el vector de la abscisas o variable independiente
% - Y es el vector de las ordenadas o variable dependiente
H = (b - a) / N;
t = zeros(1, N + 1);
Y = zeros(1, N + 1);
t = [a: H: b];
Y(1) = Y0;
for J = 1 : N
Y(J + 1) = Y(J) + H * feval(F, t(J), Y(J));
end
t = t';
Y = Y';
SOLUCION
>> [X, Y] = EULER('f', 0, 0.4, 1, 4)
X =

Y =
0
0.1000
0.2000
0.3000
0.4000

1.0000
0.8000
0.6000
0.4000
0.2000

METODO DE RUNGE KUTTA DE CUARTO ORDEN


PROBLEMA 12
Resolver el siguiente problema de valor inicial:
function y = f(X, Y)

y(0.5)=1

y = X + 2 * X * Y;
return;
PROGRAMA
function [t, Y] = RK4(F, a, b, Y0, N)
% Sintaxis
% [t, Y] = RK4(F, a, b, Y0, N)
% Entrada
% - F es la funcin, almacenada como un archivo F.m
% - a y b son los extremos del intervalo
% - Y0 es la condicin inicial YO = Y(a)
% - N es ell nmero de pasos
% Salida
% - t es el vector de la abscisas o variable independiente
% - Y es el vector de las ordenadas o variable dependiente
H = (b - a) / N;
t = zeros(1, N + 1);
Y = zeros(1, N + 1);
t = (a: H: b);
Y(1) = Y0;
for J = 1 : N
K1 = H * feval(F, t(J), Y(J));
K2 = H * feval(F, t(J) + H/2, Y(J) + K1/2);
K3 = H * feval(F, t(J) + H/2, Y(J) + K2/2);
K4 = H * feval(F, t(J) + H, Y(J) + K3);
Y(J + 1) = Y(J) + (K1 + 2 * K2 + 2 * K3 + K4)/6;
end
t = t';
Y = Y';
SOLUCION
>> [t, Y] = RK4('f', 0.5, 1, 1, 5)
t =

Y =
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000

1.0000
1.1744
1.4069
1.7155
2.1260
2.6755

Potrebbero piacerti anche