Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Exercise 8: Numerical solution of system of Equations

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:

• rank() Rankf of a matrix


• size() Size of a matrix
• if-then-else
• equationsToMatrix(); Convert linear equations to matrix form
• linsolve() Solve linear system of equations
• tic() Start stopwatch timer
• toc() Read elapsed time from stopwatch

8.0 Recap on system of equations


To solve:

x +2 y - z =1;

2x + y + 4z = 2;

3x + 3y + 4z = 1;

A=[1,2,-1;2,1,4;3,3,4]; % Matrix containing the LHS


b=[1;2;1]; % Column matrix containing the RHS
A\b % solve the simultaneous equation

ans = 3×1
7.0000
-4.0000
-2.0000

Try with

eqn1 = 2*x + y + z == 2;

eqn2 = -x + y - z == 3;

eqn3 = x + 2*y + 3*z == -10;

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

8.1.1 check if the following equation is homogeneous

A=[1,2,-1; 2,1,4; 3,3,4] ;


% A = [ 1,2,-1; 2,1,4; 1,-1,5] %try for this also
B=[0;0;0] ;
R = rank(A) % check the rank of the matrix

R = 3

[r c]=size(A) % Check number of rows and colums

r = 3
c = 3

if R==r % check the rank with number of rows


fprintf("It has trivial solutions\n")
else
fprintf(" system has %d non-trivial solutions\n",(r-R))
end

It has trivial solutions

check for the equation

8.2 Systems with non-homogeneous equations


The linear system of equations of the form AX = B is called system of non-homogenous linear equations if not
all elements in B are zeros.

The non homogeneous system of m equations AX = B in n unknowns is

• consistent (has a solution) if and only if, ρ(A) = ρ([A|B])


• has unique solution, ρ(A) = n
• has infintely many solutions, ρ(A) < n

2
• system is inconsistent ρ(A) not equal to ρ([A|B]).

8.2.1 Consider the equation

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 = rank(AB) % rank of the concatinated matrix [A|B]

RAB = 3

[r c] = size(A) %size (row/column) of matrix A

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

system has unique solution as:


ans = 3×1
7.0000
-4.0000
-2.0000

8.2.2 check for the set of equations:

x + 2y − z = 1

2x + y + 5z = 2

3x + 3y + 4z = 1

8.3 Using symbolic maths

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 = linsolve(A,B) ; % solve the equation


display(X)

X = 

Same porgram using the command solve()

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)

sol = struct with fields:


x: 7
y: -4
z: -2

% to access individual values of x,y,z we can use sol.x or sol.y or sol.z

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

Run the program again like the one shown

tic()

4
X = linsolve(A,B) ; % Ensure you have semicolon here!

toc()

Note down the time. Repeat it for A\B and slove also

8.4 Graphical representation of Solutions


We shall draw graph for both the equations and mark the intersection point

8.4.1 Obain the solution of

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

lines intersect at 2.000000 and -1.000000

Try for:

2x + y = 7;

3x − y = 3

narasimha.kaulgud@nie.ac.in

You might also like