Está en la página 1de 20

Título

Asignatura

Profesor
Facultad

lunes, 27 de junio de 2011


Universidad Católica San Antonio de Murcia - Tlf: (+34) 968 27 88 00 info@ucam.edu - www.ucam.edu
Asignatura

function [pol_interp , condicion]= fitvand(X,Y)


%% fitvand
% Método de coeficientes indeterminados (Vandermonde) para el cálculo del
% polinomio interpolante en la forma de Lagrange. Se plantea hallar el
% sistema lineal de n+1 ecuaciones donde las incógnitas son los n+1
% coeficientes del polinomio interpolante de Lagrange de los n+1 puntos de
% entrada, y se resuelve de manera numérica, devolviendo en Colag el vector
% de coeficientes del polinimio. También devuelve el parámetro condición que
% es una medida del condicionamiento de la matriz del sistema (calculada a
% partir de los valores de x). Cuanto mayor es el valor de condición, más
% sensible es la solución del problema (el cálculo de la matriz inversa) a
% variaciones o errores en los valores de entrada (las X). Además se
% representa el conjunto de puntos y el polinomio. (El programa completo
% está disponible en el campus virtual)
%
% Entrada:
% X = [ x0 x1 .. xn ] - nodos de interpolación
% Y = [ y0 y1 .. yn ] - condiciones de interpolación de interpolación
%
% Salida:
% CoLag = Coeficientes del polinomio de Lagrange en la base canónica
%
% Ejemplo:

2
r - Tlf: (+34) 968 27 88 00 -
Asignatura

% [CoLag condicion] = fitvand( [ 1.35 1.37 1.40 1.45 ], [ .1303 .1367 .1461 .1614
])

% X e Y deben estar en forma de vector fila

A = vander(X); % Calcula la matriz de Vandermonde que forma el sistema de


% coeficientes indeterminados que debemos resolver
pol_interp = A\Y'; % Resolver el sistema.
% Precaución, pol_interp(1) es el coeficiente del término de mayor grado
condicion = cond(A); % Si es mayor que 1, está mal condicionada la matriz

% Representación gráfica
plot(X,Y,'or');
hold on;
x_plot = linspace(min(X)*0.95*sign(min(X)),max(X)*1.05*sign(max(X)),100);
plot(x_plot,polyval(pol_interp,x_plot,'-'));

Aquí es donde se explica lo que hace el fitvand.

Aquí se explica lo que hace la function vander(), devuelve la matriz de Vnadermonde.


function A = vander(v)
%VANDER Vandermonde matrix.
% A = VANDER(V) returns the Vandermonde matrix whose columns
% are powers of the vector V, that is A(i,j) = v(i)^(n-j).
%
% Class support for input V:
% float: double, single

% Copyright 1984-2014 The MathWorks, Inc.

v = v(:);
n = length(v);
if n == 0
A = reshape(v, n, n);
return;
end
A = repmat(v, 1, n);
A(:, n) = 1;
A = cumprod(A, 2, 'reverse');

>> [coeficientes_indet cond]=fitvand([0.1 0.5 1.1 2.0],[12 20 10 9])

coeficientes_indet =

24.7563

3
r - Tlf: (+34) 968 27 88 00 -
Asignatura

-78.7524
59.5770
6.8051

cond =

138.7438

20

18

16

14

12

10

0
0 0.5 1 1.5 2 2.5

Donde pone coeficietes_indet son los coeficientes del polinomio interpolado ,y son esos resultados.
La cond , es el numero de condicion y por lo que vemos está mal condicionado.

4
r - Tlf: (+34) 968 27 88 00 -
Asignatura

>> x=(0:7)*pi/7

x=

Columns 1 through 5

0 0.4488 0.8976 1.3464 1.7952

Columns 6 through 8

2.2440 2.6928 3.1416

>> y=2*(sin(x)).^2-1

y=

Columns 1 through 5

-1.0000 -0.6235 0.2225 0.9010 0.9010

Columns 6 through 8

0.2225 -0.6235 -1.0000

>> [coeficientes,cond]=fitvand(x,y)

coeficientes =

5
r - Tlf: (+34) 968 27 88 00 -
Asignatura

0.0000
-0.0654
0.6163
-1.7801
1.0477
1.5113
0.0867
-1.0000

cond =

1.9349e+06

>>

>> X2=(0:15)*pi/15

X2 =

Columns 1 through 12

0 0.2094 0.4189 0.6283 0.8378 1.0472 1.2566 1.4661 1.6755 1.8850


2.0944 2.3038

Columns 13 through 16

2.5133 2.7227 2.9322 3.1416

>> Y2=2*sin(X2).^2-1

Y2 =

6
r - Tlf: (+34) 968 27 88 00 -
Asignatura

Columns 1 through 12

-1.0000 -0.9135 -0.6691 -0.3090 0.1045 0.5000 0.8090 0.9781 0.9781 0.8090
0.5000 0.1045

Columns 13 through 16

-0.3090 -0.6691 -0.9135 -1.0000

>> [coeficientes2,cond2]=fitvand(X2,Y2)

coeficientes2 =

0.0000
-0.0000
0.0000
-0.0000
0.0001
0.0001
0.0004
-0.0069
0.0006
0.0884
0.0002
-0.6668
0.0000
2.0000
0.0000
-1.0000

7
r - Tlf: (+34) 968 27 88 00 -
Asignatura

cond2 =

7.3874e+14

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5

El Resultado de la interpolación es la figura.


Si comparamos con el comando cond , encontramos el resultado por interpolar con mayor
nodos es peor , es mas sensible, el problema de esto está en el método de interpolacion que
estamos usando . Cuanto mas incremento el numero de nodos el polinomio interpolador
tiene menos calidad .

8
r - Tlf: (+34) 968 27 88 00 -
Asignatura

a.

>> X = -2:2; Y = [1 0 1 0 1];

P = lagrangepoly(X,Y);

xx = -2.5:.01:2.5;

plot(xx,polyval(P,xx),X,Y,'or');

>> p

Undefined function or variable 'p'.

Did you mean:

>> P

P=

9
r - Tlf: (+34) 968 27 88 00 -
Asignatura

0.3333 0 -1.3333 0 1.0000

>>

-1
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2

Lo que hemos hecho aquí es crear el polinomio interpolante de Lagrange

Donde P=0.3333*x^4 – 1.333*x^2 + 1.000

b.

>> x=[0 0.5 1]

10
r - Tlf: (+34) 968 27 88 00 -
Asignatura

x=

0 0.5000 1.0000

>> y=sin(pi*x/4);

>> p=lagrangepoly(x,y);

>> yy=0:0.1:1

yy =

Columns 1 through 5

0 0.1000 0.2000 0.3000 0.4000

Columns 6 through 10

0.5000 0.6000 0.7000 0.8000 0.9000

Column 11

1.0000

>> plot (yy,polyval(p,yy),x,y,'or');

>> p

p=

11
r - Tlf: (+34) 968 27 88 00 -
Asignatura

-0.1165 0.8236 0

>>

Donde el polinomio interpolante de Lagrange es P= -0.1165*x^2 + 0.8236x

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

d.

12
r - Tlf: (+34) 968 27 88 00 -
Asignatura

>> x= 0:0.5:1

x=

0 0.5000 1.0000

>> y=sin(pi*x/4);

>> y

y=

0 0.3827 0.7071

>> edit lagrangepoly

>> [P,R,S]=lagrangepoly(x,y);

>> P

P=

-0.1165 0.8236 0

>> R

R=

3.5343

13
r - Tlf: (+34) 968 27 88 00 -
Asignatura

>> S

S=

1.4555

>> e=abs(sin(pi*3.5343/4)- 1.4555)

e=

1.0978

>> polyval(P,3.5343)

ans =

1.4555

>> plot(xx,sin(pi*xx/4),'r-',xx,polyval(P,xx),'b-');

14
r - Tlf: (+34) 968 27 88 00 -
Asignatura

1.5

0.5

-0.5

-1

-1.5

-2

-2.5
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

El polinomio resultante de P2(x)= -0.1165 x^2 + 0.8236 x

Vemos que e está fuera del intervalo.

Al comprobar con polyval se ve que no es correcto.

Por lo que vemos que no es tan buena aproximación.

Como se ve la cota de error es elevada. Podemos deducir aquí que cuantos más nodos menor es
la tasa de error.

15
r - Tlf: (+34) 968 27 88 00 -
Asignatura

The newton interpolating poly is formed as \Sigma(a_i\Pi(x-x_j))


a_i coefficients are ...
1.0000
1.2974
0.8417

The polynomial is ...


1.297 x + 0.8417 x (x - 0.5) + 1

Esto es lo que hace el newton_interpolation


function p = newton_interpolation(eq, degree, interval, verbose)
% June 20, 2013 by Ehsan Behnam.
% p = newton_interpolation(...) computes the newton interpolating
% polynomial. "eq" is the string containing the original equation
% which will be interpolated.
% "degree" is the degree of the interpolating polynomial.
% "interval" is the closed interval (e.g. [a b]) for interpolation.
% "verbose" is a logical variable. Set it "true" to see the progress
% of the % function.
% "p" is the symbolic representation of the polynomial.
% You may evaluate it at x_0 simply by typing subs(p, 'x', x_0)

% Usage example: p = newton_interpolation('exp(x)', 2, [0 1]);


% returns p = 1.2974 x + 0.84168 x (x - 0.5) + 1 which is a polynomial
% of degree two interpolating e^x on [0 1].

if (nargin == 3)
verbose = false;
end
if (nargin < 3)
help('newton_interpolation');
p = '';
return;

16
r - Tlf: (+34) 968 27 88 00 -
Asignatura

end
if (numel(interval) ~= 2)
error('bad interval');
end
a0 = interval(1);
b0 = interval(2);

fx = sym(eq);
x = linspace(a0, b0, degree + 1);
y = subs(fx, 'x', x);

if (verbose)
disp('The starting values are ...');
disp([x' y']);
end

% Divided difference algorithm: finding f[x_1, x_2, ..., x_k]


% According to the recursive property:
% f[x_1,x_2,...,x_{k+1}] = (f[x_2,..., x_{k+1}]-f[x_1,x_k])/(x_{k+1}-x_1)
% Let a(i, j) = f[x_i, ..., x_j]
% Observe that according to the theory, we only need F(i, 1) values.
% Let a(i) = F(i, 1);
a = zeros(degree + 1, 1);
for i = 1:degree + 1
a(i) = y(i);
end
for i = 2:degree + 1
for j = degree + 1:-1:i
a(j) = (a(j) - a(j - 1)) / (x(j) - x(j - i + 1));
end
end

if (verbose)
disp(strcat('The newton interpolating poly is formed', ...
' as \Sigma(a_i\Pi(x-x_j))'));
disp('a_i coefficients are ...');
disp(a);
end

%Establishing the polynomial p:


% High precision is required when transferring from double to string.
% You can change it according to your own application but I have tested
% it for degree <= 10 and the error using 2 * degree is following the
% the theoretical bound.
precision = 2 * degree;
p = num2str(a(1), precision);
v = '';
for i = 1:degree
if (x(i) > 0)
sign = '-';
else
sign = '+';
end
if (i == 1)
v = strcat('(x', sign, num2str(x(i), precision), ')');
else
v = strcat(v, '*(x', sign, num2str(x(i), precision), ')');

17
r - Tlf: (+34) 968 27 88 00 -
Asignatura

end
if (a(i + 1) > 0)
p = strcat(p, '+', num2str(a(i + 1), precision), '*', v);
else
p = strcat(p, num2str(a(i + 1), precision), '*', v);
end
end
p = sym(p);
if (verbose)
disp('The polynomial is ...');
pretty(p)
end

>> p = newton_interpolation('exp(x)', 2, [0 1], true)

The starting values are ...


[ 0, 1]
[ 1/2, exp(1/2)]
[ 1, exp(1)]

The newton interpolating poly is formed as \Sigma(a_i\Pi(x-x_j))


a_i coefficients are ...
1.0000
1.2974
0.8417

The polynomial is ...


1.297 x + 0.8417 x (x - 0.5) + 1

18
r - Tlf: (+34) 968 27 88 00 -
Asignatura

p=

1.297*x + 0.8417*x*(x - 0.5) + 1

>>ezplot(p, [-1 2])

El hold on HOLD ON holds the current plot and all axis properties, including
the current color and linestyle, so that subsequent graphing commands
% add to the existing graph without resetting the color and linestyle.

1.297 x + 0.8417 x (x - 0.5) + 1

-1 -0.5 0 0.5 1 1.5 2


x

19
r - Tlf: (+34) 968 27 88 00 -
Asignatura

exp(x)

0
-1 -0.5 0 0.5 1 1.5 2
x
Una grafica es cuando está sola y la otra cuando estan las dos , vemos que entre 0 y 1 son
precisamente las mismas , una vez que se salen ya dejan de ser iguales.
Y esta es la representacion que obtenemos.

20
r - Tlf: (+34) 968 27 88 00 -

También podría gustarte