简体   繁体   中英

x and y must have same first dimension, but have shapes (40,) and (1, 80) error

I'm getting an error of x and y must have same first dimensions, but have shapes (40,) and (1, 80), I've tried to look it up and was told to use numpy.array as seen below but it is still not working. I understand that its due to the "- Stot" part but not sure what to do exactly

import numpy
import matplotlib.pyplot as plt
import math
import scipy.special

kb = 1.380649*(10**-23)
Na = 20
Nb = 20
Ntot = int(Na + Nb)
qa = 0
qb = 41
Stot = 0
qaa = []
Saa = []
Sba = []
Stota = []
Snew = []
prob = 0
h = 1
pos = 0
suma = []
sumnew = []

#creates an array based on the value of qb starting from 0 to qb in increments of 1
for k in range (qa, qb-1):
    qa = qa + 1
    qb = qb - 1
    qaa += [qa]
    Sa = (qa + Na - 1) * (numpy.log(qa + Na - 1)) - (qa*numpy.log(qa)) - (Na*numpy.log(Na-1))
    Sb = (qb + Nb - 1) * (numpy.log(qb + Nb - 1)) - (qb*numpy.log(qb)) - (Nb*numpy.log(Nb-1))
    Stot = Sa + Sb + Stot
    Saa += [Sa]
    Sba += [Sb]
    
Prob = numpy.array([(Saa + Sba - Stot)])

plt.plot(qaa, Prob)
plt.title("Graph to show probability distribution of the macrostates, P(qa)")
plt.xlabel('qa')
plt.ylabel('Probability')

I can't be sure with rhis code. The message seems to come from the plot command, but the line having the expression "(Saa + Sba - Stot)" should trigger an error, since you Saa ans Sba are lists, and the result of their adittion is the concatenation of both lists. A list minus a float (Stot is a float) is not an allowed operation If you want to add the values of the list memberwise and then substract a value from each item, convert each list to an array before performing the substraction (an array minus a float is allowed)

Nevertheless, there seems to be an error in this entropy calculation. What is the list Stota used for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM