Docsity

Numerical Methods: Approximation Polynomials and Vandermonde Matrix, Assignments of Introduction to Sociology

Toggle sidebar
bg1
MAE 107 Spring 2007
HW 7 Problem 1 This code interpolates the given data for the conductivity-temperature
function, and evaluates this function at x = 4.
Contents
Setup
(a) Forward Difference Table
(b) Construct Newton-Gregory Polynomial
(c) Using Direct MATLAB Commands
Optimal fit
Setup
clear; close all; clc
% input data point
h = 0.4; % data interval
% temperature
x = 1:h:5;
% conductivity
f = [1.210 1.1968 1.2999 1.4378 1.6183 1.849 2.1376 2.4919 2.9194 3.427 4.025];
(a) Forward Difference Table
n = size(x,2); % data poi
nts (=11)
% construct the table
table(1,:) = x;
table(2,:) = f;
for i = 3:n+1
table(i,1:n-i+2) = diff(table(i-1,1:n-i+3)); %diff(c)=[c(2)-c(1), c(3)-c(2), ..., c(n)-
c(n-1)]
end
fprintf('The forward difference table:\n')
table = table'; disp(table)
The forward difference table:
Columns 1 through 9
1.0000 1.2100 -0.0132 0.1163 -0.0815 0.0893 -0.0895 0.0898 -0.0901
1.4000 1.1968 0.1031 0.0348 0.0078 -0.0002 0.0003 -0.0003 -0.0001
1.8000 1.2999 0.1379 0.0426 0.0076 0.0001 -0.0000 -0.0004 0.0005
2.2000 1.4378 0.1805 0.0502 0.0077 0.0001 -0.0004 0.0001 0.0042
2.6000 1.6183 0.2307 0.0579 0.0078 -0.0003 -0.0003 0.0043 0
3.0000 1.8490 0.2886 0.0657 0.0075 -0.0006 0.0040 0 0
3.4000 2.1376 0.3543 0.0732 0.0069 0.0034 0 0 0
3.8000 2.4919 0.4275 0.0801 0.0103 0 0 0 0
4.2000 2.9194 0.5076 0.0904 0 0 0 0 0
4.6000 3.4270 0.5980 0 0 0 0 0 0
5.0000 4.0250 0 0 0 0 0 0 0
Columns 10 through 12
0.0900 -0.0894 0.0925
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Numerical Methods: Approximation Polynomials and Vandermonde Matrix and more Assignments Introduction to Sociology in PDF only on Docsity!

MAE 107 Spring 2007

HW 7 Problem 1 This code interpolates the given data for the conductivity-temperature

function, and evaluates this function at x = 4.

Contents

Setup

(a) Forward Difference Table

(b) Construct Newton-Gregory Polynomial

(c) Using Direct MATLAB Commands

Optimal fit

Setup

clear; close all; clc % input data point h = 0.4; % data interval % temperature x = 1:h:5; % conductivity f = [1.210 1.1968 1.2999 1.4378 1.6183 1.849 2.1376 2.4919 2.9194 3.427 4.025];

(a) Forward Difference Table

n = size(x,2); % data poi nts (=11) % construct the table table(1,:) = x; table(2,:) = f; for i = 3:n+ table(i,1:n-i+2) = diff(table(i-1,1:n-i+3)); %diff(c)=[c(2)-c(1), c(3)-c(2), ..., c(n)- c(n-1)] end fprintf('The forward difference table:\n') table = table'; disp(table) The forward difference table: Columns 1 through 9 1.0000 1.2100 -0.0132 0.1163 -0.0815 0.0893 -0.0895 0.0898 -0. 1.4000 1.1968 0.1031 0.0348 0.0078 -0.0002 0.0003 -0.0003 -0. 1.8000 1.2999 0.1379 0.0426 0.0076 0.0001 -0.0000 -0.0004 0. 2.2000 1.4378 0.1805 0.0502 0.0077 0.0001 -0.0004 0.0001 0. 2.6000 1.6183 0.2307 0.0579 0.0078 -0.0003 -0.0003 0.0043 0 3.0000 1.8490 0.2886 0.0657 0.0075 -0.0006 0.0040 0 0 3.4000 2.1376 0.3543 0.0732 0.0069 0.0034 0 0 0 3.8000 2.4919 0.4275 0.0801 0.0103 0 0 0 0 4.2000 2.9194 0.5076 0.0904 0 0 0 0 0 4.6000 3.4270 0.5980 0 0 0 0 0 0 5.0000 4.0250 0 0 0 0 0 0 0 Columns 10 through 12 0.0900 -0.0894 0.

(b) Construct Newton-Gregory Polynomial

x0 = 1; % in order to have degree 10 polynomial, one has to start at x0 = 1 s = (4-x0)/h; % construct the coefficients: s, s(s-1)/2!, ..., s(s-1)...(s-n+1)/n! c(1) = s; for i = 2: c(i) = c(i-1)(s-i+1)/i; end % evaluate the polynomial at x = 4 yng = f(1); for i = 3: yng = yng + c(i-2)table(1,i); end fprintf('\n10th-order Newton-Gregory polynomial, f(4) = %8.4f \n\n', yng) 10th-order Newton-Gregory polynomial, f(4) = 2.

(c) Using Direct MATLAB Commands

% (i) evaluate the polynomials made by direct Matlab command at x = 4 fprintf('Polynomials evaluated at x = 4: \n') % construct the polynomials fprintf('Polynomials: \n') p1 = polyfit(x,f,1) p2 = polyfit(x,f,2) p3 = polyfit(x,f,3) p5 = polyfit(x,f,5) p8 = polyfit(x,f,8) p10 = polyfit(x,f,10) % evaluate at x= fprintf('Evaluations: \n') y1 = polyval(p1,4) y2 = polyval(p2,4) y3 = polyval(p3,4) y5 = polyval(p5,4) y8 = polyval(p8,4) y10 = polyval(p10,4) % (ii) compare the errors for degree 1, 3, and 5 polynomials

Columns 10 through 11 -46.7520 13. Evaluations: y1 =

y2 =

y3 =

y5 =

y8 =

y10 =

The variance of degree 1, 3, and 5 polynomials: s1 =

s3 = 2.3905e- s5 = 4.3216e-

Optimal fit

To find the optimal fit, we take a look at the variances, defined as,

Fot this problem, s3 is small enough, but s5 is even smaller. One could use the 3rd order

polynomial to avoid "over-fitting". Also, from the plots, 5th order polynomial has no

advantage over the 3rd order polynomial. Thus p=3 or p=5 is fine.

Published with MATLAB® 7.

Plot

plot the approximation polynomials and the exact function define the plot range with a fine

resolution of 100 intervals

xx = 0:.03:3; % evaluation of the polynomial and the cubic spline approximations and the real function y3 = polyval(p3,xx); % 3rd degree polynomial y6 = polyval(p6,xx); % 6th degree polynomial y10 = polyval(p10,xx); % 10th degree polynomial ysp = spline(x,y,xx); % cubic spline interpolation yhp = interp1(x,y,xx,'pchip'); % cubic Hermite spline yo = f(xx); % exact function % plot figure(2), plot(x,y,'o','LineWidth',3); hold on; plot(xx,yo,'-', xx,y3,'--', xx,y6,'-.', xx,y10,':', xx,ysp,'-','LineWidth',2) %plot(xx,yhp,'-.b') title('hw7, problem2: polynomial and cubic spline fits to f(x)') xlabel('T'), ylabel('k'), legend('data','exact function','deg = 3','deg = 6','deg = 10','c ubic spline',0) % compute the error of the polynomials and cubic spline at x=0.2 and x=2. % x = 0. fprintf('\nthe error of the interpolations at x = 0.2: \n') xe_1 = 0.2; ye_1 = f(xe_1); e3_1 = abs( ye_1 - polyval(p3,xe_1) ) e6_1 = abs( ye_1 - polyval(p6,xe_1) ) e10_1 = abs( ye_1 - polyval(p10,xe_1) ) esp_1 = abs( ye_1 - spline(x,y,xe_1) ) % x = 2. fprintf('\nthe error of the interpolations at x = 2.8: \n') xe_2 = 2.8; ye_2 = f(xe_2); e3_2 = abs( ye_2 - polyval(p3,xe_2) ) e6_2 = abs( ye_2 - polyval(p6,xe_2) ) e10_2 = abs( ye_2 - polyval(p10,xe_2) ) esp_2 = abs( ye_2 - spline(x,y,xe_2) ) the error of the interpolations at x = 0.2: e3_1 =

e6_1 =

e10_1 =

esp_1 =

the error of the interpolations at x = 2.8:

the error of the interpolations at x = 2.8: e3_2 =

e6_2 =

e10_2 =

esp_2 =

Comment

From the plot, the exact function has high variation region (x < 1.5) and a rather flat region

(x > 1.5). One can find that the lower order polynomial has rough fitting at high variation

region while the higher order polynomial has a better fit. But teh higher order polynomials

experience undesired large variations close to the end points, especially the 10th order

polynomial, which is actually the Lagrangian polynomial. By comparing the error evaluated

at two points close to the end points and the plots for different approximation polynomials,

MAE 107 Spring 2007

HW 7 Problem 3

Contents

Function to Construct Vandermonde Matrix

Vandermonde Matrix for N = 5

Vandermonde Matrix for N = 10

Comparison

Comment

Function to Construct Vandermonde Matrix

clear; close all; clc type vandermonde.m function V = vandermonde(N) x = [0:N-1]'; V = ones(N,1); for i = 1:N- V = [V x.^(i)]; end end

Vandermonde Matrix for N = 5

V = vandermonde(5) c5 = cond(V) V = 1 0 0 0 0 1 1 1 1 1 1 2 4 8 16 1 3 9 27 81 1 4 16 64 256 c5 = 2.5929e+

Vandermonde Matrix for N = 10

V = vandermonde(10) c10 = cond(V)

V =

Columns 1 through 7 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 4 8 16 32 64 1 3 9 27 81 243 729 1 4 16 64 256 1024 4096 1 5 25 125 625 3125 15625 1 6 36 216 1296 7776 46656 1 7 49 343 2401 16807 117649 1 8 64 512 4096 32768 262144 1 9 81 729 6561 59049 531441 Columns 8 through 10 0 0 0 1 1 1 128 256 512 2187 6561 19683 16384 65536 262144 78125 390625 1953125 279936 1679616 10077696 823543 5764801 40353607 2097152 16777216 134217728 4782969 43046721 387420489 c10 = 9.0078e+

Comparison

The log of condition number represents number of significant digits affected by rounding

errors

disp('Number of significant digits affected for N=5:'); disp(fix(log10(c5))) disp('Number of significant digits affected for N=10:'); disp(fix(log10(c10))) Number of significant digits affected for N=5: 3 Number of significant digits affected for N=10: 10

Comment

We can see that the condition number rapidly increases for N=5 to N=10. This means that

the Vandermonde matrix becomes ill-conditioned very fast. So using it for polynomial

regression for N>5 will lead to substantial round-off errors. Thus it cant be used for N

greater than about 5.

Published with MATLAB® 7.