You are on page 1of 94

METODO NUMERICOS EN MATLAB

INTRODUCCIN
Tanto la ciencia y la tecnologa nos describen los fenmenos reales mediante modelos
matemticos. El estudio de estos modelos permite un conocimiento ms profundo del
fenmeno, as como de su evolucin futura.
Desafortunadamente, no siempre es posible aplicar mtodos analticos clsicos por
diferentes razones:La solucin formal es tan complicada que hace imposible cualquier
interpretacin posterior; simplemente no existen mtodos analticos capaces de
proporcionar soluciones al problema; no se adecuan al modelo concreto; o su aplicacin
resulta excesivamente compleja.
Para este tipos de casos son tiles las tcnicas numricas, que mediante una labor de
clculo ms o menos intensa, conducen a soluciones aproximadas que son siempre
numricos.
La importante del clculo radica en que implica la mayora de estos mtodos hacen que
su uso est ntimamente ligado al empleo de computadores, que mediante la
programacin nos permite la solucin de problemas matemticos.
Para la realizacin de este trabajo se utiliz el programa MATLAB.

FIAG-ESIC

METODO NUMERICOS EN MATLAB

INDICE:
PAGINA

UNIDAD I

RAICES DE UNA ECUACION NO LINEAL:


METODO DE BISECCION03
METODO DEL PUNTO FIJO06
METODO DE NEWTON10
METODO DE LA SECANTE.13

INTERPOLACION Y REGRESION
INTERPOLACION-POLINOMIO DE LAGRANGE16
REGRESION LINEAL19
REGRESION POLINOMIAL.22

UNIDAD II

SISTEMA DE ECUACIONES
METODO DE GAUSS-JORDAN.27
METODO DE GAUSS-SEIDEL30

SISTEMA DE ECUACIONES NO LINEALES


METODO DE NEWTON ..34

INTEGRACIONES NUMERICAS
METODO DEL TRAPECIO..37
METODO DE SIMPSON 1/340
METODO DE SIMPSON 3/845
2

FIAG-ESIC

METODO NUMERICOS EN MATLAB

INTEGRACION DOBLE47
UNIDAD III

ECUACIONES DIFERENCIALES ORDINARIAS


METODO DE EULER..50
METODO DE RUNGGE KUTTA54

SISTEMA DE ECUACIONES ORDINARIAS...59

ECUACIONES DE ORDEN SUPERIOR..63

ECUACIONES DIFERENCIALES DE SEGUNDO ORDEN


METODO DE LAS DIFERENCIAS FINITAS33

UNIDAD I:

RAICES DE ECUACIONES NO LINEALES


1. MTODO DE LA BISECCIN:
TEORA:
En matemticas, el mtodo de biseccin es un algoritmo de bsqueda de
races que trabaja dividiendo el intervalo a la mitad y seleccionando el
subintervalo que tiene la raz.

PROCEDIMIENTO:

Elija valores Iniciales para a y b de forma tal que lea funcin cambie
de signo sobre el intervalo. Esto se puede verificar asegurndose de que
:

f (a)f (b)<0

FIAG-ESIC

METODO NUMERICOS EN MATLAB

La primera aproximacin a la raz se determina con la frmula:

x n=( a+b)/2

Realizar las siguientes evaluaciones


subintervalo se encuentra la raz:

f (a)f (x n) < 0 Entonces

b=x n

f (a)f ( x n)>0

a=x n

Entonces

f (a)f (x n)=0 Entonces

para

determinar

en

que

x n Es la Raz

Calcule la nueva aproximacin:

x n+1=( a+b)/2

Evaluar la aproximacin relativa:

( x n +1x n )/x n +1 E
No. (Falso) Repetir el paso 3, 4 y 5
S. (Verdadero) Entonces

x n+1 Es la Raz

DIAGRAMA DE FLUJO:

FIAG-ESIC

METODO NUMERICOS EN MATLAB

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DEL PROGRAMA:


CDIGO EN EL BOTON CALCULAR:

f=get(handles.edit1,'string');
f=inline(f);
a=str2double(get(handles.edit2,'string'));
b=str2double(get(handles.edit3,'string'));
E=str2double(get(handles.edit4,'string'));
if f(a)*f(b)< 0
while abs(b-a)>E
x=(a+b)/2;
if f(a)*f(x)==0
a=b;
else
if f(a)*f(x)<0
b=x;
else
a=x;
end
end
set(handles.edit5,'string',x);

FIAG-ESIC

METODO NUMERICOS EN MATLAB

end
else
set(handles.edit5,'string','No existe la raiz en el intervalo');
end

CDIGO EN EL BOTN GRAFICAR:


functionvarargout = pushbutton2_Callback(h, eventdata, handles, varargin)
f=get(handles.edit1,'string');
f=inline(f);
ezplot(f), grid on

CDIGO EN EL BOTN SALIR:


function pushbutton6_Callback(hObject, eventdata, handles)
close
CODIGO EN EL BOTON BORRAR
function pushbutton5_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');
set(handles.edit6,'string','');

VENTANA DE DISEO Y APLICACIN:

FIAG-ESIC

METODO NUMERICOS EN MATLAB

2. MTODO DEL PUNTO FIJO:


TEORA:
Dada la ecuacin f (x)=0 , el mtodo de las aproximaciones sucesivas
reemplaza esta ecuacin por una equivalente,
forma
inicial

g(x)=f ( x)+ x .
x0 y

x=g( x ) , definida en la

Para encontrar la solucin, partimos de un valor

calculamos

una

nueva

aproximacin

x 1=g ( x 0) .

Reemplazamos el nuevo valor obtenido y repetimos el proceso. Esto da lugar


a una sucesin de valores,

{ x 0 , x 1, , x n }

que si converge, tendr como lmite

la solucin del problema.


En la figura se representa la interpretacin geomtrica del mtodo. Partimos
de un punto inicial x0 y calculamos
solucin con la recta

y=x

y=g( x 0) . La interseccin de esta

nos dar un nuevo valor

x1

ms prximo a

la solucin final.

FIAG-ESIC

METODO NUMERICOS EN MATLAB


Sin embargo, el mtodo puede divergir fcilmente. Es fcil comprobar que el
mtodo slo podr converger si la derivada

g ' (x)

es menor en valor

absoluto que la unidad (que es la pendiente de la recta definida por

y=x .

Un ejemplo de este caso se muestra en la figura. Esta condicin, que a puede


considerarse una severa restriccin del mtodo, puede obviarse fcilmente.
Para ello basta elegir la funcin

g(x) del siguiente modo:

g ( x ) =x+ f (x )

De forma que tomando un valor de


que

adecuado, siempre podemos hacer

g ( x ) cumpla la condicin de la derivada.

CONVERGENCIA:
El mtodo de aproximaciones sucesivas converge si

g ' (x) 1

Co
Convergencia Montona

Divergencia Montona

FIAG-ESIC

METODO NUMERICOS EN MATLAB

0< g' ( x ) <1 g ' (x )>1

DIAGRAMA DE FLUJO:

10

FIAG-ESIC

METODO NUMERICOS EN MATLAB

11

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
Function varargout =pushbutton2_Callback(h, eventdata, handles, varargin)
f=get(handles.edit15,'string');
g=inline(f)
a=str2double(get(handles.edit9,'string'));
E=str2double(get(handles.edit11,'string'));
n=str2double(get(handles.edit17,'string'));
x1=g(a)
k=1;
cadena1=sprintf('a = %8.6f valor inicial\n',a);
while abs(x1-a)>E & k<n
a=x1
x1=g(a)
k=k+1
cadena2=sprintf('x%d = %8.6f\n',k-1,x1);
cadena1=[cadena1,cadena2];
end
CDIGO EN EL BOTN GRAFICAR:
functionvarargout = pushbutton1_Callback(h, eventdata, handles, varargin)
funcionf=get(handles.edit1,'string');
f=inline(funcionf);
figure(1);
ezplot(f),grid on

VENTANA DE DISEO Y APLICACIN:

12

FIAG-ESIC

METODO NUMERICOS EN MATLAB

3- MTODO DE NEWTON RAPHSON:


Este mtodo parte de una aproximacin inicial
aproximacin mejor,

x 1=x 0

x0

y obtiene una

x 1 , dada por la frmula:

f ( x0 )
f ' (x 0 )

Este mtodo est definido por el denominador

f ( xi)

hace que

geomtricamente se base en una aproximacin a una recta tangente a la curva

13

FIAG-ESIC

METODO NUMERICOS EN MATLAB

y=f ( x)

trazada en el punto correspondiente a la aproximacin presente,

esto puede observarse en la figura:

DIAGRAMA DE FLUJO CON LA DERIVADA:

14

FIAG-ESIC

METODO NUMERICOS EN MATLAB

15

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
functionvarargout = pushbutton1_Callback(h, eventdata, handles, varargin)
f=get(handles.edit1,'string');
g=get(handles.edit2,'string');
f=inline(f);
g=inline(g);
x=str2double(get(handles.edit3,'string'));
E=str2double(get(handles.edit4,'string'));
x1=x-f(x)/g(x);
while abs (x1-x)>E
x=x1;
x1=x-f(x)/g(x);
end
set(handles.edit5,'string',x1);
CDIGO EN EL BOTN GRAFICAR:
functionvarargout = pushbutton2_Callback(h, eventdata, handles, varargin)
f=get(handles.edit1,'string');
f=inline(f);
ezplot(f), grid on
CDIGO EN EL BOTN SALIR:
function pushbutton6_Callback(hObject, eventdata, handles)
close

16

FIAG-ESIC

METODO NUMERICOS EN MATLAB


VENTANA DE DISEO Y APLICACIN:

17

FIAG-ESIC

METODO NUMERICOS EN MATLAB

SIN INGRESAR LA DERIVADA: DIAGRAMA DE FLUJO:

18

FIAG-ESIC

METODO NUMERICOS EN MATLAB

4. MTODO DE LA SECANTE:
TEORA:
El principal inconveniente del mtodo de Newton estriba en que requiere
conocer el valor de la primera derivada de la funcin en el punto. Sin embargo,
la forma funcional de

f (x)

dificulta en ocasiones el clculo de la derivada.

En estos casos es ms til emplear el mtodo de la secante.


El mtodo de la secante parte de dos puntos (y no slo uno como el mtodo de
Newton) y estima la tangente (es decir, la pendiente de la recta) por una
aproximacin de acuerdo con la expresin:

x
f ( x i1 ) f ( i)
f ( xi )(x i1 xi )
x i+1=x i

En general, el mtodo de la secante presenta las mismas ventajas y


limitaciones que el mtodo de Newton-Raphson explicado anteriormente.
DIAGRAMA DE FLUJO:

19

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:

20

FIAG-ESIC

METODO NUMERICOS EN MATLAB


CDIGO EN EL BOTN CALCULAR:
functionvarargout = pushbutton3_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'));
x0=str2double(get(handles.edit2,'string'));
x1=str2double(get(handles.edit3,'string'));
E=str2double(get(handles.edit4,'string'));
while abs(x1-x0)>E
x2=x1-(((x1-x0)*f(x1))/(f(x1)-f(x0)));
x0=x1;
x1=x2;
end
set(handles.edit5,'string',x2)
f=get(handles.edit1,'string');
f=inline(f);
ezplot(f), grid on
CDIGO EN EL BOTN SALIR:
function pushbutton3_Callback(hObject, eventdata, handles)
close(secante)
VENTANA DE DISEO y APLICACION:

21

FIAG-ESIC

METODO NUMERICOS EN MATLAB

INTERPOLACIN Y REGRESION
5. INTERPOLACION-POLINOMIO DE LAGRANGE:

22

FIAG-ESIC

METODO NUMERICOS EN MATLAB


TEORA:
Si

x0 , x1 , x2 , , xn

son

n+1

puntos distintos y

f ( x)

es una funcion

cuyos valores estan dados en esos puntos entonces existe un nico polinomio
P de grado a lo mas de grado n con la propiedad que

f ( x k ) =P( x k )

para

cada k=0, 1,2,n.


Este polinomio est dado por:
n

P ( x )= f ( x k ) Ln ,k (x ) polinomio de lagrange
k=0

Dnde:
n

Ln ,k ( x )=
i=0
ik

xxi
x k x i

Para un polinomio lineal la aproximacin es:

P ( x )=f ( x 0 ) L1,0 ( x ) +f ( x 1 ) L1,1 (x)


Dnde:

L1,0 ( x )=

xx1
xx 0
; L1,1 (x)=
x 0x 1
x 1x 0

Entonces:

P ( x )=

xx 1
xx 0
f ( x0 ) +
f ( x1 )
x 0x 1
x 1x 0

Para un polinomio de segundo grado est dado por:

P ( x )=f ( x 0 ) L2,0 ( x ) +f ( x 1 ) L2,1 ( x ) +f ( x 2 ) L2,2 ( x)


Dnde:

L1,0 ( x )=

23

xx 1
x 0x 1

)(

xx 2
x 0 x2

)
FIAG-ESIC

METODO NUMERICOS EN MATLAB

L2,1 ( x )=

L2,2 ( x )=

(
(

xx 0
x1 x0
xx 0
x2 x0

)(
)(

xx 2
x 1x 2
xx 1
x 2x 1

)
)

Entonces el polinomio para segundo grado es:

P ( x )=

xx 1
x 0x 1

)(

xx 2
xx 0
f ( x0 )+
x 0 x2
x 1x 0

)(

xx 2
xx 0
f ( x1 )+
x 1x 2
x 2x 0

)(

xx 1
f ( x2 )
x2 x1

Donde x es el valor a interpolar


DIAGRAMA DE FLUJO:

24

FIAG-ESIC

METODO NUMERICOS EN MATLAB

25

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:

26

CDIGO EN EL BOTN CALCULAR:


xa=str2num(get(handles.edit1,'string'));
f=str2num(get(handles.edit2,'string'));
xi=str2double(get(handles.edit3,'string'));
fun=inline(get(handles.edit4,'string'));
n=length(xa);
m=length(f);
if m==0
f=zeros(1,n);
for i=1:n
f(1,i)=fun(xa(1,i));
end
end
syms x
p=0;
for k=1:n
l=1;
for i=1:n
if k~=i
l=l*(x-xa(1,i))/(xa(1,k)-xa(1,i));
end
end
p=p+f(1,k)*l;
end
funcion=inline(p)

FIAG-ESIC

METODO NUMERICOS EN MATLAB


pto=funcion(xi)
ezplot(funcion,[xa(1,1),xi]),hold on
plot(xi,pto,'r*'),grid on
legend('f(x)','pto interpolado')
pan on
set(handles.edit6,'string',pto)
CDIGO EN EL BOTN LIMPIAR:
function pushbutton2_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit6,'string','');
CDIGO EN EL BOTN SALIR:
function pushbutton3_Callback(hObject, eventdata, handles)
close

VENTANA DE DISEO Y APLICACIN:

27

FIAG-ESIC

METODO NUMERICOS EN MATLAB

6. REGRESIN LINEAL:
TEORIA:
Supongamos que hemos medido un conjunto de pares de datos (xi, yi) en una
experiencia, por ejemplo, la posicin de un mvil en ciertos instantes de tiempo.
Queremos obtener una funcin y=f(x) que se ajuste lo mejor posible a los valores
experimentales. Se pueden ensayar muchas funciones, rectas, polinomios,
funciones potenciales o logartmicas.
Una vez establecido la funcin a ajustar se determina sus parmetros, en el caso
de un polinomio, sern los coeficientes del polinomio de modo que los datos
experimentales se desven lo menos posible de la frmula emprica.
La funcin ms sencilla es la funcin lineal y=ax+b que hemos tratado en la pgina
anterior. El procedimiento de ajustar los datos experimentales a una lnea recta se
denomina regresin lineal.

POLINOMIO APROXIMADOR
Queremos aproximar un polinomio de grado n, a un conjunto de m pares de datos
(xi,yi) de modo que n y m.
Sea el polinomio
P(x)=a0+a1x+a2x2+...anxn
Se calcula la cantidad:

Para obtener los valores de los coeficientes del polinomio aproximador se tienen
que determinar los valores de los coeficientes a0, a1, a2, ...an de forma que la
cantidad S tome un valor mnimo.
Hagamos las derivadas parciales de S respecto de a0, a1, a2, ...an iguales a cero

28

FIAG-ESIC

METODO NUMERICOS EN MATLAB

(1)

Obtenemos un sistema de n+1 ecuaciones con n+1 incgnitas, a0, a1, a2, ...an

DIAGRAMA DE FLUJO:

29

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
x=str2num(get(handles.edit1,'string'));
y=str2num(get(handles.edit2,'string'));
Sx=0;
Sy=0;
Sx2=0;
Sxy=0;
n=length(x);
for i=1:n
Sx=Sx+x(i); Sy=Sy+y(i); Sx2=Sx2+(x(i))^2; Sxy=Sxy+(x(i))*(y(i));
end
a=(n*Sxy-Sx*Sy)/(n*Sx2-(Sx)^2);
b=(Sy-a(1)*Sx)/n;
d=sprintf('%5.3f x + %5.3f',a,b);
set(handles.edit3,'string',d)
CDIGO EN EL BOTN GRAFICAR:
x=str2num(get(handles.edit1,'string'));
y=str2num(get(handles.edit2,'string'));
Sx=0;
Sy=0;
Sx2=0;
Sxy=0;
n=length(x);
for i=1:n
Sx=Sx+x(i); Sy=Sy+y(i); Sx2=Sx2+(x(i))^2; Sxy=Sxy+(x(i))*(y(i));
end
a=(n*Sxy-Sx*Sy)/(n*Sx2-(Sx)^2);
b=(Sy-a(1)*Sx)/n;
Y=a*x+b;
plot(x,Y),hold on
plot(x,y,'r*'),grid on
legend('f(x)','ptos iniciales')
pan on

30

FIAG-ESIC

METODO NUMERICOS EN MATLAB


CDIGO EN EL BOTN LIMPIAR
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
CDIGO EN EL BOTN SALIR:
close

VENTANA DE DISEO Y APLICACION:

Creado por Oscar mamani

7. REGRESIN POLINOMIAL :
TEORA:
Supongamos que se conocen los datos

,y

con

,x

, .., x n

,y

x
,

,y

x
, ..

nmeros reales distintos, y se desea

encontrar un polinomio:

31

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Pm=a0 +a1 x+ a2 x ++ am x , con m<n


Tal que:
2

S(a 0 , a 1 ,....., a m ) p m x k y k a 0 a 1 x k a 2 x 2k ,....., a m x mk y k


k 0

k 0

Sea mnima.

El grado m del polinomio

( x)

se puede escoger previamente con base

en algn resultado terico, alguna expectativa o por la aplicacin que se le


pretenda dar al polinomio. En cualquier caso estamos libres de elegir el grado
que parezca mejor. En muchos casos el grado ser uno y el polinomio obtenido
se llamar la recta que mejor se ajusta o la recta de mnimos cuadrados
para la tabla de datos.

a
S

Volviendo a la funcin

,a

, .., a

una condicin necesaria

para la existencia de un mnimo relativo de esta funcin es que las derivadas


parciales

de

a
S

,a

, .., a

con

respecto

a ,
j

j=0,1, 2, , m sean cero.


Resultan entonces las siguientes m+1 ecuaciones lineales en las incgnitas

,a

, .., a

n
S
2 a 0 a 1 x k a 2 x 2k ..... a m x mk y k 0
a 0 k 0
n
S
2 a 0 a 1 x k a 2 x 2k ..... a m x mk y k x k 0
a 1 k 0
n
S
2 a 0 a 1 x k a 2 x 2k ..... a m x mk y k x 2k 0
a 2 k 0

..........
n
S
2 a 0 a 1 x k a 2 x 2k ..... a m x mk y k x kj 0
a j k 0

............

n
S
2 a 0 a 1 x k a 2 x 2k ..... a m x mk y k x mk 0
a m k 0

Si en las ecuaciones anteriores cancelamos el 2, desarrollamos los parntesis y


usamos que

32

FIAG-ESIC

METODO NUMERICOS EN MATLAB


n

a
Obtenemos:

n 1 a 0

k 0

n 1 a 0 x k a 1 x 2k a 2 ..... x mk a m
n

x
k 0
n

x
k 0

k 0
n

a0

k 0
n

a0

k 0

a1

x
k 0
n

x
k 0

.
.

x kj a 0

k 0

4
k

a 2 .....

a 2 .....

k 0
n

k 0
n

x
k 0

m 1
k

k 0
n

k 0
n

k 0

...

x mk j a m

k 0

x 1k m a 1

k 0

x 2k m a 2 .....

k 0

x mk m a m

k 0

am

x 2k j a 2 .....

k 0

yk

2
k

yk

...

:::

am

m2
k

x 1k j a 1

k 0

x mk a 0

k 0

k 0
n

a1

x
k 0
n

x
k 0

j
k

yk

m
k

yk

Este es un SEL de m+1 ecuaciones lineales en las m+1 incgnitas a 0, a1, ..,
am, que se llama Sistema de Ecuaciones Normales. Este sistema de
ecuaciones normales se puede escribir en forma simplificada como sigue:
m

i 0

k 0

k 0

a i x ik j x kj y k

con j 0,1,....,.m

Estas ecuaciones se pueden reproducir a partir de:

p m x k a 0 a 1 x k a 2 x 2k ,....., a m x mk y k
Multiplicando a ambos lados por

x ij , j=0, 1, , m,

a 0 x kj a 1 x k x kj a 2 x 2k x kj ,....., a m x mk x kj y k x kj
a 0 x kj a 1 x 1k j a 2 x 2k j ,....., a m x mk j x kj y k

Sumando sobre k
n

k 0

k 0

k 0

k 0

k 0

a 0 x kj a 1 x 1k j a 2 x 2k j ..... a m x mk j x kj y k

33

con j 0,1,2,....., m

FIAG-ESIC

METODO NUMERICOS EN MATLAB

DIAGRAMA DE FLUJO:

34

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
function pushbutton1_Callback(hObject, eventdata, handles)
x=str2num(get(handles.edit1,'string'));
y=str2num(get(handles.edit2,'string'));
n=str2double(get(handles.edit3,'string'));
[m p]=size(x);
for i=1:n+1
for j=1:n+1
S=0;
for k=1:m
S=S+(x(k))^(i+j-2);
end
M(i,j)=S;
end
end
for i=1:n+1
SB=0;
for k=1:m
SB=SB+((x(k))^(i-1))*y(k);
end
B(i,1)=SB;
end
M=M;
B=B;
A=inv(M)*B;
A=A';
set(handles.edit4,'string',num2str(A));
CDIGO EN EL BOTN GRAFICAR:
function pushbutton2_Callback(hObject, eventdata, handles)
x=str2num(get(handles.edit1,'string'));
y=str2num(get(handles.edit2,'string'));
n=str2double(get(handles.edit3,'string'));
temp=str2num(get(handles.edit4,'string'));
[m p]=size(x);
for i=1:n+1
Px(i)=temp(n-i+2);
end
Px=Px
dom=min(x)-1.5:.2:max(x)+1.5;

35

FIAG-ESIC

METODO NUMERICOS EN MATLAB


ran=polyval(Px,dom);
valor=polyval(Px,4)
plot(x,y,'bo',dom,ran,'r-'),grid on
CDIGO EN EL BOTN LIMPIAR:
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
CDIGO EN EL BOTN SALIR:
function pushbutton4_Callback(hObject, eventdata, handles)
close
VENTANA DE DISEO Y APLICACION:

36

FIAG-ESIC

METODO NUMERICOS EN MATLAB

UNIDAD II

SISTEMAS DE ECUACIONES E INTEGRACIONES NUMERICAS


8. MTODO DE GAUSS - JORDAN
TEORA:
Sea un sistema de ecuaciones lineales de la forma:

a11 x 1 +a12 x 2 +a 13 x 3+ +a1 n x n=b1


a21 x 1 +a22 x2 +a 23 x 3+ +a 2n x n=b 2
a31 x 1 +a32 x 2 +a 33 x 3+ +a3 n x n=b3

an 1 x 1 +an 2 x2 +a n 3 x 3 ++a nn x n=b n


Se trata de un sistema de n ecuaciones con n incgnitas, x1, x2, ..., xn. Los
elementos aij y bi son nmeros reales fijados.
El sistema de ecuaciones se puede escribir, empleando una muy til
representacin matricial, como:

a 11
a21
a31

a 11

a12
a22
a32

a12

a13
a23
a33

a13

Es decir

a1 n
a2 n
a3 n

a1 n

x1

b1

)( ) ( )
x2
b2
x3 = b3

xn
bn

A X=B

Donde A es la matriz de coeficientes, X es el vector incgnitas y B es el vector


trminos independientes.
PROCEDIMIENTO:
Crear la matriz cuyos elementos son los de la matriz A y el vector B. A es la
matriz se le denomina la matriz aumentada.

37

FIAG-ESIC

METODO NUMERICOS EN MATLAB

a11
a21
a31

a n1

a 12
a 22
a 32

an 2

a13
a23
a33

an3

|)

a 1n b1
a 2 n b2
a 3 n b3

ann bn

Matriz aumentada

Mediante transformaciones elementales de filas en la matriz aumentada, los


elementos de la matriz de coeficientes A debe transformarse en la matriz
identidad y los elementos que estn en la posicin del vector de trminos
independientes B, ser la solucin del sistema.

( |)
1
0
0

0
1
0

0
0
1

0b 1

0b 2
0 b 3

1 b n

Matriz transformada

Y las races del sistema de ecuaciones son:

x 1=b 1 ; x 2=b 2 ; x 3=b3 ; ; x n=b n

El proceso, requiere de

38

n3 2 n
+n
2
2

multiplicaciones y

n3 n

2 2

sumas.

FIAG-ESIC

METODO NUMERICOS EN MATLAB

DIAGRAMA DE
FLUJO:

39

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
functionvarargout = pushbutton1_Callback(h, eventdata, handles,
varargin)
A=str2num(get(handles.edit1,'string'));
[m,n]=size(A);
for i=1:m
divisor=A(i,i);
for j=i:n
A(i,j)=A(i,j)/divisor;
end
for k=1:m
if i~=k
pivote = A(k,i);
for j=i:n
A(k,j)=A(k,j)- pivote*A(i,j);
end
end
end
end
for i=1:m
x(i)=A(i,n);
end
x=x';

40

FIAG-ESIC

METODO NUMERICOS EN MATLAB


t=1:m;
t=t';
cadena='';
for t=1:m
cad=sprintf('x%d=%6.4f',t,x(t));
cadena=[cadena;cad];
end
set(handles.edit2,'string',cadena);
CDIGO EN EL BOTN SALIR:
functionvarargout = pushbutton3_Callback(h, eventdata, handles,
varargin)
close

VENTANA DE DISEO Y APLICACIN:

41

FIAG-ESIC

METODO NUMERICOS EN MATLAB

42

FIAG-ESIC

METODO NUMERICOS EN MATLAB

9. MTODO DE GAUSS SEIDEL


TEORA:
Mtodo iterativo que su utiliza para resolver sistema de ecuaciones de la forma:

a11 x 1 +a12 x 2 +a 13 x 3+ +a1 n x n=b1


a21 x 1 +a22 x2 +a 23 x 3+ +a 2n x n=b 2
a31 x 1 +a32 x 2 +a 33 x 3+ +a3 n x n=b3

an 1 x 1 +an 2 x2 +a n 3 x 3 ++a nn x n=b n


Que matricialmente se puede escribir como A X=B, supongamos que

aii 0, i=1,2,3 . n Despejamos los X

x 1=( b1 a12 x 2a13 x 3a1 n x n) /a11


x 2=( b2 a21 x 1a23 x 3a 2n x n ) /a22
x 3=( b 3a31 x 1a32 x 2+ a3 n x n ) /a33

El proceso se inicia dando un valor inicial para los puntos


podra usar, por ejemplo la solucin trivial

x i ; i=1,2,3 . n se

x 1=x 2=x3 ==xn =0

si este

fuera el caso se tendra que:

x 1=b1 /a11
x 2=( b 2a21 ( b1 /a11 ) ) /a 22
x 3=( b3a31 ( b 1 /a11 )a32 ( b2a21 ( b 1 /a11 ) ) /a22) /a 33
..
Los

x 1 , x 2 ,... , x n son los nuevos valores inciales que sern utilizados en

una segunda iteracin.


La convergencia puede definirse mediante

43

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Ex =
i

x i j xi j1
xi j

100<T

Dnde:

Ex

: Error relativo porcentual dela

xi

raz

j : Iteracin actual
j1 : Iteracin anterior
T : Tolerancia prefijada
RE ARREGLO DE ECUACIONES
El proceso de gauss - Seidel converge si la matriz coeficientes cada elemento
de la diagonal es el mayor en valor absoluto que la suma de todos los dems
elementos de la misma fila o columna .Es decir se asegura la convergencia s.
n

i=1
ji

i=1
j i

|aii|> aij |aii|> a ji

DIAGRAMA DE FLUJO:

44

FIAG-ESIC

METODO NUMERICOS EN MATLAB

45

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO EN EL BOTN CALCULAR:

46

FIAG-ESIC

METODO NUMERICOS EN MATLAB

function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)


maxite=str2double(get(handles.edit1,'string'));
v=str2num(get(handles.edit2,'string'));
a=str2num(get(handles.edit3,'string'));
b=str2num(get(handles.edit4,'string'));
[n,n]=size(a);
cad1='';
for k=1:maxite
for i=1:n
x(i)=v(i);
end
for i=1:n
s=0;
for j=1:n
if j~=i
s=s+a(i,j)*x(j);
end
end
v(i)=(b(i)-s)/a(i,i);
x(i)=v(i);
end
for t=1:n
cad2=sprintf('x%d=%10.8f ',t,x(t));
cad1=[cad1,cad2];
end
cad2=sprintf('\n',t);
cad1=[cad1,cad2];
end
set(handles.edit5,'string',cad1);
CDIGO EN EL BOTN SALIR:
function varargout = pushbutton3_Callback(h, eventdata, handles, varargin)
close

CDIGO EN EL BOTN LIMPIAR:


set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');

VENTANA DE DISEO Y APLICACIN:

47

FIAG-ESIC

METODO NUMERICOS EN MATLAB

SISTEMA DE ECUACIONES NO LINEALES


10. METODO DE NEWTON:
TEORA:

48

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Estudiaremos funciones vectoriales f : R^n R^m cuando n = m.


Si se supone que f C y en un punto xk de un proceso iterativo tendente a
resolver f(x) = 0 se aproxima la funcin mediante el modelo, Mk(xk), que dene
el desarrollo en serie de Taylor alrededor de ese punto, truncndolo a partir de
los trminos de segundo orden, se tiene que:
Mk(xk) = f(xk) + J(xk)(x xk), donde J(xk) es la matriz Jacobiana del sistema
en xk:

Si se utiliza esa aproximacin lineal de la funcin y se resuelve el sistema de


ecuaciones lineales que dene:
f(xk) + J(xk)(x xk) = 0
Su solucin:
x = xk J(xk) ^(1)f(xk)
*Determinara un nuevo punto del proceso iterativo.
*La relacin de recurrencia del mtodo de Newton-Raphson para sistemas de
ecuaciones no lineales es pues:

El paso de Newton es x(k+1) xk: una aproximacin de x xk.


Cada ecuacion, fi: R^n R, se reemplaza o aproxima por el hiperplano
tangente en xk a la curva que dene esa fi.
El algoritmo de Newton-Raphson para resolver sistemas de ecuaciones
no lineales es el siguiente:

CODIGO DEL PROGRAMA:

CDIGO EN EL BOTN CALCULAR:

function pushbutton1_Callback(hObject, eventdata, handles)

49

FIAG-ESIC

METODO NUMERICOS EN MATLAB


f1x=get(handles.edit1,'string');
f2x=get(handles.edit2,'string');
f3x=get(handles.edit3,'string');
n=str2double(get(handles.edit4,'string'));
syms x1 x2 x3
f1=f1x;
f2=f2x;
f3=f3x;
g1=diff(f1,x1);
g2=diff(f1,x2);
g3=diff(f1,x3);
g4=diff(f2,x1);
g5=diff(f2,x2);
g6=diff(f2,x3);
g7=diff(f3,x1);
g8=diff(f3,x2);
g9=diff(f3,x3);
a=1;
b=1;
c=1;
cad=;
for i=1:n
A=zeros(3,3);
B=zeros(3,1);
A(1,1)=subs(g1,{x1,x2,x3},{a,b,c});
A(1,2)=subs(g2,{x1,x2,x3},{a,b,c});
A(1,3)=subs(g3,{x1,x2,x3},{a,b,c});
A(2,1)=subs(g4,{x1,x2,x3},{a,b,c});
A(2,2)=subs(g5,{x1,x2,x3},{a,b,c});
A(2,3)=subs(g6,{x1,x2,x3},{a,b,c});
A(3,1)=subs(g7,{x1,x2,x3},{a,b,c});
A(3,2)=subs(g8,{x1,x2,x3},{a,b,c});
A(3,3)=subs(g9,{x1,x2,x3},{a,b,c});
B(1,1)=-subs(f1,{x1,x2,x3},{a,b,c});
B(2,1)=-subs(f2,{x1,x2,x3},{a,b,c});
B(3,1)=-subs(f3,{x1,x2,x3},{a,b,c});
H=inv(A)*B;
a=a+H(1,1);
b=b+H(2,1);
c=c+H(3,1);
cadena=sprintf(' %d.x1=%7.3f x2=%7.3f
cad=[cad,cadena];
end
set(handles.edit6,'string',a);
set(handles.edit7,'string',b);
set(handles.edit8,'string',c);
set(handles.edit9,'string',cad);

x3=%7.3f\n',i,a,b,c);

CDIGO EN EL BOTN LIMPIAR:


function pushbutton2_Callback(hObject, eventdata, handles)

50

FIAG-ESIC

METODO NUMERICOS EN MATLAB


set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
set(handles.edit8,'string','');
set(handles.edit9,'string','');

CDIGO EN EL BOTN SALIR:


function pushbutton3_Callback(hObject, eventdata, handles)
close

VENTANA DE DISEO Y APLICACION:

INTEGRACIN NUMRICA
11. REGLA DEL TRAPECIO:
TEORA:

51

FIAG-ESIC

METODO NUMERICOS EN MATLAB

y=f ( x)

Este mtodo resulta de sustituir la funcin

P ( x )=a0 +a1 x

primer grado

en

por un polinomio de

[ a , b ] =[ x 0 , x 1 ] al polinomio P( x) se

le puede representar mediante un polinomio

P( x)

se le puede

representar mediante un polinomio de Lagrange, es decir:


x1

P ( x ) dx=
x0

( x x1 )
( xx 0 )
f ( x0 ) +
f ( x1 ) dx
( x 0x 1 )
( x 1x 0 )
x1

f ( x ) dx=
x0

Resolviendo:

f ( x ) dx=

h
[ f (x 0)f (x 1) ] , donde h=x 1x 0
2
b

Generalizando:
x2

x1

xn

f ( x ) dx+ f ( x ) dx ++ f ( x ) dx= f ( x ) dx
x1

x0

x n1

x1

f ( x ) dx+
x0

Aplicando la regla del trapecio a c/u de las integrales se tiene:

f ( x ) dx=lim f (x n) x k
a

n k=1

DIAGRAMA DE FLUJO:

52

FIAG-ESIC

METODO NUMERICOS EN MATLAB

53

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
functionvarargout = pushbutton4_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'));
a=str2num(get(handles.edit2,'string'));
b=str2num(get(handles.edit3,'string'));
n=str2double(get(handles.edit4,'string'));
h=(b-a)/n;
s=f(a)+f(b);
for i=2:n
x(i)=a+(i-1)*h;
s=s+2*f(x(i));
end
I=s*(h/2);
set(handles.edit5,'string',I);
CDIGO EN EL BOTN GRAFICAR:
functionvarargout = pushbutton5_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'));
a=str2num(get(handles.edit2,'string'));
b=str2num(get(handles.edit3,'string'));
n=str2double(get(handles.edit4,'string'));
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=f(x(i));
end
x=[x,b,a,a];
y=[y,0,0,f(a)];
fill(x,y,[0.8 0.8 0.9])
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=f(x(i));
end
hold on
ezplot(f,[min(x):0.2:max(x)])

54

FIAG-ESIC

METODO NUMERICOS EN MATLAB


plot(x,y,'og')
plot(x,y,'g')
CDIGO EN EL BOTN SALIR:
function varargout = pushbutton3_Callback(h, eventdata, handles, varargin)
close
CDIGO EN EL BOTN LIMPIAR:
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');

VENTANA DE DISEO Y APLICACION:

55

FIAG-ESIC

METODO NUMERICOS EN MATLAB

12. REGLA DE SIMPSON 1/3:


TEORA:
La regla de Simpson de 1/3 resulta cuando se sustituye la funcin y=f(x) por
un polinomio de segundo grado es decir:
b

f ( x ) dx P ( x ) dx donde P ( x )=a0 + a1 x +a2 x 2


a

En el intervalo

[ a , b ] =[ x 0 , x2 ] al polinomio

P (x )

se le puede representar

por un polinomio de LaGrange de segundo orden


Es decir:
b

f ( x ) dx [ f ( x 0 ) L2,0 ( x ) +f ( x 1 ) L2,1 ( x ) +f ( x 2 ) L2,2 (x)] dx


a

x1

f ( x ) dx
a

x0

[( )( ) ( )( ) ( )( ) ]
xx 1
x 0x 1

xx 2
xx 0
f ( x0 )+
x 0 x2
x 1x 0

xx 2
xx 0
f ( x1 )+
x 1x 2
x 2x 0

xx 1
f ( x 2 ) dx
x2 x1

Resolviendo la integral se obtiene:


b

f ( x ) dx h3 [ f ( x 0 ) +4 f ( x 1 ) +f ( x 2 ) ] , donde h=
a

x 2x 0
2

GENERALIZANDO PARA ''n'' INTERVALOS

56

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Los intervalos se toman de dos en dos:


x2

x4

x6

xn

f ( x ) dx= f ( x ) dx+ f ( x ) dx + f ( x ) dx+ + f ( x ) dx


x0

x2

x4

x n2

Aplicando la regla de Simpson de 1/3 para cada integral de tiene:


b

f ( x ) dx h3 [ f ( x 0 ) +4 f ( x 1 ) +2 f ( x 2 )+ 4 f ( x 3 )+ 2 f ( x 4 ) ++ 2 f ( xn2 ) + 4 f ( x n1 ) + f ( xn ) ]
a

Dnde:

h=

ba x nx 0
=
; donde n es multiplo de 2
n
n

x i=x 0+ ih ; i=1,2,3 n

DIAGRAMA DE FLUJO:

57

FIAG-ESIC

METODO NUMERICOS EN MATLAB

58

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:

CDIGO EN EL BOTN OK:


functionvarargout = pushbutton1_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'));
a=str2double(get(handles.edit2,'string'));
b=str2double(get(handles.edit3,'string'));
n=str2double(get(handles.edit4,'string'));
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
end
if mod(n,2)==0
s=0;
for i=3:2:n+1

59

FIAG-ESIC

METODO NUMERICOS EN MATLAB


s=s+f(x(i-2))+4*f(x(i-1))+f(x(i))
end
I=(h/3)*s
set(handles.edit5,'string',I);
end
CDIGO EN EL BOTN GRAFICAR:
functionvarargout = pushbutton2_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'));
a=str2double(get(handles.edit2,'string'));
b=str2double(get(handles.edit3,'string'));
n=str2double(get(handles.edit4,'string'));
h=(b-a)/n;
s=f(a)+f(b);
for i=1:n+1
x(i)=a+((i-1)*h);
y(i)=f(x(i));
end
x=[x,b,a,a];
y=[y,0,0,f(a)];
fill(x,y,[0.8 0.4 0.9])
for i=1:n+1
x(i)=a+((i-1)*h);
y(i)=f(x(i));
line([x(i),x(i)],[0,f(x(i))]);
end
hold on
ezplot(f,[min(x):0.2:max(x)])

CDIGO EN EL BOTN LIMPIAR:


function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');

CDIGO EN EL BOTN SALIR:


function varargout = pushbutton4_Callback(h, eventdata, handles, varargin)
close
VENTANA DE DISEO Y APLICACIN:

60

FIAG-ESIC

METODO NUMERICOS EN MATLAB

13. REGLA DE SIMPSON DE 3/8:


TEORA:
La regla

y=f (x)
b

de Simpson de 3/8 resulta

cuando se sustituye la funcin

por un polinomio de tercer grado es decir:


b

f ( x ) dx P ( x ) dxdonde P ( x )=a0 +a1 x+ a2 x 2 +a3 x 3


a

61

FIAG-ESIC

METODO NUMERICOS EN MATLAB

En el intervalo

[ a , b ] =[x 0 , x2 ] al polinomio P ( x ) se le puede representar

por un polinomio de LaGrange de tercer orden.


Es decir:
b

b =x 3

f ( x ) dx [ f ( x 0 ) L3,0 ( x ) + f ( x1 ) L3,1 ( x ) + f ( x 2) L3,2 ( x ) + f ( x3 ) L3,3 (x )] dx


a

a =x 0

Resolviendo la integral se obtiene:


b

f (x) dx 38h [ f ( x 0 ) +3 f ( x 1 ) +3 f ( x 2 ) + f ( x 3 ) ] , dondeh=


a

x 2x 0
3

DIAGRAMA DE FLUJO:

62

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
functionvarargout = pushbutton3_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'))
a=str2double(get(handles.edit2,'string'))
b=str2double(get(handles.edit3,'string'))
n=str2double(get(handles.edit4,'string'))
h=(b-a)/n
for i=1:n+1
x(i)=a+(i-1)*h
end
if rem(n,3)==0
s=0
for i=3:n+1:3
s=s+f(x(i-2))+3*f(x(i-1))+3*f(x(i))+f(x(i-1))
end
I=((3*h)/8)*s;
set(handles.edit5,'string',I)
end
CDIGO EN EL BOTN GRAFICAR:
functionvarargout = pushbutton4_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'))
a=str2double(get(handles.edit2,'string'))
b=str2double(get(handles.edit3,'string'))
n=str2double(get(handles.edit4,'string'))
h=(b-a)/n;
s=f(a)+f(b)
for i=1:n+1

63

FIAG-ESIC

METODO NUMERICOS EN MATLAB


x(i)=a+((i-1)*h)
y(i)=f(x(i));
end
x=[x,b,a,a]
y=[y,0,0,f(a)]
fill(x,y,[0.6 0.8 0.4])
for i=1:n+1
x(i)=a+((i-1)*h)
y(i)=f(x(i));
line([x(i),x(i)],[0,f(x(i))])
end
hold on
ezplot(f,[min(x):0.2:max(x)]);
CDIGO EN EL BOTN LIMPIAR:
function pushbutton5_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');
CDIGO EN EL BOTN SALIR:
function pushbutton6_Callback(hObject, eventdata, handles)
close
VENTANA DE DISEO Y APLICACIN:

64

FIAG-ESIC

METODO NUMERICOS EN MATLAB

14.

INTEGRALES MLTIPLES
TEORA:
Para el clculo de integrales de funciones de varia variables se pueden usar
las reglas ya estudiadas como la regla del trapecio, regla de Simpson 1/3 y 3/8
son tiles para resolver integrales dobles y triples.
En esta ocasin usaremos Simpson de 1/3 para el clculo de una integral
doble de la forma:
.

f ( x , y ) dA
R

Dnde:

R= { ( x , y ) /a x b ; c y d }
R= { ( x , y ) /a x b ; c g ( x ) d }
Para aproximar la solucin de la integral
b g2 ( x )

f ( x , y ) dy dx
a g1 ( x )

Utilizando la regla de Simpson 1/3

donde h 1=

x 2x 0
g ( x )g1 (x )
; h 2= 2
2
2

Por lo tanto:

65

FIAG-ESIC

METODO NUMERICOS EN MATLAB


b g2 ( x )

x2

f ( x , y ) dy dx= w ( x ) dx= 31 [ w ( x 0 ) + 4 w ( x 1 ) + w ( x2 ) ]
a g1 ( x )

x0

Dnde:

w ( x )=

h2
[ f (x , g1 ( x ))+ 4 f (x , g1 ( x ) +h2 (x ))+ f (x , g2 ( x ))]
3

DIAGRAMA DE FLUJO
INICIO
LEER
f(x,y), g1(x), g2(x), a, b
h=(b-a)/2
x0=a
s=0
i=1:3
h2=(g2(x0)-g1(x0))/2
w(i)=(h2/3)*(f((x0),g1(x0))+4*f(x0,g1(x0)+h2)+f(x0,g2(x0)))
x0=x0+h

I=(h/3)*(w(1)+4*w(2)+w(3))
ESCRIBIR
I
FIN

66

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN CALCULAR:
function pushbutton1_Callback(hObject, eventdata, handles)
f=inline(get(handles.edit1,'string'),'x','y');
g1=inline(get(handles.edit2,'string'));
g2=inline(get(handles.edit3,'string'));
a=str2double(get(handles.edit4,'string'));
b=str2double(get(handles.edit5,'string'));
n=str2double(get(handles.edit7,'string'));
h=(b-a)/n;
x0=a;
for i=1:n+1
h2=(g2(x0)-g1(x0))/2;
w(i)=(h2/3)*(f((x0),g1(x0))+4*f(x0,g1(x0)+h2)+f(x0,g2(x0)));
x0=x0+h;
end
I=0;
d=1;
for i=1:n/2
I=I+(h/3)*(w(d)+4*w(d+1)+w(d+2));
d=d+2;
end
set(handles.edit6,'string',I);
CDIGO EN EL BOTN GRAFICAR:
function pushbutton2_Callback(hObject, eventdata, handles)
f=get(handles.edit1,'string');
f1=inline(f,'x','y');
ezmesh(f1);
grid on
CDIGO EN EL BOTN LIMPIAR:
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');

67

FIAG-ESIC

METODO NUMERICOS EN MATLAB


set(handles.edit5,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
CDIGO EN EL BOTN SALIR:
function pushbutton4_Callback(hObject, eventdata, handles)
close

VENTANA DE DISEO Y APLICACIN:

68

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Creado por Oscar mamani

UNIDAD III

ECUACIONES DIFERENCIALES ORDINARIAS


15. METODO DE EULER:
TEORA:
Este mtodo consiste en dividir el intervalo
longitud

'h';

h=

x 0 , x 1 , x 2 , .. , x n =x f
y ( x0 ) = y 0

( ba )
n
donde

[a ,b ]

en n subintervalos de

,de manera que se obtiene los n+ 1 puntos

x i=x 0+ ih ; i=1,2,3 n

representada por el punto

P0=( x 0 , y 0 )

la condicin

inicial

por donde pasa la

curva solucin, donde :

dy
dx

69

( x 0 , y0 )

=f ( x 0 , y 0 )

FIAG-ESIC

METODO NUMERICOS EN MATLAB

FORMULADE EULER

y i+1= yi +h ( x i , y i) , i=1,2,3 n
Es decir, se genera una sucesin de aproximacin:

y 1= y 0 +h f ( x 0 , y 0 )
y 2= y 1 +h f ( x 1 , y 1 )
y 3= y 2 +h f ( x 2 , y 2)

y n= y n1 +h f ( x n1 , y n1)

DIAGRAMA DE FLUJO:

70

FIAG-ESIC

METODO NUMERICOS EN MATLAB

71

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN OK:
function pushbutton1_Callback(h, eventdata, handles, varargin)
f1=get(handles.edit1,'string');
x0=str2double(get(handles.edit2,'string'));
y0=str2double(get(handles.edit6,'string'));
n=str2double(get(handles.edit3,'string'));
b=str2double(get(handles.edit4,'string'));
f=inline(f1,'x','y');
h=(b-x0)/n;
m=sprintf('x(0)=%5.3f y(0)=%5.3f\n',x0,y0);
cad=[m];
for i=1:n
y0=y0+h*f(x0,y0);
x0=x0+h;
cadena=sprintf('x(%d)=%6.3f y(%d)=%6.3f\n',i,x0,i,y0);
cad=[cad,cadena];
end
set(handles.edit5,'string',y0);
set(handles.edit17,'string',cad);
CDIGO EN EL BOTN LIMPIAR:
functionvarargout = pushbutton3_Callback(h, eventdata, handles, varargin)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
CDIGO EN EL BOTN GRAFICAR:
functionvarargout = pushbutton2_Callback(h, eventdata, handles, varargin)

72

FIAG-ESIC

METODO NUMERICOS EN MATLAB


f1=get(handles.edit1,'string');
x0=str2double(get(handles.edit2,'string'));
y0=str2double(get(handles.edit6,'string'));
n=str2double(get(handles.edit3,'string'));
b=str2double(get(handles.edit4,'string'));
f=inline(f1,'x','y');
h=(b-x0)/n;
X=[x0];
Y=[y0];
for i=1:n
y0=y0+h*f(x0,y0);
x0=x0+h;
X=[X,x0];
Y=[Y,y0];
end
plot(X,Y,'r*'),grid on
legend('f(x)');
CDIGO EN EL BOTN SALIR:
function pushbutton4_Callback(hObject, eventdata, handles)
close

VENTANA DE DISEO Y APLICACIN:

73

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Creado por Oscar mamani

16. METODO RUNGE KUTTA DE CUARTO ORDEN :


TEORA:
El mtodo de Runge-Kutta es un mtodo genrico de resolucin numrica de
ecuaciones diferenciales. Este conjunto de mtodos fue desarrollado
alrededor del ao 1900 por los matemticos C. Runge y M. W. Kutta.
Este mtodo puede ser usado para resolver un nmero grande de ecuaciones
diferenciales.
Dada la ecuacin diferencial ordinaria
iniciales

y ( x 0)= y 0

entonces por el

y'=

dy
=f ( x , y )
dx

con condiciones

segundo teorema fundamenta del

clculo se tiene:
'

y dx = y ( x n+1) y (x n)
x n+1

xn

74

FIAG-ESIC

METODO NUMERICOS EN MATLAB

[ x n , x n+1 ]

Para aplicar la regla de Simpson de 1/3 a

se le dividi en dos

intervalos es decir:
Entonces

y ' dx =

h /2 '
h
y ( x n ) + 4 y ' x n+ + y ' ( x n+1 )
3
2

xn+ 1

xn

Al trmino

4 y' x n +

h
2

se le expresa como:

y ' x n+

para aproximar la pendiente de

y ( xn +1 )= y ( x n ) +

h
2

2 y' x n +

h
h
+2 y ' x n+
2
2

) (

en el punto promedio

h '
h
h
y ( x n ) +2 y' x n + +2 y ' x n+ + y ' ( x n+1 )
6
2
2

) (

( x + h2 )
n

Pero

y ' =f ( x n , y n )

Por EULER se tiene que:

y ( xn +1 )= y ( x n ) +h f ( x n , y n )

Hacemos cambios de variables:

k 1= y ' ( x n ) entonces

Hagamos

k 1=f ( x n , y n )
x

h
k 2= y ' x n +
2

Hagamos

y xn +

entonces

por

euler

h
h
= y ( x n ) + f ( x n , y n ) entonces
2
2

h
h
k 2=f x n + , y n + k 1
2
2

75

h
y ( n+ )
2
h
xn + ,
2
k 2=f

)
FIAG-ESIC

METODO NUMERICOS EN MATLAB

k 3 = y ' xn +

Hagamos

y xn +

h
2

entonces

h
y ( n+ )
2
h
xn + ,
2
k 3=f

por

euler

por

euler

h
h
= y ( x n ) + y ' ( x n , y n ) entonces:
2
2

h
h
k 3 =f x n + , y n + k 2
2
2

)
x

Hagamos

k 4= y ' ( xn +1 )

y ( xn +1 ) = y ( x n) + h y ' x n +

entonces

h
2

h
y ( n+ )
2
h
xn + ,
2
k 4=f

entonces:

k 4=f ( xn +1, y 0 +h k 3 )
Por lo tanto:

y ( xn +1 ) = y n +

h
[ k +2 k 2 +3 k 3+ k 4 ]
6 1

Dnde:

h=

76

x n+1 xn
; mes el numero de intervalos .
m

FIAG-ESIC

METODO NUMERICOS EN MATLAB

DIAGRAMA DE FLUJO:

77

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CDIGO DE PROGRAMA:
CDIGO EN EL BOTN OK:
function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)
f=inline(get(handles.edit1,'string'),'x','y');
n=str2double(get(handles.edit2,'string'));
x0=str2double(get(handles.edit3,'string'));
y0=str2double(get(handles.edit4,'string'));
xn=str2double(get(handles.edit6,'string'));
h=(xn-x0)/n;
cad=[];
cad2=[];
for i=1:n
K1=f(x0,y0);
K2=f(x0+h/2,y0+K1*h/2);
K3=f(x0+h/2,y0+K2*h/2);
K4=f(x0+h,y0+K3*h);
y0=y0+h*(K1+2*K2+2*K3+K4)/6;
yn=sprintf('%d: %5.4f\n',i,y0);
cad=[cad,yn];
x0=x0+h;
xn=sprintf('%d: %5.4f\n',i,x0);

78

FIAG-ESIC

METODO NUMERICOS EN MATLAB


cad2=[cad2,xn];
end
set(handles.edit5,'string',y0);
set(handles.edit8,'string',cad2);
set(handles.edit9,'string',cad);
CDIGO EN EL BOTN GRAFICAR:
function pushbutton2_Callback(hObject, eventdata, handles)
f=inline(get(handles.edit1,'string'),'x','y');
n=str2double(get(handles.edit2,'string'));
x0=str2double(get(handles.edit3,'string'));
y0=str2double(get(handles.edit4,'string'));
xn=str2double(get(handles.edit6,'string'));
h=(xn-x0)/n;
cad1=[y0];
cad3=[x0];
for i=1:n
K1=f(x0,y0);
K2=f(x0+h/2,y0+K1*h/2);
K3=f(x0+h/2,y0+K2*h/2);
K4=f(x0+h,y0+K3*h);
y0=y0+h*(K1+2*K2+2*K3+K4)/6;
cad1=[cad1,y0];
x0=x0+h;
cad3=[cad3,x0];
end
plot(cad3,cad1,'*b')
grid on;
legend('Y');
pan on
CDIGO EN EL BOTN LIMPIAR:
function varargout = pushbutton3_Callback(h, eventdata, handles, varargin)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
set(handles.edit8,'string','');
CDIGO EN EL BOTN SALIR:
function varargout = pushbutton4_Callback(h, eventdata, handles, varargin)
close

79

FIAG-ESIC

METODO NUMERICOS EN MATLAB

VENTANA DE DISEO Y APLICACIN

17.

Creado por Oscar

SISTEMA DE ECUACIONES DIFERENCIALES ORDINARIAS :


TEORIA:
Un sistema de ecuaciones diferenciales de primer orden se presenta en forma
general como:
Y1=f1(x1, Y1, Y2, Y3,, Yn)
Y2= f2(x1, Y1, Y2, Y3,, Yn)
Y3= f3(x1, Y1, Y2, Y3,, Yn)
.
.
Yn= fn(x1, Y1, Y2, Y3,, Yn)
Para dar la solucin a este sistema necesitamos las n condiciones iniciales en un
valor x.
Por ejemplo tengamos un sistema de ecuaciones:
Y1=f1(x1, Y1, Y2, Y3)

80

Y1(x0)=d1

FIAG-ESIC

METODO NUMERICOS EN MATLAB


Y2= f2(x1, Y1, Y2, Y3)
Y3= f3(x1, Y1, Y2, Y3)
h=(xn-x0)/N

Y2(x0)=d2
Y3(x0)=d3

Para hallar la solucin de un sistema de ecuaciones se puede utilizar el mtodo de


Runge kutta 4to orden.
Donde se tiene:
k1,1=f1(x0,y1,y2,y3);
k1,2=f2(x0,y1,y2,y3);
k1,3=f3(x0,y1,y2,y3);
k2,1=f1(x0+h/2,y1+(h/2)*k1,1,y2+(h/2)*k1,2,y3+(h/2)*k1,3);
k2,2=f2(x0+h/2,y1+(h/2)*k1,1,y2+(h/2)*k1,2,y3+(h/2)*k1,3);
k2,3=f3(x0+h/2,y1+(h/2)*k1,1,y2+(h/2)*k1,2,y3+(h/2)*k1,3);
k3,1=f1(x0+h/2,y1+(h/2)*k2,1,y2+(h/2)*k2,2,y3+(h/2)*k2,3);
k3,2=f2(x0+h/2,y1+(h/2)*k2,1,y2+(h/2)*k2,2,y3+(h/2)*k2,3);
k3,3=f3(x0+h/2,y1+(h/2)*k2,1,y2+(h/2)*k2,2,y3+(h/2)*k2,3);
k4,1=f1(x0+h,y1+h*k3,1,y2+h*k3,2,y3+h*k3,3);
k4,2=f2(x0+h,y1+h*k3,1,y2+h*k3,2,y3+h*k3,3);
k4,3=f3(x0+h,y1+h*k3,1,y2+h*k3,2,y3+h*k3,3);
Donde:
Y1(x+h)=Y1(x)+h*(k1,1+2*k2,1+2*k3,1+k4,1)/6;
Y2(x+h)=Y2(x)+h*(k1,2+2*k2,2+2*k3,2+k4,2)/6;
Y3(x+h)=Y3(x)+h*(k1,3+2*k2,3+2*k3,3+k4,3)/6;

CODIGO DE PROGRAMA:
CODIGO PARA EL BOTON CALCULAR:
function pushbutton1_Callback(hObject, eventdata, handles)
f1x=get(handles.edit1,'string');
f2x=get(handles.edit2,'string');
f3x=get(handles.edit3,'string');
x0=str2double(get(handles.edit4,'string'));
xn=str2double(get(handles.edit5,'string'));
y1=str2double(get(handles.edit6,'string'));
y2=str2double(get(handles.edit7,'string'));
y3=str2double(get(handles.edit8,'string'));
n=str2double(get(handles.edit9,'string'));
f1=inline(f1x,'x','y1','y2','y3');
f2=inline(f2x,'x','y1','y2','y3');
f3=inline(f3x,'x','y1','y2','y3');
h=(xn-x0)/n;
cad1='';
for i=1:n

81

FIAG-ESIC

METODO NUMERICOS EN MATLAB


k11=f1(x0,y1,y2,y3);
k12=f2(x0,y1,y2,y3);
k13=f3(x0,y1,y2,y3);
k21=f1(x0+h/2,y1+(h/2)*k11,y2+(h/2)*k12,y3+(h/2)*k13);
k22=f2(x0+h/2,y1+(h/2)*k11,y2+(h/2)*k12,y3+(h/2)*k13);
k23=f3(x0+h/2,y1+(h/2)*k11,y2+(h/2)*k12,y3+(h/2)*k13);
k31=f1(x0+h/2,y1+(h/2)*k21,y2+(h/2)*k22,y3+(h/2)*k23);
k32=f2(x0+h/2,y1+(h/2)*k21,y2+(h/2)*k22,y3+(h/2)*k23);
k33=f3(x0+h/2,y1+(h/2)*k21,y2+(h/2)*k22,y3+(h/2)*k23);
k41=f1(x0+h,y1+h*k31,y2+h*k32,y3+h*k33);
k42=f2(x0+h,y1+h*k31,y2+h*k32,y3+h*k33);
k43=f3(x0+h,y1+h*k31,y2+h*k32,y3+h*k33);
y1=y1+h*(k11+2*k21+2*k31+k41)/6;
y2=y2+h*(k12+2*k22+2*k32+k42)/6;
y3=y3+h*(k13+2*k23+2*k33+k43)/6;
x0=x0+h;
cad2=sprintf(' %d.y1=%7.3f y2=%7.3f y3=%7.3f\n',i,y1,y2,y3);
cad1=[cad1,cad2];
end
set(handles.edit11,'string',y1);
set(handles.edit12,'string',y2);
set(handles.edit13,'string',y3);
set(handles.edit10,'string',cad1);
CODIGO PARA EL BOTON GRAFICAR:
function pushbutton2_Callback(hobject, eventdata, handles)
f1x=get(handles.edit1,'string');
f2x=get(handles.edit2,'string');
f3x=get(handles.edit3,'string');
x0=str2double(get(handles.edit4,'string'));
xn=str2double(get(handles.edit5,'string'));
y1=str2double(get(handles.edit6,'string'));
y2=str2double(get(handles.edit7,'string'));
y3=str2double(get(handles.edit8,'string'));
n=str2double(get(handles.edit9,'string'));
f1=inline(f1x,'x','y1','y2','y3');
f2=inline(f2x,'x','y1','y2','y3');
f3=inline(f3x,'x','y1','y2','y3');
h=(xn-x0)/n;
cad1='';
matrix1=[y1];
matrix2=[y2];
matrix3=[y3];
for i=1:n
k11=f1(x0,y1,y2,y3);
k12=f2(x0,y1,y2,y3);
k13=f3(x0,y1,y2,y3);
k21=f1(x0+h/2,y1+(h/2)*k11,y2+(h/2)*k12,y3+(h/2)*k13);
k22=f2(x0+h/2,y1+(h/2)*k11,y2+(h/2)*k12,y3+(h/2)*k13);
k23=f3(x0+h/2,y1+(h/2)*k11,y2+(h/2)*k12,y3+(h/2)*k13);

82

FIAG-ESIC

METODO NUMERICOS EN MATLAB


k31=f1(x0+h/2,y1+(h/2)*k21,y2+(h/2)*k22,y3+(h/2)*k23);
k32=f2(x0+h/2,y1+(h/2)*k21,y2+(h/2)*k22,y3+(h/2)*k23);
k33=f3(x0+h/2,y1+(h/2)*k21,y2+(h/2)*k22,y3+(h/2)*k23);
k41=f1(x0+h,y1+h*k31,y2+h*k32,y3+h*k33);
k42=f2(x0+h,y1+h*k31,y2+h*k32,y3+h*k33);
k43=f3(x0+h,y1+h*k31,y2+h*k32,y3+h*k33);
y1=y1+h*(k11+2*k21+2*k31+k41)/6;
y2=y2+h*(k12+2*k22+2*k32+k42)/6;
y3=y3+h*(k13+2*k23+2*k33+k43)/6;
x0=x0+h;
matrix1=[matrix1,y1];
matrix2=[matrix2,y2];
matrix3=[matrix3,y3];
end
for i=1:n+1
x(i)=x0+(i-1)*h;
end
plot(x,matrix1,'r*'),hold on
plot(x,matrix2,'g*'),hold on
plot(x,matrix3,'b*'),hold on
grid on
pan on
legend('y1','y2','y3')
CODIGO PARA EL BOTON LIMPIAR:
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
set(handles.edit8,'string','');
set(handles.edit9,'string','');
set(handles.edit10,'string','');
set(handles.edit11,'string','');
set(handles.edit12,'string','');
set(handles.edit13,'string','');
CODIGO PARA EL BOTON SALIR:
function pushbutton4_Callback(hObject, eventdata, handles)
close
VENTANA DE DISEO Y APLICACIN:

83

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Creado por Oscar mamani

18. ECUACIONES DIFERENCIALES DE ORDEN SUPERIOR:


TEORIA:
Dado el problema de valor inicial PVI general.

y(n) =f (x , y , y ' , y '' , y ' ' , , y (n 1) )

Donde Y(x0)=d1 Y(x0)=d2.

Y (n1 ) ( x 0 )=dn

Y (xf)=?
La estrategia es convertir la ecuacin diferencial en un sistema de ecuaciones
diferenciales lineales de primer orden.
Sea:
u1=Y

un= Y

(n1)

u2=Y

84

FIAG-ESIC

METODO NUMERICOS EN MATLAB


u3=Y
Ahora:
u1=Y=u2
u2=Y=u3
u3=Y=u4
..

..

Con sus respectivas condiciones iniciales:


u1(x0)=d0 u2(x0)=d2

u3(x0)=d3un(x0)=d(n-1)

Y se forma un sistema de ecuaciones:


Luego se desarrolla como un sistema de ecuaciones con sus respectivos
condiciones iniciales, utilizando el mtodo de Runge kutta.

CODIGO DE PROGRAMA:
CODIGO PARA EL BOTON CALCULAR:
function pushbutton1_Callback(hObject, eventdata, handles)
f1=inline('u2','x','u1','u2');
f2=inline(get(handles.edit1,'string'),'x','u1','u2');
a=str2double(get(handles.edit2,'string'));
b=str2double(get(handles.edit3,'string'));
u1=str2double(get(handles.edit4,'string'));
u2=str2double(get(handles.edit5,'string'));
n=str2double(get(handles.edit6,'string'));
h=(b-a)/n;
x=a;
cad1='';

85

FIAG-ESIC

METODO NUMERICOS EN MATLAB


for i=1:n
k11=f1(x,u1,u2);
k12=f2(x,u1,u2);
k21=f1(x+h/2,u1+(h/2)*k11,u2+(h/2)*k12);
k22=f2(x+h/2,u1+(h/2)*k11,u2+(h/2)*k12);
k31=f1(x+h/2,u1+(h/2)*k21,u2+(h/2)*k22);
k32=f2(x+h/2,u1+(h/2)*k21,u2+(h/2)*k22);
k41=f1(x+h,u1+h*k31,u2+h*k32);
k42=f2(x+h,u1+h*k31,u2+h*k32);
u1=u1+h*(k11+2*k21+2*k31+k41)/6;
u2=u2+h*(k12+2*k22+2*k32+k42)/6;
x=x+h;
cad2=sprintf(' %d. Y=%7.3f u2=%7.3f\n',i,u1,u2);
cad1=[cad1,cad2];
end
set(handles.edit7,'string',u1);
set(handles.edit8,'string',cad1);
CODIGO PARA EL BOTON GRAFICAR:
function pushbutton2_Callback(hObject, eventdata, handles)
f1=inline('u2','x','u1','u2');
f2=inline(get(handles.edit1,'string'),'x','u1','u2');
a=str2double(get(handles.edit2,'string'));
b=str2double(get(handles.edit3,'string'));
u1=str2double(get(handles.edit4,'string'));
u2=str2double(get(handles.edit5,'string'));
n=str2double(get(handles.edit6,'string'));
h=(b-a)/n;
x=a;
w(1)=a;
y(1)=u1;
for i=1:n
k11=f1(x,u1,u2);
k12=f2(x,u1,u2);
k21=f1(x+h/2,u1+(h/2)*k11,u2+(h/2)*k12);
k22=f2(x+h/2,u1+(h/2)*k11,u2+(h/2)*k12);
k31=f1(x+h/2,u1+(h/2)*k21,u2+(h/2)*k22);
k32=f2(x+h/2,u1+(h/2)*k21,u2+(h/2)*k22);
k41=f1(x+h,u1+h*k31,u2+h*k32);
k42=f2(x+h,u1+h*k31,u2+h*k32);
u1=u1+h*(k11+2*k21+2*k31+k41)/6;
u2=u2+h*(k12+2*k22+2*k32+k42)/6;
x=x+h;
y(i+1)=u1;
w(i+1)=x;
end
set(handles.edit7,'string',u1);
axes(handles.axes1)
plot(w,y,'r.',w,y),grid on
title('GRFICA DE Y');

86

FIAG-ESIC

METODO NUMERICOS EN MATLAB


pan on
CODIGO PARA EL BOTON LIMPIAR:
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit7,'string','');
set(handles.edit8,'string','');
set(handles.edit6,'string','');
set(handles.edit5,'string','');
set(handles.edit4,'string','');
set(handles.edit3,'string','');
set(handles.edit2,'string','');
set(handles.edit1,'string','');
CODIGO PARA EL BOTON SALIR:
function pushbutton4_Callback(hObject, eventdata, handles)
close

VENTANA DE DISEO Y APLICACIN:

87

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Creado por Oscar mamani

19. METODO DE DIFERENCIAS FINITAS-ECUACION DE SEGUNDO ORDEN:


TEORIA:
Un problema de valores de frontera consta de una ecuacin diferencial de orden n
condiciones de frontera y puestas sobre la funcin conocida en valores de la
variable independiente.
PARA UNA ECUACION DIFERENCIAL DE SEGUNDO ORDEN:
Si:
Y"= P(x) Y' + Q(x)Y + R(x)

x [a, b]

Y(a)=
Y(b)=
Dada una discretizacion de la regin:
X(i)=a+i*h i=0,1,2..N h=(b-a)/N
Y se aproximan las derivadas mediante (operadores en diferencias). Por ejemplo
utilizando que:

88

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Se observa que la ecuacin en diferencias no se puede aplicar, directamente a los


nodos de frontera a=x0 , b=Xn donde se han de considerar las condiciones de
contorno.
Por ello se aplica la ecuacin en diferencias a los nodos interiores y se deja los
exteriores para las condiciones de contorno.
Con la notacin pi=p(xi) qi=q(xi) ri=r(xi) tendremos en los nodos interiores (i=1,
,N-1)

Agrupando trminos queda:

Esto es un sistema de N-1 ecuaciones con N+1 incgnitas, introduciendo las


notaciones:
2-pi*h=ai

2*h*h*qi-4=bi

2+pi*h=ci

Se tiene las N-1 ecuaciones:

89

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Se tendr la siguiente operacin de matrices:

Donde yi ,i=0,1N seria todas las imgenes buscadas.


CODIGO DE PROGRAMA:
CODIGO PARA EL BOTON CALCULAR:
function pushbutton1_Callback(hObject, eventdata, handles)
p=inline(get(handles.edit1,'string'));
q=inline(get(handles.edit2,'string'));
r=inline(get(handles.edit3,'string'));
a=str2double(get(handles.edit4,'string'));

90

FIAG-ESIC

METODO NUMERICOS EN MATLAB


y0=str2double(get(handles.edit5,'string'));
b=str2double(get(handles.edit6,'string'));
yn=str2double(get(handles.edit7,'string'));
n=str2double(get(handles.edit8,'string'));
h=(b-a)/n;
for i=1:n+1
x(i)=a+h*(i-1);
end
A=zeros(n-1,n-1);
d=1;
for i=1:n-1
if i==1 | i==n-1
if i==1
A(1,1)=2+(h*h)*q(x(2));
A(1,2)=-1+(h/2)*p(x(2));
else
A(n-1,n-2)=-1-(h/2)*p(x(n));
A(n-1,n-1)=2+(h*h)*q(x(n));
end
else
A(i,d)=-1-(h/2)*p(x(i+1));
A(i,d+1)=2+(h*h)*q(x(i+1));
A(i,d+2)=-1+(h/2)*p(x(i+1));
d=d+1;
end
end
B=zeros(n-1,1);
for i=1:n-1
if i==1 | i==n-1
if i==1
B(1,1)=-(h*h)*r(x(i+1))-(-1-(h/2)*p(x(i+1)))*y0;
else
B(n-1,1)=-(h*h)*r(x(n))-(-1+(h/2)*p(x(n)))*yn;
end
else
B(i,1)=-(h*h)*r(x(i+1));
end
end
C=inv(A)*B;
d=[y0;C;yn];
cad='';
cad1='';
for i=1:n+1
cadena=sprintf('%d:%6.4f\n',i,d(i,1));
cad=[cad,cadena];
cade=sprintf('%d:%6.4f\n',i,x(i));
cad1=[cad1,cade];
end
set(handles.edit9,'string',cad1);
set(handles.edit10,'string',cad);

91

FIAG-ESIC

METODO NUMERICOS EN MATLAB

CODIGO PARA EL BOTON GRAFICAR:


function pushbutton2_Callback(hObject, eventdata, handles)
p=inline(get(handles.edit1,'string'));
q=inline(get(handles.edit2,'string'));
r=inline(get(handles.edit3,'string'));
a=str2double(get(handles.edit4,'string'));
y0=str2double(get(handles.edit5,'string'));
b=str2double(get(handles.edit6,'string'));
yn=str2double(get(handles.edit7,'string'));
n=str2double(get(handles.edit8,'string'));
h=(b-a)/n;
for i=1:n+1
x(i)=a+h*(i-1);
end
A=zeros(n-1,n-1);
d=1;
for i=1:n-1
if i==1 | i==n-1
if i==1
A(1,1)=2+(h*h)*q(x(2));
A(1,2)=-1+(h/2)*p(x(2));
else
A(n-1,n-2)=-1-(h/2)*p(x(n));
A(n-1,n-1)=2+(h*h)*q(x(n));
end
else
A(i,d)=-1-(h/2)*p(x(i+1));
A(i,d+1)=2+(h*h)*q(x(i+1));
A(i,d+2)=-1+(h/2)*p(x(i+1));
d=d+1;
end
end
B=zeros(n-1,1);
for i=1:n-1
if i==1 | i==n-1
if i==1
B(1,1)=-(h*h)*r(x(i+1))-(-1-(h/2)*p(x(i+1)))*y0;
else
B(n-1,1)=-(h*h)*r(x(n))-(-1+(h/2)*p(x(n)))*yn;
end
else
B(i,1)=-(h*h)*r(x(i+1));
end
end
C=inv(A)*B;
d=[y0;C;yn];
plot(x,d,'r*'),grid on
xlabel('');
ylabel('LA CURVA');

92

FIAG-ESIC

METODO NUMERICOS EN MATLAB


zoom on
CODIGO PARA EL BOTON LIMPIAR:
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.edit1,'string','');
set(handles.edit2,'string','');
set(handles.edit3,'string','');
set(handles.edit4,'string','');
set(handles.edit5,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
set(handles.edit8,'string','');
set(handles.edit9,'string','');
set(handles.edit10,'string','');
CODIGO PARA EL BOTON SALIR:
function pushbutton3_Callback(hObject, eventdata, handles)
close
VENTANA DE DISEO Y APLICACIN:

93

FIAG-ESIC

METODO NUMERICOS EN MATLAB

Creado por Oscar mamani

94

FIAG-ESIC

You might also like