Homework 6 Exercise 1 First Name: Last Name:

advertisement
Numerical Methods
MATH 417 - 501/502
A. Bonito
March 2
Spring 2016
Last Name:
First Name:
Homework 6
Exercise 1
40% (BY HAND)
Gauss-Lobatto quadrature
For 0 < α < 1, let t0 = −1, t1 = −α, t2 = α, t3 = 1 and let ω0 ,...,ω3 be real numbers. Consider
the quadrature formula given by
J(g) =
3
X
Z
1
ωj g(tj ) ≈
g(t)dt.
−1
j=0
R1
1. Find ωj , j = 0, ..., 3 depending on α such that J(p) = −1 p(t)dt for every p polynomial of
degree 6 3.
R1
2. Is there an α such that J(p) = −1 p(t)dt for every p polynomial of degree 6 r, where r > 3?
If yes, find the largest r and the corresponding values for α and ωj , j = 0, ..., 3.
3. Approximate the integral
Z
3
(t3 − t2 )dt
−1
using the above quadrature.
4. How far is the approximate value from the exact value?
Exercise 2
30% (MATLAB)
Replace the symbols X in the matlab code below to solve the linear system
Ax = b
when A is tri-diagonal, i.e. the coefficients satisfy aij = 0 whenever |i − j| > 1.
%%%% GaussTri %%%%
%% Input: tridiagonal square matrix A (not checked)
%%
vector b of corresponding size (not checked)
%% Output: solution to Ax=b
function
x=GaussTri(A,b)
N=size(A,1);
for i=1:N-1
%multiply the ith row by pivot
p=1/A(i,i);
A(i,X) = p*A(i,X);
b(i) = p*b(i);
% eliminate the ith column
A(i+1,i+1)=A(i+1,i+1)-X;
b(i+1) = b(i+1)-A(X,X)*b(i);
end
%last step
p = 1/A(N,N);
b(N)=p*b(N);
%once the matrix is upper triangular (with one on the diagonal)
%solve (the solution is stored in b)
for i=N-1:-1:1
b(i)=b(i)-A(i,i+1)*b(i+1);
end
x=b;
%%%% END %%%%
Test your routine with
A=gallery('tridiag',50,-1,2,-1);
b=[1:50].';
Exercise 3
30% (MATLAB)
The matlab code below is an implementation of the Gaussian elimination algorithm to solve the
linear system
Ax = b.
As we shall see in class, it may happen that the algorithm find a vanishing (or small) pivot. To
cope with this issue, at the begining of the first for loop, it is necessary to flip the ith row with
the jth row such that j > i and |aji | = maxk>i |aki |. This is called partial pivoting. Modify the
Gaussian algorithm below to invlude partial pivoting. Test your routine with
A=[0,1,3;5,2,3;6,8,1];
b=[1;4;1];
%%%% Gauss %%%%
%% Input:
Square matrix A (not checked)
%%
vector b of corresponding size (not checked)
%%
%% Output: solution to Ax=b
function x=Gauss(A,b)
N=size(A,1);
for i=1:N-1
% multiply ith row by pivot
p=1/A(i,i);
for j=i+1:N
A(i,j) = p*A(i,j);
end
b(i) = p*b(i);
% eliminate the ith column
for k=i+1:N
for j=i+1:N
A(k,j)=A(k,j)-A(k,i)*A(i,j);
end
b(k) = b(k)-A(k,i)*b(i);
end
end
% last row
p = 1/A(N,N);
b(N)=p*b(N);
% solve for x
for i=N-1:-1:1
for j=i+1:N
b(i) = b(i) - A(i,j)*b(j);
end
end
x=b;
%%% END %%%
Download