Problem number 5 Problem 1: Perform Gauss elimination with partial pivoting by h
ID: 3348455 • Letter: P
Question
Problem number 5
Problem 1: Perform Gauss elimination with partial pivoting by hand for the following A and b -2.5 11.5 1.5 1-5 7-1 A= Use MATIAB's left division to check your answer. Problem 2: Write a MATLAB script to perform Gauss elimination with partial pivoting for the A Problem 3: Write a MATLAB function called gauss_elin to perform Gauss elimination with partial Problem 4: Compte the L, U, and P matrices for A from proble by hand, and use those and b from problem 1. pivoting for a given A and b. A and b diould be inputs. and?should be an output. matrices to solve Az = b for b from problem I. Then, use your LU factorization to solve Az b for b-4, 2,-1, 3 Problem 5: Write a MATLAB function called lu fact to perform LU factorization with pivoting for a given A. L, U, and P should be outputs. Then use your function to perform Gauss elimination for A and b from problem 1. Then, without calling your function a second time, solve Ax = b for the same A but with b from problem 4.Explanation / Answer
function dummy=solve_Ax_b()
clc;
clear all;
A = [
3 2 4 1
1 -5 7 -1
-6 8 9 1
3 3 3 3 ];
b=[6;-2.5;11.5;1.5];
[ P, L, U ] = LUdecomposition(A)
z1= transpose(P)*b;
z2=Gauss_elimination(L, z1);
disp('Solution of Ax=b using Lu decompostion')
y=Gauss_elimination(U, z2)
b1=[4;2;-1;3]
disp('Without using function')
sol=inv(A)*b1
function [ P, L, U ] = LUdecomposition(A)
% Ensures A is n by n
[n,n]=size(A);
L=eye(n); P=L; U=A;
for k=1:n
[pivot m]=max(abs(U(k:n,k)));
m=m+k-1;
if m~=k
% interchange rows m and k in U
temp=U(k,:);
U(k,:)=U(m,:);
U(m,:)=temp;
% interchange rows m and k in P
temp=P(k,:);
P(k,:)=P(m,:);
P(m,:)=temp;
if k >= 2
temp=L(k,1:k-1);
L(k,1:k-1)=L(m,1:k-1);
L(m,1:k-1)=temp;
end
end
for j=k+1:n
L(j,k)=U(j,k)/U(k,k);
U(j,:)=U(j,:)-L(j,k)*U(k,:);
end
end
end
function x = Gauss_elimination(A, b)
% Solve linear system Ax = b
% using Gaussian elimination without pivoting
% A is an n by n matrix
% b is an n by k matrix (k copies of n-vectors)
% x is an n by k matrix (k copies of solution vectors)
[n, n] = size(A); % Find size of matrix A
[n, k] = size(b); % Find size of matrix b
x = zeros(n,k); % Initialize x
for i = 1:n-1
m = -A(i+1:n,i)/A(i,i); % multipliers
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);
end;
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
end
end
%%%%%% solution %%%%%%%
P =
0 0 1 0
0 0 0 1
0 1 0 0
1 0 0 0
L =
1.0000 0 0 0
-0.5000 1.0000 0 0
-0.1667 -0.5238 1.0000 0
-0.5000 0.8571 0.1667 1.0000
U =
-6.0000 8.0000 9.0000 1.0000
0 7.0000 7.5000 3.5000
0 0 12.4286 1.0000
0 0 0 -1.6667
Solution of Ax=b using Lu decompostion
y =
-2.2017
-2.9075
0.3259
8.6167
b1 =
4
2
-1
3
Without using function
sol =
0.9138
0.2931
0.2931
-0.5000
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.