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
Please answer this MATLAB questions when able. Thanks. 4. Laboratory Problem Description In this laboratory you...
Please answer these Matlab questions when able. Thanks. 4. Laboratory Problem Description In this laboratory you are required to find eigenvalue and eigenvectors for A in the following systems of linear equation Ax-B: 17x1+2x2+3x3+4x44 5x1+6x2+7x3+8x4 3 9x1+10x2+11x3+12x4 2 13x1+14x2+15x3+16x4 1 Apply the following commands: >>A 1 12:211 Verify that the columns of X in the example above indeed are eigenvectors to the matrix A. In other words, multiply (using Matlab) the matrix A to the columns (one at a time)...
Q1 The linear system Ax = b is given by: x1−x2 + 4x3 = 7 4x1 + 2x2 –x3= 18, x1 + 3x2+ x3 = 16, has the solution x=(3, 4,2)T. Using the initial guess x (0)=(1, 1,1)T Solve the above system as is using: Gauss-Seidel method. If the error increases, what does that mean and what should you do? (see b below) Condition the system so that convergence is secured and solve using the Gauss-Siedel method. Q2: Find a system...
matlab 1. Given the system of equations 9 + x2 +x3 +x4 = 75 xi +8x2 x3x54 X1+X1 +7X3 + X4 = 43 xi+x2 +x6x434 Write a code to find the solution of linear equations using a) Gauss elimination method b) Gauss-Seidel iterative method c) Jacobi's iterative method d) Compare the number of iterations required for b) and c) to the exact solution Assume an initial guess of the solution as (X1, X2, X3, X4) = (0,0,0,0).
Question 11 In Exercises 9-12, show that the Gauss-Seidel method diverges for the given system using the initial approximation (x1, x2,...,x) = (0,0,...,0). 9. x– 2x2 = -1 2xy + x2 = 3 11. 2x, – 3x2 = -7 x1 + 3x2 – 10x3 = 9 3x + x3 = 13 10. - x + 4x, = 1 3xı – 2x2 = 2 12. x, + 3x, – x3 = 5 3x1 - x2 = 5 x2 + 2x3 =...
Use an algorithm that you would systematically follow to apply the technique and solve each set of systems of linear equations. For example, you may select the technique of finding the inverse of the coefficient matrix A, and then applying Theorem 1.6.2: x = A^-1 b. There are several ways that we have learned to find A^-1. Pick one of those ways to code or write as an algorithm. Or another example, you may select Cramer’s rule. Within Cramer’s rule,...
3 Linear systems 18. Solve the linear system of equations using the Naive Gauss elimination method x,+x: + x) = 1 +2x, +4x1 x 19. Solve the linear system of equations using the Gauss elimination method with partial pivoting 12x1 +10x2-7x3=15 6x, + 5x2 + 3x3 =14 24x,-x2 + 5x, = 28 20. Find the LU decomposition for the following system of linear equations 6x, +2x, +2, 2 21. Find an approximate solution for the following linear system of equations...
Q.2) Solve the following set of linear equation by Gauss-Siedel Iteration method with initial guesses of(X10-X20 = X30-1). Compute only for three iterations. 2X1-3X2 + X3=1 xi 2x2+3x3 10 4x1+ X2 2x3 12 Q.2) Solve the following set of linear equation by Gauss-Siedel Iteration method with initial guesses of(X10-X20 = X30-1). Compute only for three iterations. 2X1-3X2 + X3=1 xi 2x2+3x3 10 4x1+ X2 2x3 12
Problem 2 (50 points): Estimate x1 and x2 from the following system of equations using 4 iterations of the Gauss-Seidel method with α,-1 and an initial guess of x,-1 and X2-1. x2-3x, +1.9 0 x,+x-3.0 = 0 Problem 2 (50 points): Estimate x1 and x2 from the following system of equations using 4 iterations of the Gauss-Seidel method with α,-1 and an initial guess of x,-1 and X2-1. x2-3x, +1.9 0 x,+x-3.0 = 0
[4] Problem 4. Consider the following system [28' 12 3 x] after one iteration of Gauss-Seidel method using [x 13 Find the values of [x1 x]T-[0 0 0]" as the initial guess. X2 X2
need help on number 13 Exercises 11-16. Represent each linear system in marrix form. Solve by Gauss elimination when the system is consistent and cross-check by substituting your solution set back into all equations. Interpret the solution geometrically in terms planes in R3. of 2x1 +3x2 x3 = 1 4x1 7x2+ 3 3 11. 7x1 +10x2 4x3 = 4 3x1 +3x2+x3 =-4.5 12. x1+ x2+x3 = 0.5 2x-2x2 5.0 x+2x2 3x3 1 3x1+6x2 + x3 = 13 13. 4x1 +8x2...