Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Exercises This lab assignment includes three items. You will write a file co

ID: 3870898 • Letter: L

Question

Lab Exercises This lab assignment includes three items. You will write a file containing psuedocode, one Matlab function and one Matlab script. The script ca the function and your psuedocode must be robust enough to be a clear basis for the function and script files. The function you will write must implement the method of Naïve Gauss Elimination. Here is the official assignment: 1. Psuedocode Write psuedocode for a function implementing Naive Gauss Elimination. Save this pseudocode to a file called NaiveGaussPseudocode.txt and submit it. Since you wil be writing real code based on this pseudocode, you should take this step seriously

Explanation / Answer

% Code from "Gauss elimination and Gauss Jordan methods using MATLAB"
% https://www.youtube.com/watch?v=kMApKEKisKE

a = [3 4 -2 2 2
4 9 -3 5 8
-2 -3 7 6 10
1 4 6 7 2];
%Gauss elimination method [m,n)=size(a);
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
for s=m:-1:1
c=0;
for k=2:m
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
disp('Gauss elimination method:');
a
x'
% Gauss-Jordan method
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(1,:);a(1,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end

for j=m:-1:2
for i=j-1:-1:1
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
for s=1:m
a(s,:)=a(s,:)/a(s,s);
x(s)=a(s,n);
end
disp('Gauss-Jordan method:');
a
x'

function [pi] = GE(Q)

A = Q';
n = size(A);
for i=1:n-1
for j=i+1:n
A(j,i) = -A(j,i)/A(i,i);
end
for j =i+1:n
for k=i+1:n
A(j,k) = A(j,k)+ A(j,i) * A(i,k);
end
end
end

x(n) = 1;
for i = n-1:-1:1
for j= i+1:n
x(i) = x(i) + A(i,j)*x(j);
end
x(i) = -x(i)/A(i,i);
end

pi = x/norm(x,1);

function x = naiv_gauss(A,b);
n = length(b); x = zeros(n,1);
for k=1:n-1 % forward elimination
for i=k+1:n
xmult = A(i,k)/A(k,k);
for j=k+1:n
A(i,j) = A(i,j)-xmult*A(k,j);
end
b(i) = b(i)-xmult*b(k);
end
end
% back substitution
x(n) = b(n)/A(n,n);
for i=n-1:-1:1
sum = b(i);
for j=i+1:n
sum = sum-A(i,j)*x(j);
end
x(i) = sum/A(i,i);
end
end

n = size(A);
n = n(1);

B = zeros(n,1);

% We have 3 steps for a 4x4 matrix so we have
% n-1 steps for an nxn matrix
for k = 1 : n-1   
for i = k+1 : n
% step 1: Create multiples that would make the top left 1
% printf("multi = %d / %d ", A(i,k), A(k,k), A(i,k)/A(k,k) )
for j = k : n
A(i,j) = A(i,j) - (A(i,k)/A(k,k)) * A(k,j);
end
B(i) = B(i) - (A(i,k)/A(k,k)) * B(k);
end
end

function [x,U] = gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute An=Mn*An-1, bn=Mn*bn-1
for i=k+1:n;
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end;
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end;
end
function [x,U]=gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end
end
After this try to run the code:

A=rand(5,4);
b=rand(4,1);
[x,U] = gausselim(A,b)

Sub New(ByVal Number As Decimal)

Dim ResultingFraction As New Fraction

Dim k As Double = 1
If Number < 0 Then
k = -1
End If

ResultingFraction = GetFraction(Math.Abs(Number))
ResultingFraction.Numerator *= k

Me.Numerator = ResultingFraction.Numerator
Me.Denominator = ResultingFraction.Denominator
End Sub

Private Function GetFraction(ByVal d As Decimal)
Dim Temp As Decimal = d

Dim list As New List(Of Decimal)

For i As Integer = 0 To 1000
list.Add(Math.Truncate(Temp))
Dim ff As Decimal = GetContinuedFraction(list).ToDouble
If d = ff Then
Exit For
End If

Try
Temp = 1 / (Temp - Math.Truncate(Temp))
Catch ex As Exception
Exit For
End Try
Next


Return GetContinuedFraction(list)
End Function

Private Function GetContinuedFraction(ByVal d As List(Of Decimal)) As Fraction
Dim result As New Fraction
result.Numerator = d(d.Count - 1)
result.Denominator = 1


For i As Integer = d.Count - 2 To 0 Step -1
If Not result.Denominator = 0 Then
result = New Fraction(d(i), 1) + 1 / result
End If
Next

Return result