I cant find the corret structure with this specific linear system code

Question:

I am trying to code an specific linear algebra code
Trying to get the x,y,z solutions

that’s the code

import numpy as np
# Exerciocio de fixação para solução de sistemas lineares

# Given
'''
1.5x -y = 0
3x+1-z=0
'''
# Define Ax=b
A=np.array([[1.5,-1,0,],[3,0,-1,]])
b=np.array([0,-1])

#Solve x,y,z
x,y,z= np.linalg.solve(A,b)
#Print x,y,z
print((x,y,z))

that’s the message error

Traceback (most recent call last):
  File "script.py", line 14, in <module>
    x,y,z= np.linalg.solve(A,b)
  File "<__array_function__ internals>", line 6, in solve
  File "/usr/local/lib/python3.6/dist-packages/numpy/linalg/linalg.py", line 390, in solve
    _assertNdSquareness(a)
  File "/usr/local/lib/python3.6/dist-packages/numpy/linalg/linalg.py", line 213, in _assertNdSquareness
    raise LinAlgError('Last 2 dimensions of the array must be square')
numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square
Asked By: h1k3rpath

||

Answers:

You have 3 variables so you will need 3 equations to solve it (if a solution exists). Let’s add 3x+y+z = 1 to your two equations.

a = np.array([[1.5, -1, 0], [3, 0, -1], [3,1,1]])
b = np.array([0, -1, 1])
x,y,z= np.linalg.solve(a,b)
print (x,y,z)

output:

0.0 -0.0 1.0

Lets check :

1.5*0-0 = 0
3*0+0-1*1 = -1
3*0+0+1 = 1
Answered By: mujjiga
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.