Solving linear equations using matrices in Python

Viewed 901

I wrote a code to solve a set of 3 linear equations containing 3 variables.

The equations are : x+y+3z = 6; 2x+3y-4z=6;3x+2y+7z=0

The code I wrote is:

import numpy as np
A=np.matrix([1,1,3],[2,3,-4],[3,2,7])
B=np.matrix([6],[6],[0])
Ainverse=np.linalg.inv(A)
X=Ainverse*B
print (X)

But this is showing the error: TypeError: Field elements must be 2- or 3-tuples, got '2'

I dont seem to understand what it is. Please help

1 Answers

You have missed [] bracket in matrix(...):

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

Please refer this.

Related