- Create an LU-decomposition function called lu_tridiag.m that takes 3 vectors as inputs and calculates the LU-decomposition of a tridiagonal matrix. The output should be 3 vectors, the diagonal of the Upper matrix, and the two off-diagonal vectors of the Lower and Upper matrices.
function [ud,uo,lo]=lu_tridiag(e,f,g)
%Takes main diaganol and diaganol to the top and bottom and make matrix A
%and then an Identity matrix B is made.
%The matrix A is then turned into the upper matrix while B turns into the
%lower matrix in LU decomposition.
%The function outputs the main diagnol of the upper matrix and the off
%diagnol of the lower and upper matrices.
A=diag(e,-1)+ diag(f)+diag(g,1);
B=eye(length(f));
for i=1:length(e)
x=A(i+1,i)/A(i,i);
A(i+1,i)= (-e(i)/f(i))*f(i)+e(i);
A(i+1,i+1)= (-x)*g(i)+f(i+1);
B(i+1,i)=x;
ud(1)=A(1,1);
ud(i+1)= A(i+1,i+1);
uo(i)=A(i,i+1);
lo(i)=B(i+1,i);
end
end
- Use the output from lu_tridiag.m to create a forward substitution and
back-substitution function called solve_tridiag.m that provides the solution of
Ax=b given the vectors from the output of [ud,uo,lo]=lu_tridiag(e,f,g). Note: do not use
the backslash solver
\
, create an algebraic solution
function [x]=solve_tridiag(ud,uo,lo,b)
%Takes the main diagnol of the upper matrix and the off
%diagnol of the lower and upper matrices and the vector b.
%This function solves for the vector x.
y=zeros(1,length(ud));
for i = 1:(length(ud)-1)
y(1)=b(1);
y(i+1)= b(i+1)-(lo(i)*y(i));
end
x=zeros(1,length(ud));
for k =(length(ud):-1:2)
x(length(ud))=y(length(ud))/ud(length(ud));
x(k-1)= (y(k-1)-(uo(k-1)*x(k)))/(ud(k-1));
end
4)Test your function on the matrices A3, A4, ..., A10 generated with test_arrays.m Solving for b=ones(N,1); where N is the size of A. In your README.md file, compare the norm of the error between your result and the result of AN\b.
size of A | norm(error) |
---|---|
3 | 1.2413e-16 |
4 | 2.4197e-16 |
5 | 3.9252e-16 |
6 | 6.7987e-17 |
7 | 7.0869e-17 |
8 | 5.1029e-16 |
9 | NaN |
10 | 2.2485e-17 |
To determine the error I used the norm function in matlab; norm(my answer-backslash operator).
5)In the system shown above, determine the three differential equations for the position of masses 1, 2, and 3. Solve for the vibrational modes of the spring-mass system if k1=10 N/m, k2=k3=20 N/m, and k4=10 N/m. The masses are m1=1 kg, m2=2 kg and m3=4 kg. Determine the eigenvalues and natural frequencies
The Eigen Values and Natural Frequencies are solved for, the Natural Frequencies are in the units of rad/s.
- Determine the eigenvalues for a 5-segment (4-interior nodes), 6-segment (5-interior nodes), and 10-segment (9-interior nodes).
Using the above handwritten calulations and matlab, the # of eigen values and the smallest and largest for each number ofsegments are shown in the table below.
# of segments | largest | smallest | # of eigenvalues |
---|---|---|---|
5 | 3.610 | 0.3820 | 4 |
6 | 5.3776 | 0.3861 | 5 |
10 | 15.6085 | 0.3915 | 9 |
If the segment length approaches 0, the number of eigen values would grow towards infinity.