Question
Please answer this MATLAB questions when able. Thanks.

4. Laboratory Problem Description In this laboratory you are required to Find the solution of the following systems of linear
0 0
Add a comment Improve this question Transcribed image text
Answer #1

4 (1) Matlab Code

function x = GE(A,b)
%For 4 (1)
A=[1, 1, 1; 4, -1, -1; 1, 2, -1];
b=[3, 2, 2];
b=b';

[m,n]= size(A);
if m ~= n
disp('Not a square');
end

for p=1:n
array(p)=p;
end%for

A = [A,b];

%elimination
for i = 1:n-1
pivot = i;
%select pivot
for j = i+1:n
if abs(A(array(i),i)) < abs(A(array(j),j)) %row interchange <-------
temp = array(i);
array(i) = array(j);
array(j) = temp;

end
end

while (pivot <= n && A(pivot,i)== 0)
pivot = pivot+1;
end
if pivot > n
disp('No unique solution');
break
else
if pivot > i
tem = array(i);
array(i) = pivot
pivot= tem;
end
end

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

if A(n,n) == 0
disp('No unique solution');
return
end

%backward substitution
x(n) = A(array(n),n+1)/A(array(n),n);
for i = n - 1:-1:1
sum = 0;
for j = i+1:n
sum = sum + A(array(i),j)*x(j);
end
x(i) = (A(array(i),n+1) - sum)/A(array(i),i);
end

end%function

Output

ans =

1 1 1

4 (2)

Matlab Code

function x = GE(A,b)
%For 4 (2)
A=[2, -1, 3; 1, 3, -2; 3, 0, 5];
b=[1, 2, 3];
b=b';

[m,n]= size(A);
if m ~= n
disp('Not a square');
end

for p=1:n
array(p)=p;
end%for

A = [A,b];

%elimination
for i = 1:n-1
pivot = i;
%select pivot
for j = i+1:n
if abs(A(array(i),i)) < abs(A(array(j),j)) %row interchange <-------
temp = array(i);
array(i) = array(j);
array(j) = temp;

end
end

while (pivot <= n && A(pivot,i)== 0)
pivot = pivot+1;
end
if pivot > n
disp('No unique solution');
break
else
if pivot > i
tem = array(i);
array(i) = pivot
pivot= tem;
end
end

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

if A(n,n) == 0
disp('No unique solution');
return
end

%backward substitution
x(n) = A(array(n),n+1)/A(array(n),n);
for i = n - 1:-1:1
sum = 0;
for j = i+1:n
sum = sum + A(array(i),j)*x(j);
end
x(i) = (A(array(i),n+1) - sum)/A(array(i),i);
end

end%function

Output

ans =

0.285714285714285 0.857142857142857 0.428571428571429

Matlab code 4 last part

function x = GE(A,b)
%For 4 (3)
A=[4, 3, 7; 3, 2, 1; 2, 3, 4];
[L U P]=lu(A)
b=[3, 1, 2];
b=b';

[m,n]= size(A);
if m ~= n
disp('Not a square');
end

for p=1:n
array(p)=p;
end%for

A = [A,b];

%elimination
for i = 1:n-1
pivot = i;
%select pivot
for j = i+1:n
if abs(A(array(i),i)) < abs(A(array(j),j)) %row interchange <-------
temp = array(i);
array(i) = array(j);
array(j) = temp;

end
end

while (pivot <= n && A(pivot,i)== 0)
pivot = pivot+1;
end
if pivot > n
disp('No unique solution');
break
else
if pivot > i
tem = array(i);
array(i) = pivot
pivot= tem;
end
end

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

if A(n,n) == 0
disp('No unique solution');
return
end

%backward substitution
x(n) = A(array(n),n+1)/A(array(n),n);
for i = n - 1:-1:1
sum = 0;
for j = i+1:n
sum = sum + A(array(i),j)*x(j);
end
x(i) = (A(array(i),n+1) - sum)/A(array(i),i);
end

end%function

Output

L =

1.000000000000000 0 0
0.500000000000000 1.000000000000000 0
0.750000000000000 -0.166666666666667 1.000000000000000


U =

4.000000000000000 3.000000000000000 7.000000000000000
0 1.500000000000000 0.500000000000000
0 0 -4.166666666666667


P =

1 0 0
0 0 1
0 1 0


ans =

0.080000000000000 0.240000000000000 0.280000000000000

Matlab Code

clc
clear all
A=[4 3 7;3 2 1;2 3 4];
b=[3 1 2]';
x=[0 0.2 0.3]';
% x=zeros(n,1);
n=size(x,1);
normVal=Inf;
%%
% * _*Tolerence for method*_

tol=0.01; itr=0;
%% Algorithm: Gauss Seidel Method
%%
while normVal>tol
x_old=x;
  
for i=1:n
  
sigma=0;
  
for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end
  
for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end
  
x(i)=(1/A(i,i))*(b(i)-sigma);
end
  
itr=itr+1;
normVal=norm(x_old-x);
end
x;
itr

Output

x =

0.078588867187500
0.243103027343750
0.278378295898437


itr =

3

5 (1). Matlab Code

A=[1 4 3;2 1 -1;3 -1 -4];
b=[10 -1 11]';
mldivide(A,b)

output

Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 9.251859e-18.

ans =

1.0e+16 *

-4.850030367937457
4.850030367937457
-4.850030367937457

5.(2) Matlab code for 1 and 2(a)

A=[5 1 1 2;2 4 0 -1;1 0 3 0;2 -1 3 8];
[L U P]=lu(A)
b=[8 -3 7 17]';
mldivide(A,b)

Output

L =

1.000000000000000 0 0 0
0.400000000000000 1.000000000000000 0 0
0.200000000000000 -0.055555555555556 1.000000000000000 0
0.400000000000000 -0.388888888888889 0.880000000000000 1.000000000000000


U =

5.000000000000000 1.000000000000000 1.000000000000000 2.000000000000000
0 3.600000000000000 -0.400000000000000 -1.800000000000000
0 0 2.777777777777778 -0.500000000000000
0 0 0 6.940000000000000


P =

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1


ans =

1.000000000000000
-1.000000000000000
2.000000000000000
1.000000000000000

5.(2)(b)

Matlab Code

clc
clear all
A=[5 1 1 2;2 4 0 -1;1 0 3 0;2 -1 3 8];
b=[8 -3 7 17]';
x=[0 0 0 0]';
% x=zeros(n,1);
n=size(x,1);
normVal=Inf;
%%
% * _*Tolerence for method*_

tol=0.001; itr=0;
%% Algorithm: Gauss Seidel Method
%%
while normVal>tol
x_old=x;
  
for i=1:n
  
sigma=0;
  
for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end
  
for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end
  
x(i)=(1/A(i,i))*(b(i)-sigma);
end
  
itr=itr+1;
normVal=norm(x_old-x);
end
x;
itr

Output

x =

1.000116018281966
-1.000079239141597
1.999961327239345
0.999975592822055


itr =

8
Matlab code for 5.2.c)

clc
clear all
A=[5 1 1 2;2 4 0 -1;1 0 3 0;2 -1 3 8];
b=[8 -3 7 17]';
x=[0.5 -0.2 1 0.2]';
% x=zeros(n,1);
n=size(x,1);
normVal=Inf;
%%
% * _*Tolerence for method*_

tol=10^-6; itr=0;
%% Algorithm: Gauss Seidel Method
%%
while normVal>tol
x_old=x;
  
for i=1:n
  
sigma=0;
  
for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end
  
for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end
  
x(i)=(1/A(i,i))*(b(i)-sigma);
end
  
itr=itr+1;
normVal=norm(x_old-x);
end
x
itr

Output

x =

1.000000149681997
-1.000000102231069
1.999999950106001
0.999999968510867


itr =

13

Add a comment
Know the answer?
Add Answer to:
Please answer this MATLAB questions when able. Thanks. 4. Laboratory Problem Description In this laboratory you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT