Question

Total(25 marks) 3. Given the system of equation as 3x + 7y - 2z=2 x - 5y + z = 13 2x + 3y - 102=-23 (a) Write a Matlab/C++ co
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 3:

  • Part (a)

Matlab code:

%code to solve system of liner equations
%given equations are: 3x + 7y -2z = 2
% x - 5y + z +13
% 2x + 3y - 10z = -23
% We know AX = B, so putting the values of A and B

A = [3 7 -2; 1 -5 1; 2 3 -10];
B = [2; 13; -23];
X = linsolve(A, B) %function to get the values of x, y and z

Screenshot:

MATLAB Drive > solve.mx + 1 2 %code to solve system of liner equations *given equations are: 3x + 7y -2z = 2 x - 5y + z +13 %

  • Part (b)

Matlab code:

1 5 - 6 - 9 - 12 - 13 - 14 - MATLAB Drive gauss_jordan.mx + Xcode to solve the system of equation using Gauss-Jordan method 2

MATLAB Drive gauss_jordan.mx + - 7 end 34 - A=; 35 function Al=gaussjordan_step (A,i) % calling of fuction function 39 - 49 -

Source code:

%code to solve the system of equation using Gauss-Jordan method
function [x,er]=gauss_jordan_elim(A,b)
A = [3 7 -2; 1 -5 1; 2 3 -10] % input for augmented matrix A
B = [2 ; 13; -23] % intput for matrix B
[n,m]=size(A); % finding the size of matrix A
er =0; % calculation of error
x=zeros(n,1); % calling fuction zero
if n ~= m
disp('error: n~=m'); % displaying error if found
er = 1;
end % end of the scope of if
if length(B) ~= n % finding the legth of matrix B
disp('error: wrong size of B'); % displaying error, if found
er = 2;
else
if size(B,2) ~= 1
B=B';
end % end of the scope of if-else
if size(B,2) ~= 1
disp('error: B is a matrix'); % displaying erron in matrix B
er = 3;
end
end
if er == 0
Aa=[A,B];
for i=1:n
[Aa(i:n,i:n+1),er]=gauss_pivot(Aa(i:n,i:n+1));
if er == 0
Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i);
end
end
x=Aa(:,n+1);
end
A=0;
function A1=gauss_jordan_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
function [A1,er]=gauss_pivot(A) % calling of fucntion
[n,m]=size(A); % finding the size of matrix A
A1=A; % process of assigning
er = 0; % error flag
if A1(1,1) == 0
check = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > n
disp('error: matrix is singular');
er = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end

To run the code, copy the source code in MatLab editor and save is as SomeFileName.m and run it. You will get the following output.

Output:

gauss_jordan.m COMMAND WINDOW 3 >> gauss_jordan A = 3 7 -2 1 -5 1 2 3 -10 B = 2 13 -23 ans = 5 -1 3

  • Part (c)

(I)

1 2 solve.mx + %code to solve system of liner equations given equations are: 3x + 7y -22 = 2 x - 5y + 2 +13 2x + 3y - lez = -

(ii)

MATLAB Drive > solve.mx + A = [3 7 -2; 1-5 1; 2 3 -10]; A %to find the determinant of matrix A Y = det(A) 4 7 %to find invers

*NOTE: Drop a comment for queries.

Add a comment
Know the answer?
Add Answer to:
Total(25 marks) 3. Given the system of equation as 3x + 7y - 2z=2 x -...
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