Please solve it by MATLAB. Also for this problem, you don\'t have to do pivoting
ID: 2080057 • Letter: P
Question
Please solve it by MATLAB.Also for this problem, you don't have to do pivoting
This is problem 1 that mentioned in problem 3. 2. Problems solved with Matlab. Problem 3 Write a user-defined MATLAB function that solves a system of n linear equations with the Gauss-Jordan method. The program should include pivoting in which the pivot row is switched with the row that has a pivot element with the largest absolute numerical value. For the function name and arguments use x Gauss Jordan(a,b), where a is the matrix of coefficients, b is the right-hand-side column of constants, andxisthe solution. Use GaussJordan to solve problems 1.
Explanation / Answer
enter the following code in editor and save it as GaussJordan.m and the run the code
The code is as follows:
function x=GaussJordan(a,b)
a = [2 -2 1;3 2 -5; -1 2 3] % matrix a
b = [10 ; -16; 8] % matrix b
[n,m]=size(a); % finding the size of matrix a
Aa=[a,b];
for i=1:n
Aa(1:n,i:n+1)=GaussJordan_step(Aa(1:n,i:n+1),i);
end
x=Aa(:,n+1);
a=0;
function A1=GaussJordan_step(a,i) % calling of fuction function
[n,m]=size(a); % determination of size of matrix a
A1=a; % assigning a to A1
s=A1(i,1);
A1(i,:) = a(i,:)/s;
k=[[1:i-1],[i+1:n]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end % end of for loop
Once the code is executed the following result is displayed
>> GaussJordan
a =
2 -2 1
3 2 -5
-1 2 3
b =
10
-16
8
ans =
2
-1
4
For a different input change the value of matrix a and b in the file and save it and then run the program