toni
toni

Reputation: 19

numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square

i am trying to solve the equations passed to a and b but I am having an error of numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square how can I solve this issue

import sympy as sp

import numpy as np
import matplotlib.pyplot as plt
l=np.arange(1,1.5,0.01)
d=np.arange(0.005,0.015,0.0002)
Eb1=3543.75
A1=1
A2=(np.pi)*(d/2)**2/2
A3=np.pi
F11=0
F21=2*(np.arctan(0.5/l)*(180/np.pi))/360
F12=(d*np.pi*F21)
F12p=(((4/l**2)+4)**0.5-2)*(l/2)
F13=F12p-F12
F14=1-F12-F13
F23=0.5
F24=1-F21-F23
F34=F14/np.pi
rho=0.7
k=0.04
pr=0.7
cp=1005
myu=2.4*10**-5
alpha=5.68*10**-5
beta=1/500
Ra=(rho*9.81*beta*(500-293)*l)/(myu*alpha)
Nu=0.68+(0.67*Ra**0.25)/(1+(0.429/pr)**(9/16))**(4/9)
h=Nu*k
J1=3543.75+0.42*h*(500-295)
a = np.array([[F12,F13,F14], [-1*(F12+A2*F23+A2*F24),A2*F23,A2*F24], [A2*F23,-1*(A3*F34+A2*F23+F13),A3*F34]])
b = np.array([(7/3+F12+F14+F13)*J1-Eb1,-F12*J1,-F13*J1])
x = np.linalg.solve(a, b)
J2=x[0]
J3=x[1]
J4=x[2]
Eb2=((1-0.8)/(0.8*A2))*((J2-J1)*A1*F12)+J2
qrad=(Eb2-J2)/((1-0.8)/(0.8*A2))
print(qrad)

Upvotes: 0

Views: 1170

Answers (1)

Paddy Harrison
Paddy Harrison

Reputation: 1992

If you do some shape reshuffling to get the solver to work:

x = np.linalg.solve(np.moveaxis(a, 2, 0), b.T)
x = x.T

Gives qrad as :

[0.04295253 0.04653147 0.05025864 0.05413453 0.0581596  0.0623343
 0.06665908 0.07113439 0.07576063 0.08053823 0.08546759 0.0905491
 0.09578314 0.10117008 0.10671028 0.11240411 0.11825189 0.12425396
 0.13041064 0.13672224 0.14318907 0.14981142 0.15658958 0.16352382
 0.1706144  0.17786158 0.18526562 0.19282674 0.20054518 0.20842117
 0.21645491 0.22464661 0.23299646 0.24150466 0.25017139 0.2589968
 0.26798108 0.27712437 0.28642682 0.29588858 0.30550977 0.31529051
 0.32523093 0.33533114 0.34559123 0.3560113  0.36659143 0.37733172
 0.38823223 0.39929302]

Upvotes: 1

Related Questions