0% found this document useful (0 votes)
106 views7 pages

Linsolve for Linear Equations in MATLAB

The document describes an algorithm for numerically solving systems of linear equations in MATLAB. It provides 4 examples of using the algorithm to test for consistency and solve systems. The algorithm involves: 1) Declaring variables and reading in equations 2) Forming the coefficients matrix and augmented matrix 3) Calculating the rank of the matrices 4) Solving the equations if consistent or displaying that it is inconsistent 5) Plotting the solutions It demonstrates both consistent and inconsistent systems.

Uploaded by

Saipreet Santosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views7 pages

Linsolve for Linear Equations in MATLAB

The document describes an algorithm for numerically solving systems of linear equations in MATLAB. It provides 4 examples of using the algorithm to test for consistency and solve systems. The algorithm involves: 1) Declaring variables and reading in equations 2) Forming the coefficients matrix and augmented matrix 3) Calculating the rank of the matrices 4) Solving the equations if consistent or displaying that it is inconsistent 5) Plotting the solutions It demonstrates both consistent and inconsistent systems.

Uploaded by

Saipreet Santosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Matrix Laboratory Manual

LAB 08) Numerical solution of system of linear equations, test for


consistency and graphical representation

Example 1: Test for consistency and solve for


X+ Y+Z =6
X – Y + 2Z = 5
3X + Y + Z = 8

Algorithm
Step 1: Start
Step 2: Declare the variables (syms x y z )
Step 3: Read eqn1, 2, 3
Step 4: Formulate the coefficients of system of equations in matrix
form (using the "equationToMatrix" function)
Step 5: Calculate rank of matrix A and augmented matrix A,B
Step 6: if rankA == rank([A,B])
disp(‘the system is consistent’)
else
disp(‘the system is inconsistent’)
Step 7: solve for the system of linear equations represented by Ax=B.
using linsolve (A,B)
Step 8: Plot a 3D graph using function plot3
Step 9: Set the limits of the X,Y and Z axes
Step10: Label X,Y and Z axes

Department of Mathematics KLE Institute of Technology


Matrix Laboratory Manual

Code:

syms x y z
eqn1 = x+y+z == 6
eqn2 = x-y+2*z == 5
eqn3 = 3*x+y+z == 8
[A,B] = equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z])
RankA = rank(A)
RankAB = rank([A,B])
if rank(A) == rank([A,B])
disp('The system is consistent')

X = linsolve(A,B)
figure
plot3([A(1),B(1)], [A(2),B(2)], [A(3),B(3)],'r','LineWidth', 2)
hold on
grid on
xlim([-15,15])
ylim([-15,15])
zlim([-15,15])
xlabel("X-Axes"); ylabel("Y-Axes"); zlabel("Z-Axes")
title('Numerical solution of system of linear equations')
else
disp('The system is inconsistent')
end

Output

eqn1 =
eqn2 =
eqn3 =

A=

B=

RankA = 3
RankAB = 3
The system is consistent
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual

X=

Example 2 : Test for consistency and solve for

4x-5y+z = -3
2x+3y-z = 3
3x-y+2z = 5

syms x y z
eqn1 = 4*x-5*y+z == -3
eqn2 = 2*x+3*y-z == 3
eqn3 = 3*x-y+2*z == 5
[A,B] = equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z])
RankA = rank(A)
RankAB = rank([A,B])
if rank(A) == rank([A,B])
disp('The system is consistent')
X = linsolve(A,B)
plot3([A(1),B(1)], [A(2),B(2)], [A(3),B(3)],'r','LineWidth', 2)
hold on
grid on
xlim([-15,15])
ylim([-15,15])

Department of Mathematics KLE Institute of Technology


Matrix Laboratory Manual

zlim([-15,15])
xlabel("X-Axes"); ylabel("Y-Axes"); zlabel("Z-Axes")
title('Numerical solution of system of linear equations')
else
disp('The system is inconsistent')
end

Output
eqn1 =
eqn2 =
eqn3 =

A=

B=

RankA = 3
RankAB = 3
The system is consistent

X=

Department of Mathematics KLE Institute of Technology


Matrix Laboratory Manual

Example 3:- Test for consistency and solve for


x-4y+7z = 14
3x+8y-2z = 13
7x+-8y+26z = 5
syms x y z
eqn1 = x-4*y+7*z == 14
eqn2 = 3*x+8*y-2*z == 13
eqn3 = 7*x+-8*y+26*z == 5
[A,B] = equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z])
RankA = rank(A)
RankAB = rank([A,B])
if rank(A) == rank([A,B])
disp('The system is consistent')
X = linsolve(A,B)
plot3([A(1),B(1)], [A(2),B(2)], [A(3),B(3)],'r','LineWidth', 2)
hold on
grid on
xlim([-15,15])
ylim([-15,15])
zlim([-15,15])
xlabel("X-Axes"); ylabel("Y-Axes"); zlabel("Z-Axes")
title('Numerical solution of system of linear equations')
else
disp('The system is inconsistent')
end

eqn1 =
eqn2 =
eqn3 =

A=

B=

RankA = 2
RankAB = 3
The system is inconsistent

Department of Mathematics KLE Institute of Technology


Matrix Laboratory Manual

Example 4:- Test for consistency and solve for


4x-2y+6z == 8
x+y-3z == -1
15x-3y+9z == 21

Code:

syms x y z
eqn1 = 4*x-2*y+6*z == 8
eqn2 = x+y-3*z == -1
eqn3 = 15*x-3*y+9*z == 21
[A,B] = equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z])
RankA = rank(A)
RankAB = rank([A,B])
if rank(A) == rank([A,B])
disp('The system is consistent')
else
disp('The system is inconsistent')
end
X = linsolve(A,B)
figure
plot3([A(1),B(1)], [A(2),B(2)], [A(3),B(3)],'r','LineWidth', 2)
hold on
grid on
xlim([-10,10])
ylim([-10,10])
zlim([-10,10])
xlabel("X-Axes"); ylabel("Y-Axes"); zlabel("Z-Axes")
title('Numerical solution of system of linear equations')
Output:

eqn1 =
eqn2 =
eqn3 =

A=

B=

RankA = 2
RankAB = 2

The system is consistent

Department of Mathematics KLE Institute of Technology


Matrix Laboratory Manual

Warning: Solution is not unique because the system is rank-


deficient.

X=

Department of Mathematics KLE Institute of Technology

You might also like