Sei sulla pagina 1di 17

RESOLUCION DE PROBLEMAS DE METODOS NUMERICOS

1. Use la interpolacin de diferencias divididas para encontrar el polinomio


que pasa por los puntos.
(a) (0,1); (2,3); (3,0)
(b) (- 1,0) ;(2,1) ;(3.1); (5.2)
(C) (0, 2); (2,1); (4,4)
2. Utilice diferencias divididas de Newton para encontrar los polinomios de
interpolacin de los puntos del ejercicio 1 y compruebe su concordancia
con el polinomio de interpolacin de LaGrange.
SOLUCIN:
CODIGO DE DIFERENCIAS DIVIDIDAS
clear;clc
%Asignamos lo valores de las Variables "X" y "Y"
x=[0 2 3];y=[1 3 0];
%Asignamos otros nombres a nuestras valores iniciales para utilizarlos
como
%puntos de comparacin al momento de querer evaluar el polimonio en un
%valor determinado. (ver "Presentacin de Resualtados")
xu=x;yu=y;
%Formamos una Matriz cero de la longitud del Vector "Y"
d=zeros(length(y));
% Se genera una Matriz cero donde la primera columna sea el Vector "Y"
d(:,1)=y';
%Formacion de las diferencias divididas
for k=2:length(x)
for j=1:length(x)+1-k
d(j,k)=(d(j+1,k-1)-d(j,k-1))/(x(j+k-1)-x(j));
end
end
% Formacin del Polinomio
for w=1:length(x)
dq=num2str(abs(d(1,w)));
if w>1
if x(w-1)<0
signo1='+';
else
signo1='-';
end
end
if d(1,w)<0
signo2='-';
else
signo2='+';
end
if w==1
acum=num2str(d(1,1));
elseif w==2
polinomioactual=['(x' signo1 num2str(abs(x(w-1))) ')' ];
actual=[dq '*' polinomioactual];

acum=[acum signo2 actual];


else
polinomioactual=[polinomioactual '.*' '(x' signo1
num2str(abs(x(w-1))) ')' ];
actual=[dq '*' polinomioactual];
acum=[acum signo2 actual];
end
end
% Presentacin de Resultados
fprintf('\n Valores de X y Y \n');
disp(xu);
disp(yu);
fprintf('\n Polinomio de Interpolacin : %s \n',acum);
x=input(' X interp = ');
if x>max(xu)||x<min(xu)
fprintf('\t Valor de "X" fuera de rango. El resultado puede ser
equivocado \n\a');
end
xinterp=x;
yinterp=eval(acum);
fprintf(' Y interp (%g) = %g \n',x,yinterp);
% Grafica de los puntos
fprintf('\n Pulse cualquier tecla para ver la grfica de los
puntos\n');
pause
xg=linspace(min(xu),max(xu));
x=xg;yg=eval(acum);
plot(xg,yg,xu,yu,'xk',xinterp,yinterp,'sr');
grid,title('\bf DIFERENCIAS DIVIDIDAS')

a) Valores de X y Y
0
2
3
1
3
0
Polinomio de Interpolacin : 1+1*(x-0)-1.3333*(x-0).*(x-2)
X interp = 0
Y interp (0) = 1
Pulse cualquier tecla para ver la grfica de los puntos

b) Valores de X y Y
-1

Polinomio de Interpolacin: 0+0.33333*(x+1)-0.083333*(x+1).*(x2)+0.041667*(x+1).*(x-2).*(x-3)


X interp = 4
Y interp (4) = 1.24999
Pulse cualquier tecla para ver la grfica de los puntos

c) Valores de X y Y
0

-2

Polinomio de Interpolacin : -2+1.5*(x-0)+0*(x-0).*(x-2)


X interp = 5
Valor de "X" fuera de rango. El resultado puede ser equivocado
Y interp (5) = 5.5

2) SOLUCION:
CODIGO DE LAGRANGE
clc
clear all
x=[0 2 3];
y=[1 3 0];
n=length(x); %nmero de pares de datos
%A=vander(x);
A=zeros(n); %lneas equivalentes a utilizar vander(x)
for j=1:n
A(:,j)=(x').^(n-j);

end
p=A\y'; %sistema de ecuaciones lineales, y' es vector columna
fprintf('\n Valores de X y Y \n');
disp(x);
disp(y);
p=polyfit(x,y,n-1) %n-1 es el grado del polinomio
xx=linspace(0.97,9.44,50); %interpolacin
yy=polyval(p,xx);
hold on
plot(x,y,'o','markersize',4,'markerfacecolor','r')
plot(xx,yy,'b')
xlabel('x')
ylabel('y')
title('Interpolacin de Lagrange');
hold off

Respecto
a) Valores de X y Y
0

p = -1.3333

3.6667

1.0000

b) Valores de X y Y
-1

p = 0.0417 -0.2500

0.4583

0.7500

C) Valores de X y Y
0

-2

p = -0.0000

1.5000 -2.0000

EJERCICIO 13

13. Sea P(x) el polinomio de dcimo grado que pasa a travs de los 11
puntos ( - 5 . 5 ) . ( - 4 . 5 ) . ( - 3 . 5 ) . ( - 2 . 5 ) . ( - 1 . 5 ) . (0 .5 ). (1 .5 ).
(2 .5 ). (3.5). (4.5). (5,42).Calcule P(6 )
clear;clc
%Asignamos lo valores de las Variables "X" y "Y"
x=[-5 -4 -3 -2 -1 0 1 2 3 4 5];y=[5 5 5 5 5 5 5 5 5 5 42];
%Asignamos otros nombres a nuestras valores iniciales para utilizarlos
como
%puntos de comparacin al momento de querer evaluar el polimonio en un
%valor determinado. (ver "Presentacin de Resualtados")
xu=x;yu=y;
%Formamos una Matriz cero de la longitud del Vector "Y"
d=zeros(length(y));
% Se genera una Matriz cero donde la primera columna sea el Vector "Y"
d(:,1)=y';
%Formacion de las diferencias divididas
for k=2:length(x)
for j=1:length(x)+1-k
d(j,k)=(d(j+1,k-1)-d(j,k-1))/(x(j+k-1)-x(j));
end
end
% Formacin del Polinomio
for w=1:length(x)
dq=num2str(abs(d(1,w)));
if w>1
if x(w-1)<0
signo1='+';
else
signo1='-';
end
end
if d(1,w)<0
signo2='-';
else
signo2='+';
end
if w==1
acum=num2str(d(1,1));
elseif w==2
polinomioactual=['(x' signo1 num2str(abs(x(w-1))) ')' ];
actual=[dq '*' polinomioactual];
acum=[acum signo2 actual];
else
polinomioactual=[polinomioactual '.*' '(x' signo1
num2str(abs(x(w-1))) ')' ];
actual=[dq '*' polinomioactual];
acum=[acum signo2 actual];
end
end
% Presentacin de Resultados
fprintf('\n Valores de X y Y \n');
disp(xu);

disp(yu);
fprintf('\n Polinomio de Interpolacin : %s \n',acum);
x=input(' X interp = ');
if x>max(xu)||x<min(xu)
fprintf('\t Valor de "X" fuera de rango. El resultado puede ser
equivocado \n\a');
end
xinterp=x;
yinterp=eval(acum);
fprintf(' Y interp (%g) = %g \n',x,yinterp);
% Grafica de los puntos
fprintf('\n Pulse cualquier tecla para ver la grfica de los
puntos\n');
pause
xg=linspace(min(xu),max(xu));
x=xg;yg=eval(acum);
plot(xg,yg,xu,yu,'xk',xinterp,yinterp,'sr');
grid,title('\bf DIFERENCIAS DIVIDIDAS')

Valores de X y Y
-5

-4

-3

-2

-1

42

Polinomio de Interpolacin :
5+0*(x+5)+0*(x+5).*(x+4)+0*(x+5).*(x+4).*(x+3)+0*(x+5).*(x+4).*(x+3).
*(x+2)+0*(x+5).*(x+4).*(x+3).*(x+2).*(x+1)+0*(x+5).*(x+4).*(x+3).*(x+2)
.*(x+1).*(x-0)+0*(x+5).*(x+4).*(x+3).*(x+2).*(x+1).*(x-0).*(x1)+0*(x+5).*(x+4).*(x+3).*(x+2).*(x+1).*(x-0).*(x-1).*(x2)+0*(x+5).*(x+4).*(x+3).*(x+2).*(x+1).*(x-0).*(x-1).*(x-2).*(x-3)+1.0196e005*(x+5).*(x+4).*(x+3).*(x+2).*(x+1).*(x-0).*(x-1).*(x-2).*(x-3).*(x-4)
X interp = 6
Valor de "X" fuera de rango. El resultado puede ser equivocado
Y interp (6) = 411.992
Pulse cualquier tecla para ver la grfica de los puntos

DIFERENCIACION E INTEGRACION:

2. Utilice la frmulas de la diferencia centrada de tres puntos para


aproximar f(0), donde f(x) = exp(x) para (a) h=0.1 (b) h=0.01 (c)
h=0.001.
CODIGO
global funcion
funcion=input('ingrese la funcion \n f(x)=','s');
x0=input('ingrese el valor del punto a evaluar \n x0 =');
h=input('ingrese los valores de h en vector: \n h =');
x=x0;
for k=1 : length(h)
err(k)=abs((eval(diff(diff(funcion,sym('x')),sym('x')))*h(k))/2);
x=x0;
m=eval(funcion);
x=x0+h(k);
n=eval(funcion);
aprx(k)=(n-m)/(h(k));
end
fprintf( '
h
(f(x0+h)-f(x0))/h
h/2*(x(0))^2\n')
v=[h; aprx
; err];
fprintf( ' %f
%f
%f\n' ,v)

SOLUCION:
ingrese la funcion
f(x)=exp(x)
ingrese el valor del punto a evaluar
x0 =0
ingrese los valores de h en vector:
h =[0.1 0.01 0.001]

(f(x0+h)-f(x0))/h
aproximacin

h/2*(x(0))^2
error

0.100000

1.051709

0.050000

0.010000

1.005017

0.005526

0.001000

1.000500

0.000505

Para la diferenciacin e integracin:


clear; format long
while 1
disp('[1] TRAPECIO SIMPLE (h>0)')
disp('[2] TRAPECIO COMPUESTO(h<0)')
disp('[3] FORMULA DE SIMPSON SIMPLE')
disp('[4] FORMULA DE LOS TRES OCTAVOS DE SIMPSON')
disp('[5] FORMULA DE SIMPSON COMPUESTO')
disp('[6] INTEGRACION DE ROMBERG')
disp('[7] ROMBERG MODIFICADO')
disp('[8] VOLVER')
elecc3 = input('ELIGA OPCION ');
switch elecc3
case 1
clc; clear;
fprintf('\t\tTRAPECIO SIMPLE\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
h=b-a;
x=a;
f=eval(funcion); x=b;
f= (f+eval(funcion))*(h/2);
fprintf('El valor aproximado es: %10.15f\n\n',f)
case 2
clc; clear;
fprintf('\t\tTRAPECIO COMPUESTO\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
n=input('ingrese el numero de intervalos\n');
h=(b-a)/n;
f=0;
for k=1:n-1
x=a+h*k;
f=f+eval(funcion);
end
f=2*f;
x=a; f=f+eval(funcion); x=b; f=f+eval(funcion);
f=(h/2)*(f);
fprintf('El valor aproximado es: %10.15f\n\n',f)
case 3
clc; clear;
fprintf('\t\tFORMULA DE SIMPSON SIMPLE\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('ingrese el limite superior de la funcion\n');
a=input('ingrese el limite inferior de la integral\n');
h=(b-a)/2;
x=a; f=eval(funcion); x=b; f=f+eval(funcion);
x=a+h; f=f+ 4*(eval(funcion));
f=(h/3)*f;
fprintf('El valor aproximado de la integral es: %10.15f\n\n',f)

case 4
clc; clear;
fprintf('\t\tFORMULA DE LOS TRES OCTAVOS DE SIMPSON\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('ingrese el limite superior de la funcion\n');
a=input('ingrese el limite inferior de la integral\n');
h=(b-a)/3;
x=a;
f=eval(funcion);x=a+h; f=f+3*(eval(funcion));
x=a+2*h; f=f+3*(eval(funcion)); x=b;
f=f+eval(funcion);
f=(3*h/8)*f;
fprintf('El valor aproximado de la integral es: %10.15f\n\n',f)
case 5
clc; clear;
fprintf('\t\tFORMULA DE SIMPSON COMPUESTO\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
n=input('ingrese el numero de intervalos');
h=(b-a)/(2*n);
f=0;
for k=1:n-1
x=a+h*(2*k);
f=f+eval(funcion);
end
f1=0;
for k=1:n
x=a+h*(2*k-1);
f1=f1+eval(funcion);
end
f=2*f+4*f1;
x=a; f=f+eval(funcion); x=b; f=f+eval(funcion);
f=(h/3)*f;
fprintf('el valor aproximado de la integral es: %10.15f\n\n',f)
case 6
clc; clear;
fprintf('\t\tINTEGRACION DE ROMBERG\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b= input('ingrese el lmite superior de la integral \n');
a= input('ingrese el lmite inferior de la integral \n');
n= input('ingrese el nmero de intervalos\n');
h=(b-a);
M=1;
J=0;
R=zeros(n,n);
x=a;
f1=eval(funcion);
x=b;
f2=eval(funcion);
R(1,1)=h*(f1+f2)/2;
while (J<(n-1))
J=J+1;

h=h/2;
s=0;
for p=1:M
x=a+h*(2*p-1);
f3=eval(funcion);
s=s+f3;
end
R(J+1,1)=(1/2)*(R(J,1))+h*s;
M=2*M;
for k =1:J
R(J+1,k+1)=R(J+1,k)+(R(J+1,k)-R(J,k))/(4^k-1);
end
end
R
fprintf('La aproximacion buscada es: %10.15f\n\n', R(J+1,J+1))
case 7
clc; clear;
fprintf('\t\tINTEGRACION DE ROMBERG\n')
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('Ingrese el limite superior:\n');
a=input('Ingrese el limite inferior:\n');
n=input('Ingrese el numero de particiones:\n');
tol=input('Ingrese la tolerancia:\n');
M=1;
h=b-a;
err=1;
J=0;
R=zeros(4,4);
x=a;
f_a=eval(funcion);
x=b;
f_b=eval(funcion);
R(1,1)=h*(f_a+f_b)/2;
disp(' quad err h')
while((err>tol)&(J<n))|(J<4)
J=J+1;
h=h/2;
s=0;
for p=1:M
x1=a+h*(2*p-1);
x=x1;
f_x1=eval(funcion);
s=s+f_x1;
end
R(J+1,1)=R(J,1)/2+h*s;
M=2*M;
for K=1:J
R(J+1,K+1)=R(J+1,K)+(R(J+1,K)-R(J,K))/(4^K-1);
end
err=abs(R(J,J)-R(J+1,K+1));
fprintf('%10.9f %10.9f %10.9f\n',R(J+1,J+1),err,h)
end
disp('LA MATRIZ TRIANGULAR INFERIOR ES:')

disp(R)
disp('El error es para el numero de particiones:')
disp(err)
disp('El tamao de la ultima particion es:')
disp(h)
disp('La respuesta es:')
disp(R(J+1,J+1))
otherwise
clc
break
end
end

Potrebbero piacerti anche