Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy not solving matrix equation correctly

Tags:

python

numpy

In the code below, Is result not supposed to be the same as eqtn_ans[1] since numpy had no problem solving the simultaneous equation? Or I'm I missing something?

Since numpy was able to get the inverse of eqtn_coeffs doesn't that make the equation solvable?

import numpy as np

def solve_simultaneous_equation(eq_coef, ans):
        A = np.array(eq_coef)
        B = np.array(ans)
        E = np.linalg.solve(A,B)
        return E

eqtn_coeffs = [[0.13230431079864502, -0.4504314661026001, 0.6201357841491699], 
               [-0.04826474189758301, -0.4006437063217163, 1.5354962348937988], 
               [-0.22883379459381104, -0.3508559465408325, 2.4508566856384277]]

eqtn_ans = [0.0, 0.7853981852531433, 1.3258177042007446]

params = solve_simultaneous_equation(eqtn_coeffs, eqtn_ans)

result = np.dot(eqtn_coeffs[1],params)

print(result) # The result of this should be the same as eqtn_ans[1] 
              # but its not even though numpy was able to solve the equation.

result:

0.625
like image 924
Emmanuel Avatar asked Apr 08 '26 15:04

Emmanuel


1 Answers

It is easy to see why you have a problem

np.linalg.det(eqtn_coeffs)

prints

3.3194268620340867e-17

If the determinant of a matrix is zero, then the linear system of equations it represents has no solution (to be mor precise, no desired unique solution). In other words, the system of equations contains at least two equations that are not linearly independent.

like image 92
Severin Pappadeux Avatar answered Apr 10 '26 04:04

Severin Pappadeux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!