Sei sulla pagina 1di 12

Math 5334: Homework 3 Solutions

March 6, 2009

Problem 3.3
x = [-1 -0.96 -0.65 0.1 0.4 1]’;
y = [-1 -0.1512 0.3860 0.4802 0.8838 1]’;
u = (-1:0.01:1)’;

v1 = piecelin(x,y,u);
v2 = polyinterp(x,y,u);
v3 = splinetx(x,y,u);
v4 = pchiptx(x,y,u);

% plot the data points


plot(x,y,’o’)
% plot the 4 interpolations on the same graph
hold on
plot(u,v1)
plot(u,v2,’r’)
plot(u,v3, ’g’)
plot(u,v4, ’k’)

% label the four plots and label the graph


legend(’(x,y)’, ’piecelin’, ’polyinterp’, ’splinetx’, ’pchiptx’)
title(’HW2, problem 3.3’);
xlabel(’u’);
ylabel(’v = P(u)’);

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 1
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

% evaluate each interpolant at x = -0.3


piecelin(x,y,-0.3)
ans = 0.4300

polyinterp(x,y,-0.3)
ans = -0.9990

splinetx(x,y,-0.3)
ans = -0.1957

pchiptx(x,y,-0.3)
ans = 0.4322

I prefer the value at x = −0.3 given by pchiptx. The point x = −0.3 lies in the interval between x3 =
−0.65 and x4 = 0.10, but the values of P (−0.3) given by polyinterp and splinetx lie significantly
below y3 and y4 . In fact, the given y values of data are monotonically increasing with x. So we
have no reason to suspect that the true function actually oscillates as polyinterp and splinetx do.
That leaves pchiptx and piecewise linear, which give very similar answers for P (−0.3). Since
pchiptx also produces a smooth function, I prefer pchiptx for this data.
To estimate the coefficients of the low-degree polynomial used to generate the date, we can
try using a Vandermonde matrix. In general, it is a bad idea to compute using the Vandermonde
matrix. But since cond(vander(x)) isn’t too large (≈ 222), we will try it.

format long
c = vander(x)\y

c =

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 2
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

16.001760721584741
0.000671813407319
-20.002172969527788
-0.000679471509077
5.000412247943046
0.000007658101757

This is an inaccurate computation, so we will guess the actual coefficients to be these coefficients
rounded to the nearest integer. That gives the polynomial:

y = 16x5 − 20x3 + 5x

which appears again in problem 3.8 as the power form of the Chebyshev polynomial of degree five.
We can check if our guess is right by evaluating this polynomial at the 6 given data points:
testy = 16*x.^5 - 20*x.^3 + 5*x

testy =
-1.000000000000000
-0.151243161600001
0.386035000000000
0.480160000000000
0.883840000000000
1.000000000000000

which equals our given y data values to the four digits of accuracy given.

Problem 3.4

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 3
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

The pchiptx interpolation seems to make a smoother, nicer looking, version of a hand. The
splinetx version has some unrealistic looking sharp turns.
For the same reasons, I’d would think that the hand picture in the book was done with pchiptx.

Problem 3.8
See prob3 8.m for the Matlab code. All six representations are plotted here. We can see that they

represent the same function since the plots are identical.

Problem 4.2
(a) Skipped
(b) The roots function computes the roots of a polynomial.
% C = [cubic coeff, quad coeff; linear coeff, const coeff]
C = [1 0 -2 -5];

roots(C)

ans =
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i

The roots function found all three roots.


(c) fzerotx takes an anonymous function f and an interval on which to look for the root. We know
that the real root lies in [2, 3], so we’ll use that for the interval.

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 4
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

% make an anonymous function for the polynomial


f = @(x) x.^3 - 2*x - 5

f =
@(x)x.^3-2*x-5

x = fzerotx(f,[2,3])

x =
2.0946

(d) For Newton’s method, we need f (x), f 0 (x), and an initial guess. I’ll use i = −1 as the
initial guess in order to find a complex root.

f = @(x) x.^3 - 2*x - 5

f =
@(x)x.^3-2*x-5

fprime = @(x) 3*x.^2 - 2

fprime =
@(x)3*x.^2-2

help mynewton2
function x = mynewton2(f,fprime,x0)

define funtions f and fprime as inlines or m-files


finds a root of f using initial guess x0

x = mynewton2(f,fprime,i)
-1
-0.5627487397187583
-0.7783722615045087
-1.042308494125078
-1.046911787596
-1.047274787446181
-1.047275740770723
-1.047275740771163
-1.047275740771163

x =
-1.0473 + 1.1359i

My Newton code is:

function x = mynewton2(f,fprime,x0)

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 5
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

% function x = mynewton2(f,fprime,x0)
%
% define functions f and fprime as inlines or m-files
% finds a root of f using initial guess x0
%
% [Note that anything I put in the initial comment lines
% after the function definition will show up when I type
% "help mynewton2"]

x = x0;

xprev = rand(1);
while abs(x - xprev) > eps*abs(x)
xprev = x;
x = x - f(x)/fprime(x);

disp(sprintf(’%5.16g’, x));

end

(e) There is no notion of ordering on the complex numbers. So we cannot use the bisection
method to find a complex root. There is no notion of one complex number being greater than
zero and another being less than zero.

Problem 4.14
For both of the forward interpolation methods (a) and (b), we need to define a function as the
interpolant minus 60, then find a zero of that function.
For each of the methods, we need v and d vectors:
v = 0:10:60

v =
0 10 20 30 40 50 60

d = [0 5 20 46 70 102 153]

d =
0 5 20 46 70 102 153

(a) Define f 1 as the piecewise linear interpolant minus 60, then find a zero:

f1 = @(x) piecelin(v,d,x) - 60;


fzerotx(f1,[30,40])
ans =
35.8333

(b) Define f 2 as a piecewise cubic interpolant minus 60, then find a zero:

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 6
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

f2 = @(x) pchiptx(v,d,x) - 60;


fzerotx(f2,[30,40])
ans =
36.0007

(c) For reverse piecewise cubic interpolation, we note that the distance measurements are mono-
tonically increasing and switch the roles of v and d. In this case, we do not need to use a zero
finder, but simply evaluate the reverse piecewise cubic interpolant at 60:

% reverse piecewise cubic


pchiptx(d,v,60)
ans =
35.9876

As noted in the problem, because the data are well-behaved, the three values obtained are close
to each other, but not identical.

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 7
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

BF Problem 6.1 #4

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 8
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
(a)
Math 5334 — HW03 Solutions March 6, 2009

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 9
(b)
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

BF Problem 7.1 #5
(a) A = [1/2 1/3; 1/3 1/4];
b = [1/63; 1/168];
x = [1/7; -1/6];
xtilde = [0.142; -0.166];

norm(x - xtilde, inf)


ans =
8.5714e-04

norm(A*xtilde - b, inf)
ans =
2.0635e-04

(b) A = [1 2 3; 2 3 4; 3 4 6];
b = [1; -1; 2];
x = [0; -7; 5];
xtilde = [-0.33; -7.9; 5.8];

norm(x - xtilde, inf)


ans =
0.900000000000000

norm(A*xtilde - b, inf)
ans =
0.270000000000000

(c) A = [1 2 3; 2 3 4; 3 4 6];
b = [1; -1; 2];
x = [0; -7; 5];
xtilde = [-0.2; -7.5; 5.4];
norm(x - xtilde, inf)

ans =
0.500000000000000

norm(A*xtilde - b, inf)
ans =
0.299999999999997

(d) A = [0.04 0.01 -0.01; 0.2 0.5 -0.2; 1 2 4];


x = [1.827586; 0.6551724; 1.965517];
xtilde = [1.8; 0.64; 1.9];

norm(x - xtilde, inf)


ans =
0.065517000000000

b = [0.06; 0.3; 11]

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 10
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

norm(A*xtilde - b, inf)
ans =
0.320000000000000

BF Problem 2.2 #6
For a fixed point iteration to find a solution of x3 − x − 1 = 0, we first need to convert the equation
to a fixed point equation x = g(x). The simplest to try might be x = x3 − 1 = g(x), but when we
iterate with this form, the solution diverges. Another version to try is

x3 − x − 1 = 0
x3 = x + 1

x = 3x+1
= g(x)

This leads to a successful fixed point iteration.


I wrote a simple Matlab function to do a fixed point iteration on a function until |f (p)−p| < tol,
where tol is a given tolerance.

function p = myfixedpt(f, p0, tol)


% function p = myfixedpt(f, p0, tol)
%
% fixed point iteration on f until |f(p) - p| < tol

p = f(p0);
count = 1;

while abs(p - p0) > tol


p0 = p;
p = f(p0)
count = count + 1;
end

We can write g(x) as a Matlab anonymous function and iterate.

g = @(x) (x + 1)^(1/3);

p0 = 1;

tol = 1e-2;

p = myfixedpt(g,1,1e-2)
p =
1.312293836683289

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 11
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.
Math 5334 — HW03 Solutions March 6, 2009

We can compare this with the Matlab roots function to make sure we got a root of the original
equation:

roots([1 0 -1 -1])

ans =
1.324717957244745
-0.662358978622373 + 0.562279512062301i
-0.662358978622373 - 0.562279512062301i

The fixed point method did locate a real root of the original equation, and got it to within the two
digits of accuracy we asked for.

Copyright 2009, Victoria Howle and Department of Mathematics & Statistics, Texas Tech 12
University. All rights reserved. No part of this document may be reproduced, redistributed, or
transmitted in any manner without the permission of the instructor.

Potrebbero piacerti anche