Professional Documents
Culture Documents
Expt 8
Expt 8
Table of Contents
Functions used:........................................................................................................................................................ 1
8.0 Recap on system of equations........................................................................................................................... 1
8.1 Solution of system of equations:.........................................................................................................................2
8.2 Systems with non-homogeneous equations....................................................................................................... 2
8.3 Using symbolic maths.........................................................................................................................................3
8.4 Graphical representation of Solutions.................................................................................................................5
Functions used:
x +2 y - z =1;
2x + y + 4z = 2;
3x + 3y + 4z = 1;
ans = 3×1
7.0000
-4.0000
-2.0000
Try with
eqn1 = 2*x + y + z == 2;
eqn2 = -x + y - z == 3;
1
8.1 Solution of system of equations:
The homogeneous system of m equations AX = 0 in n unknowns has a non trivial solution if and only if the rank
of the matrix A is less than n.
If (rank of matrix ) ρ(A) = r < n, then the system possesses (n − r) linearly independent solutions
R = 3
r = 3
c = 3
2
• system is inconsistent ρ(A) not equal to ρ([A|B]).
clear; clc
A=[1 2 -1; 2,1,4; 3,3,4];
B=[1;2;1];
AB = [A,B] ; %concatinate both the matrices to get [A|B]
RA = rank(A) % Rank of the matrix
RA = 3
RAB = 3
r = 3
c = 3
if RA==RAB
if RA==r
fprintf("system has unique solution as:\n")
A\B % find the uniqe solution
else
fprintf("System has infinite solutions\n")
end
else
fprintf(" The system of equations is inconsistent ")
end
x + 2y − z = 1
2x + y + 5z = 2
3x + 3y + 4z = 1
3
8.3.1 Simultaneous equations can be solved using symbolic maths too
syms x y z
eqn1 = x + 2*y -z == 1 ; %define 3 equations
eqn2 = 2*x + y + 4*z == 2 ;
eqn3 = 3*x + 3*y +4*z == 1 ;
%convert the equations into matrix form
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z])
A =
B =
X =
syms x y z
eqn1 = x + 2*y -z == 1 ; % defiine threee equations
eqn2 = 2*x + y + 4*z == 2 ;
eqn3 = 3*x + 3*y +4*z == 1 ;
sol = solve([eqn1, eqn2, eqn3], [x, y, z]);
display(sol)
We have seen 3 methods to solve the simultaneous equation. Let us look at the time taken by each of them.
We shall use tic() and toc() to measure the time. While using tic-toc, disable the screen output. Else the
system will count the time to display the values on the screen too, therby giving incorrect time imformation
tic()
4
X = linsolve(A,B) ; % Ensure you have semicolon here!
toc()
Note down the time. Repeat it for A\B and slove also
3x + 5y = 1; x + y = 1
clear; clf
syms x y
eqn1 = 3* x + 5 * y == 1;
eqn2 = x + y == 1; % define two equations
sol = solve ( [eqn1,eqn2] ,[x , y ] ); % solve the equations
p=sol.x;
q=sol.y; % get the x and y values at intersection
x=-5:0.01:5 ;
y1 = ( 1 - 3 * x ) / 5; % eqaution to plot first line
y2 = 1 - x; % the second lin
plot (x , y1 ,x , y2 ); hold on % plot both the lines on the same graph
plot(p,q,"k*"); grid on % Mark the intersection point as * in black
title ( " $3x + 5y = 1 ; x + y = 1$ " )
legend (' $3x + 5y = 1$ ' , ' $x + y = 1$ ')
5
fprintf("lines intersect at %f and %f \n",p,q); % print the intersection values
Try for:
2x + y = 7;
3x − y = 3
narasimha.kaulgud@nie.ac.in