Beruflich Dokumente
Kultur Dokumente
%X : Vector De Abscisas
%Y : Vector De Ordenadas
%C : Matriz De Coeficientes De Interpolacin Polinomial
%L : Matriz De Coeficientes Polinomiales
w=length(X);
n=w-1;
L=zeros(w,w);
for k=1:n+1
V=1;
for j=1:n+1
if k~=j
V=conv(V,poly(X(j)))/(X(k)-X(j));
end
end
L(k,:)=V;
end
C=Y*L;
EJEMPLO
Hallar el Polinomio Interpolador de Lagrange que pasa por los puntos (2,3), (4,5),
(6,5), (7,6), (8,8) y (9,7).
SOLUCION
C =
L =
>> pretty(poly2sym(C))
function [C,D]=pnewton(X,Y)
%X contiene las abscisas de los puntos de interpolacion
%Y contiene las ordenadas de los puntos de interpolacion
%C contiene los coeficientes del polinomio interpolador de newton
%D contien la tabla de diferencias divididas
n=length(X);
D=zeros(n,n);
D(:,1)=Y';
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
C=D(n,n);
for k=(n-1):-1:1
C=conv(C,poly(X(k)));
m=length(C);
C(m)=C(m)+D(k,k);
end
EJEMPLO
Hallar el Polinomio Interpolador de Newton que pasa por los puntos (2,3), (4,5), (6,5),
(7,6), (8,8) y (9,7).
SOLUCION
C =
L =
3.0000 0 0 0 0 0
5.0000 1.0000 0 0 0 0
5.0000 0 -0.2500 0 0 0
>>pretty(poly2sym(C))
clc;
clear all;
close all;
fprintf('\n\n\n');
fprintf('INTERPOLACION');
fprintf('\n-------------\n\n');
x=input('Ingrese Los Valores De x : ');
fprintf('\n');
y=input('Ingrese Los Valores De y : ');
fprintf('\n');
xx=input('Para Que Valores De X Quiere Interpolar ?');
temp_x=interp1(x',y',xx','linear');
fprintf('\n\nRpta : ');
fprintf('\n\nLos Valores De\n x y\n\n');
for k=1:length(temp_x)
fprintf(' %5.6f %5.6f\n',xx(k),temp_x(k));
end
EJEMPLO
SOLUCION
>>interpolacion1
INTERPOLACION
-------------
Rpta:
Los Valores De
x y
875.000000 311.714286
800.000000 388.400000
clc;
clear all;
close all;
fprintf('\n\n\n');
fprintf('INTERPOLACION');
fprintf('\n-------------\n\n');
x=input('Ingrese Los Valores De x : ');
fprintf('\n');
y=input('Ingrese Los Valores De y : ');
fprintf('\n');
z=input('Ingrese Los Valores De z : ');
fprintf('\n');
xx=input('Para Que Valores De X Quiere Interpolar ?');
yy_zz=interp1(x',[y',z'],xx','linear');
fprintf('\n\nRpta : ');
fprintf('\n\nLos Valores De\n x y z\n\n');
for k=1:length(yy_zz)
fprintf(' %5.4f %5.4f %5.4f\n',xx(k),yy_zz(k,:));
end
EJEMPLO
SOLUCION
>>interpolacion2
INTERPOLACION
-------------
Los Valores De
x y z
function [L,n]=derivadalim(f,x,tolerancia)
% f es la funcion a derivar
% x es el punto de derivacion
% L=[H' D' E'] el H es el vector de tamanos de peso
% D es el vector de derivadas aproximadas
% E es el vector de cotas de error
% n es la coordenada de la mejor aproximacion
iteracionesmaximas=15;
h=1;
H(1)=h;
D(1)=(feval(f,x+h)-feval(f,-h))/(2*h);
E(1)=0;
R(1)=0;
for n=1:2
h=h/10;
H(n+1)=h;
D(n+1)=(feval(f,x+h)-feval(f,-h))/(2*h);
E(n+1)=abs(D(n+1)-D(n));
R(n+1)=2*E(n+1)*(abs(D(n+1))+abs(D(n))+eps);
end
n=2;
while(E(n)>E(n+1))&(R(n)>tolerancia)&n<iteracionesmaximas
h=h/10;
H(n+2)=h;
D(n+2)=(feval(f,x+h)-feval(f,-h))/(2*h);
E(n+2)=abs(D(n+2)-D(n+1));
R(n+2)=2*E(n+2)*(abs(D(n+2))+abs(D(n+1))+eps);
n=n+1;
end
n=length(D)-1;
L=[H' D' E'];
EJEMPLOS
1 15
1.- Aproximar la derivada de la funcin () = (cos ( )) en el punto
2
SOLUCION
5+ () 1+5
2.- Aproximar la derivada de la funcin () = tan (cos ( )) en el punto
1+ 2 3
SOLUCION
function [D,errorabsoluto,errorrelativo,n]=richardson(f,x,delta,tolerancia)
% f : Funcion a derivar
% x : Punto donde hallar la derivada
% delta : Tolerancia de error
% tolerancia : Tolerancia de error relativo
% D : Matriz de aproximacion de las derivadas
errorabsoluto=1;
errorrelativo=1;
h=1;
j=1;
D(1,1)=(feval(f,x+h)-feval(f,x-h))/(2*h);
while (errorrelativo>tolerancia) & (errorabsoluto>delta) & (j<12)
h=h/2;
D(j+1,1)=(feval(f,x+h)-feval(f,x-h))/(2*h);
for k=1:j
D(j+1,k+1)=D(j+1,k)+(D(j+1,k)-D(j,k))/((4^k)-1);
end
errorabsoluto=abs(D(j+1,j+1)-D(j,j));
errorrelativo=2*errorabsoluto/(abs(D(j+1,j+1))+abs(D(j,j))+eps);
j=j+1;
end
[n,n]=size(D);
EJEMPLO
1 15
1.- Aproximar la derivada de la funcin () = (cos ( )) en el punto
2
SOLUCION
function f=funcion(x);
f=sin(cos(1./x));
>> [D,errorrelativo,errorabsoluto,n]=richardson('funcion',(1-sqrt(5))/2,0.001,0.001)
D =
-0.7448 0 0 0 0 0
-1.1335 -1.2631 0 0 0 0
errorrelativo =
6.9003e-04
errorabsoluto =
2.6419e-04
n =
function [A,df]=nodos(X,Y)
% X es el vector de abscisas e Y es un vector lxn de ordenadas
% A es un vector lxn con los coeficientes del polinomio de Newton de grado N
% df es la aproximacion de la derivada
A=Y;
N=length(X);
for j=2:N
for k=N:-1:j
A(k)=(A(k)-A(k-1))/(X(k)-X(k-j+1));
end
end
x0=X(1);
df=A(2);
prod=1;
n1=length(A)-1;
for k=2:n1
prod=prod*(x0-X(k));
df=df+prod*A(k+1);
end
EJEMPLO
1 15
1.- Aproximar la derivada de la funcin () = (cos ( )) en el punto
2
SOLUCION
A =
df =
-1.4952
function T=trapeciorecursiva(f,a,b,n)
M=1;
h=b-a;
T=zeros(1,n+1);
T(1)=h*(feval(f,a)+feval(f,b))/2;
for j=1:n
M=2*M;
h=h/2;
s=0;
for k=1:M/2
x=a+h*(2*k-1);
s=s+feval(f,x);
end
T(j+1)=T(j)/2+h*s;
end
EJEMPLO
2 1
Calcular la sgte integral: 0 1
2+
10
SOLUCION
Definimos la funcin
function F=integrando1(x);
F=1/(x^2+1/10);
>> trapeciorecursiva('integrando1',0,2,14)
ans =
Columns 1 through 11
Columns 12 through 15
function s=trapeciocompuesta(f,a,b,M)
h=(b-a)/M;
s=0;
for k=1:(M-1)
x=a+h*k;
s=s+feval(f,x);
end
s=h*(feval(f,a)+feval(f,b))/2+h*s;
EJEMPLO
2 1
Calcular la sgte integral: 0 1
2+
10
SOLUCION
Definimos la funcin
function F=integrando1(x);
F=1/(x^2+1/10);
>> trapeciocompuesta('integrando1',0,2,14)
ans =
4.4710
clear all;
clc;
close all;
fprintf('\n\n\n');
fprintf('SOLUCIONES DE INTEGRALES');
fprintf('\n________________________\n\n');
f=input('Ingrese el nombre de la funcion a integrar :');
fprintf('\n');
aa=1;
while(aa==1)
a=input('Ingrese Limite Inferior : ');
fprintf('\n');
b=input('Ingrese Limite Superior : ');
int=quad(f,a,b);
fprintf('\n\n RPTA : ');
fprintf('\n\n El Valor de la Integarl es : %5.6f\n\n',int);
rpta=input('Desea Ingresar Nuevos Limites ? [Y/N] : ','s');
if(rpta=='Y')
aa=1;
fprintf('\n');
elseif(rpta=='N')
aa=2;
end
end
EJEMPLO
1.5 ()+
Integrar 0.5
2 + 2 2
A=-0.5
B=3
C=1.5
D=
R=4
SOLUCION
function y=prob1(x)
A=-0.5;
B=3;
C=1.5;
D=pi;
R=4;
num=exp(A*x).*sin(B*x)+x.^C-D;
den=sqrt(R^2+x.^2)-2;
y=num./den;
>> integral
SOLUCIONES DE INTEGRALES
________________________
RPTA:
EJEMPLO
HORA 6:00 6:12 6:24 6:36 6:48 7:00 7:12 7:24 7:36 7:48 8:00 8:12
GASTO 6.2 6.0 5.9 5.9 6.2 6.4 6.5 6.8 6.9 7.1 7.3 6.9
Cul es la cantidad de petrleo bombeado en 2 horas 12 minutos?
2.2
Usar la integral llevando el tiempo a horas 0 ()
SOLUCION
>>trapecio
===================
Ingrese Los Xs [x0, x1, x2,...]: [0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2]
Ingrese Los f(x) s [f(x0), f(x1), f(x2),...]: [6.2 6.0 5.9 5.9 6.2 6.4 6.5 6.8 6.9 7.1
7.3 6.9]
RPTA: