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

8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

CLASS 8

SOLVING SYSTEMS OF EQUATIONS

Q Find all solutions for linear system

𝑥 1 + 2𝑥 2 − 𝑥 3 = 1
2𝑥 1 + 𝑥 2 + 4𝑥 3 = 2
3𝑥 1 + 3𝑥 2 + 4𝑥 3 = 1
In [1]:

import numpy as np

In [2]:

A=np.array([[1,2,-1],[2,1,4],[3,3,4]])
B=np.array([1,2,1])
np.linalg.solve(A,B)

Out[2]:

array([ 7., -4., -2.])

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 1/7


8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

In [6]:

A=np.matrix([[1,2,-1],[2,1,4],[3,3,4]])
B=np.matrix([1,2,1])
np.linalg.solve(A,B)

--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
Input In [6], in <cell line: 3>()
1 A=np.matrix([[1,2,-1],[2,1,4],[3,3,4]])
2 B=np.matrix([1,2,1])
----> 3 np.linalg.solve(A,B)

File <__array_function__ internals>:5, in solve(*args, **kwargs)

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:393, in solve(a, b)


391 signature = 'DD->D' if isComplexType(t) else 'dd->d'
392 extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 393 r = gufunc(a, b, signature=signature, extobj=extobj)
395 return wrap(r.astype(result_t, copy=False))

ValueError: solve: Input operand 1 has a mismatch in its core dimension 0,


with gufunc signature (m,m),(m,n)->(m,n) (size 1 is different from 3)

In [5]:

A=np.matrix([[1,2,-1],[2,1,4],[3,3,4]])
B=np.matrix([[1],[2],[1]])
np.linalg.solve(A,B)

Out[5]:

matrix([[ 7.],
[-4.],
[-2.]])

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 2/7


8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

In [8]:

A=np.matrix([[1,-1,1,-1],[1,-1,1,1],[4,-4,4,0],[-2,2,-2,1]])
B=np.matrix([[2],[0],[4],[-3]])
np.linalg.solve(A,B)

--------------------------------------------------------------------------
-
LinAlgError Traceback (most recent call las
t)
Input In [8], in <cell line: 3>()
1 A=np.matrix([[1,-1,1,-1],[1,-1,1,1],[4,-4,4,0],[-2,2,-2,1]])
2 B=np.matrix([[2],[0],[4],[-3]])
----> 3 np.linalg.solve(A,B)

File <__array_function__ internals>:5, in solve(*args, **kwargs)

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:393, in solve(a, b)


391 signature = 'DD->D' if isComplexType(t) else 'dd->d'
392 extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 393 r = gufunc(a, b, signature=signature, extobj=extobj)
395 return wrap(r.astype(result_t, copy=False))

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:88, in _raise_linal


gerror_singular(err, flag)
87 def _raise_linalgerror_singular(err, flag):
---> 88 raise LinAlgError("Singular matrix")

LinAlgError: Singular matrix

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 3/7


8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

Solve system of equation

2𝑥 1 + 3𝑥 2 + 𝑥 3 = 0
𝑥 1 − 𝑥 2 − 3𝑥 3 = 0
In [10]:

A=np.matrix([[2,3,1],[1,-1,-2]])
B=np.matrix([[0],[0]])
np.linalg.solve(A,B)

--------------------------------------------------------------------------
-
LinAlgError Traceback (most recent call las
t)
Input In [10], in <cell line: 3>()
1 A=np.matrix([[2,3,1],[1,-1,-2]])
2 B=np.matrix([[0],[0]])
----> 3 np.linalg.solve(A,B)

File <__array_function__ internals>:5, in solve(*args, **kwargs)

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:380, in solve(a, b)


378 a, _ = _makearray(a)
379 _assert_stacked_2d(a)
--> 380 _assert_stacked_square(a)
381 b, wrap = _makearray(b)
382 t, result_t = _commonType(a, b)

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:203, in _assert_sta


cked_square(*arrays)
201 m, n = a.shape[-2:]
202 if m != n:
--> 203 raise LinAlgError('Last 2 dimensions of the array must be squa
re')

LinAlgError: Last 2 dimensions of the array must be square

Q.2 Solve following system of linear equations

1)
𝑥1 + 2𝑥2 − 𝑥3 = −1
2𝑥1 + 𝑥2 + 4𝑥3 = 2
3𝑥1 + 3𝑥2 + 4𝑥3 = 1

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 4/7


8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

In [12]:

A=np.matrix([[1,2,-1],[2,1,4],[3,3,4]])
B=np.matrix([[-1],[2],[1]])
np.linalg.solve(A,B)

Out[12]:

matrix([[ 1.66666667e+00],
[-1.33333333e+00],
[-3.70074342e-16]])

2)
𝑥1 − 𝑥2 + 𝑥3 − 𝑥4 = 2
𝑥1 − 𝑥2 + 𝑥3 + 𝑥4 = 0
4𝑥1 − 4𝑥2 + 4𝑥3 = 4
−2𝑥1 + 2𝑥2 − 2𝑥3 + 𝑥4 = −3

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 5/7


8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

In [13]:

A=np.matrix([[1,-1,1,-1],[1,-1,1,1],[4,-4,4],[-2,2,-2,1]])
B=np.matrix([[2],[0],[4],[-3]])
np.linalg.solve(A,B)

C:\Users\HP\Pranav\lib\site-packages\numpy\matrixlib\defmatrix.py:145: Vis
ibleDeprecationWarning: Creating an ndarray from ragged nested sequences
(which is a list-or-tuple of lists-or-tuples-or ndarrays with different le
ngths or shapes) is deprecated. If you meant to do this, you must specify
'dtype=object' when creating the ndarray.
arr = N.array(data, dtype=dtype, copy=copy)

--------------------------------------------------------------------------
-
LinAlgError Traceback (most recent call las
t)
Input In [13], in <cell line: 3>()
1 A=np.matrix([[1,-1,1,-1],[1,-1,1,1],[4,-4,4],[-2,2,-2,1]])
2 B=np.matrix([[2],[0],[4],[-3]])
----> 3 np.linalg.solve(A,B)

File <__array_function__ internals>:5, in solve(*args, **kwargs)

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:380, in solve(a, b)


378 a, _ = _makearray(a)
379 _assert_stacked_2d(a)
--> 380 _assert_stacked_square(a)
381 b, wrap = _makearray(b)
382 t, result_t = _commonType(a, b)

File ~\Pranav\lib\site-packages\numpy\linalg\linalg.py:203, in _assert_sta


cked_square(*arrays)
201 m, n = a.shape[-2:]
202 if m != n:
--> 203 raise LinAlgError('Last 2 dimensions of the array must be squa
re')

LinAlgError: Last 2 dimensions of the array must be square

3)
𝑥1 + 2𝑥2 − 𝑥3 = 1
2𝑥1 + 𝑥2 + 5𝑥3 = 2
3𝑥1 + 3𝑥2 + 4𝑥3 = 1
In [14]:

A=np.matrix([[1,2,-1],[2,1,5],[3,3,4]])
B=np.matrix([[1],[2],[1]])
np.linalg.solve(A,B)

Out[14]:

matrix([[-1.04293886e+16],
[ 6.63688366e+15],
[ 2.84437871e+15]])

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 6/7


8/22/23, 3:55 PM CLASS 8 AUG 22 SEM 3 - Jupyter Notebook

4)
𝑥1 + 2𝑥2 + 𝑥3 = 4
4𝑥1 + 5𝑥2 + 6𝑥3 = 7
7𝑥1 + 8𝑥2 + 9𝑥3 = 10
In [15]:

A=np.matrix([[1,2,1],[4,5,6],[7,8,9]])
B=np.matrix([[4],[7],[10]])
np.linalg.solve(A,B)

Out[15]:

matrix([[-2.00000000e+00],
[ 3.00000000e+00],
[-3.48927236e-16]])

In [ ]:

localhost:8888/notebooks/CLASS 8 AUG 22 SEM 3.ipynb 7/7

You might also like