6

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@IhsanAhmedK If this works then accept the answer. Thanks
-1

solve with numpy.linalg package

import numpy as np
from numpy.linalg import solve

A=np.array([[1,1,3],[2,3,-4],[3,2,7]])
# b=np.array([[6],[6],[0]])
b=np.array([[6],[6],[0]]).T[0]

x= solve(A,b)
print (x.flatten())
print('\n'.join(["x" + str(i[0]) + " = "+str(format(i[1], '.2f')) + ", "  for i in enumerate(x.flatten())]))      # tuple print
print(np.allclose(np.dot(A, x), b))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.