Вы находитесь на странице: 1из 17

LABORATORIO DE MTODOS NUMRICOS

RAICES DE ECUACIONES GUIA 4-II


LABORATORIO

Grficas 2D con Matlab:


Funciones de la forma y = f(x)
Dibujar la grafica de la funcin y=sen(x)
>>x=0:pi/100:2*pi;
>>x=linspace(0,2*pi,200);
>> y = sin(x);
>>plot(x,y)
UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 1

LABORATORIO DE MTODOS NUMRICOS

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 2

LABORATORIO DE MTODOS NUMRICOS

Curvas paramtricas
>>t=linspace(-5,5,1000);
>>plot((t.*(t.^2-1))./(t.^2+1),(2*(t.^2-1))./(t.^2+1))

Curvas en el espacio:
>>t=linspace(0,8*pi,2000);
>>plot3(sin(t),cos(t),t),grid on

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 3

LABORATORIO DE MTODOS NUMRICOS

Curvas de la forma

y=f ( x , y )

>>[x,y]=meshgrid(-2:.5:2);
>>z=exp(-x.^2-y.^2);
>>plot3(x,y,z)
>>mesh(x,y,z)
>>surf(x,y,z)
>>surf(x,y,z)

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 4

LABORATORIO DE MTODOS NUMRICOS

Ejercicio 1 Biseccin

BOLZANO:
% bolzano2.m
function[raiz,z,it]=bolzano2(f,a,b,TOL)
UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 5

LABORATORIO DE MTODOS NUMRICOS

f=inline('exp(-x)-x');
z=[];
for it=1:1000
x=(a+b)/2;
err=(b-a)/2;
z=[z; a x b err];
if feval(f,a)*feval(f,x)<0
b=x;
else
a=x;
end
if err<TOL
break
end
end
raiz=x;

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 6

LABORATORIO DE MTODOS NUMRICOS

Ejercicio 2: localizacin
a) Races de la funcin

x /3

f ( x )=

e
sen (x)
2

en el

intervalo de [-10 10]

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 7

LABORATORIO DE MTODOS NUMRICOS

% fun1.m
function [f]=fun1(x)
f=1/2*exp(x/3)-sin(x);

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 8

LABORATORIO DE MTODOS NUMRICOS

% Localiza.m
clc, clear all, format short
x=-10:10;
y=fun1(x);
plot(x,y),grid
disp('x vs y')
disp([x' y'])
% Intervalos que contienen raices
acu=[];
for i=1:length(x)-1
if y(i)*y(i+1)<0, acu=[acu; x(i) x(i+1)];
end
end
disp('Intervalos que contienen raices...');
disp(acu)

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 9

LABORATORIO DE MTODOS NUMRICOS

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 10

LABORATORIO DE MTODOS NUMRICOS

b)Usando el mtodo de newton rapson con una


tolerancia de 1e-8
% dfun1.m
function [df]=dfun1(x)
df=1/6*exp(x/3)-cos(x);
% raphson.m
function[acu,raiz,it]=raphson(f,df,x,TOL)
acu=[];
for it=1:100
xn=x-feval(f,x)/feval(df,x);
err=abs(xn-x);
acu=[acu; xn err];
x=xn;
if err<TOL
break
end
end
raiz=xn;

Ejercicio 3: Solucin de sistema no lineal


UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 11

LABORATORIO DE MTODOS NUMRICOS

Mtodo del punto fijo


function[H]=pto_fs(G,x0,tol,maxit)
syms x1 x2
H=[0 x0' 1];
for i=1:maxit
x=subs(G,{x1 x2},{x0(1) x0(2)});
e=norm(x-x0,inf);
H=[H; i,x' e];
if e<tol
break
end
x0=x;
end
clear all
syms x1 x2
G=[0.5*sin(x2);
0.25*exp(-x1)]
x0=[0 0]';
maxit=50;
tol=0.5e-3
H=pto_fs(G,x0,tol,maxit);
disp(' i
x1
x2
error')

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 12

LABORATORIO DE MTODOS NUMRICOS

fprintf('%4.0f %12.8f %16.8f %20.8e


\n',H')

Ejemplo 4: Newton Raphson para sistemas


function [x,iter]=newton(f,fp,x0,tol,itermax)
UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 13

LABORATORIO DE MTODOS NUMRICOS

%NEWTON Mtodo de Newton para sistemas


no lineales % Los datos de entrada son
% f: Nombre de la funcin que representa el
sistema.
% fp: Nombre de la funcin que calcula el
Jacobiano.
% x0: El punto inicial (vector columna).
% tol: Tolerancia para el error relativo en la
solucin calculada
% itermax: Nmero mximo de iteraciones que
se repiten las iteraciones
if nargin<4
tol=1.0e-4;
end
if nargin<5
itermax=20;
end x=x0;
normx=0;
normz=inf;
iter=0;
while (normz>tol*normx)&(iter<=itermax)
f0=feval(f,x);
fp0=feval(fp,x);
z=-fp0\f0;
normz=norm(z,2);
normx=norm(x,2);
x=x+z;
UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 14

LABORATORIO DE MTODOS NUMRICOS

iter=iter+1;
end

Ejemplo 5: para el siguiente sistema no


lineal
2 x 1sen ( x 2 )=0
4 x 2exp ( x 1 )=0

Definir F(x) y el jacobiano de F(x)


F ( x )=

F ' ( x )=

2 x 1sen ( x 2) =0
fr x=(x 1 , x 2 )
4 x 2exp (x 1) =0

2cos ( x 2 )
exp ( x 1 ) 4

En matlab
function [y,J]=F(x)
y=[2*x(1)-sin(x(2)); 4*x(2)-exp(-x(1))];
J=[2, -cos(x(2)); exp(-x(1)), 4];
Para hacer un paso de newton usar la funcin:
function y = newtonstep(x)
[y,J] = F(x);
UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 15

LABORATORIO DE MTODOS NUMRICOS

dx = - J \ y;
y = dx + x;
Para aplicar Newton al problema
clear all
x0=[0 0]';
maxit=50;
tol=0.5e-3;
H=[0 x0' 1];
x=x0;
for i=1:maxit
y = newtonstep(x);
e=norm(y-x,inf);
H=[H; i y' e];
if e<tol
break
end
x=y;
end
disp(' i
x1
x2
error')
fprintf('%4.0f %12.8f %16.8f %20.8e \n',H')

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 16

LABORATORIO DE MTODOS NUMRICOS

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS Pgina 17

Вам также может понравиться