Domanda

Here is my code,

import numpy as np
import math
import matplotlib.pyplot as plt

#Voltages 
V,I = np.genfromtxt('Photocurrent_graph_2.csv', dtype=float, delimiter=',', 
                    usecols=(0,2), skiprows=1, unpack=True)

z = np.polyfit(V,I,6)


def function(x):
    return (z[0])*x**6 (z[1])*x**5 + (z[2])*x**4 + (z[3])*x**3 + (z[4])*x**2 + (z[6])*x + z[7]

plt.plot(V, function(V))    
plt.plot(V,I,'ro')

If I comment out the line plt.plot(V, function(V)), python does not give me any errors. What have I done wrong?

EDIT:

I followed iCodez's suggestion, but am not provided with a new error messsage,

return (z[0])*x**6 + (z[1])*x**5 + (z[2])*x**4 + (z[3])*x**3 + (z[4])*x**2 + (z[6])*x + z[7]
IndexError: index 7 is out of bounds for axis 0 with size 7
È stato utile?

Soluzione

The problem is with this line:

return (z[0])*x**6 (z[1])*x**5 + (z[2])*x**4 + (z[3])*x**3 + (z[4])*x**2 + (z[6])*x + z[7]

You forgot a + between (z[0])*x**6 and (z[1])*x**5.

Here is the fixed version:

return (z[0])*x**6 + (z[1])*x**5 + (z[2])*x**4 + (z[3])*x**3 + (z[4])*x**2 + (z[6])*x + z[7]

Without the +, you try to call (z[0])*x**6, which is an integer, by placing (z[1]) directly after it.

Altri suggerimenti

You missed + sign after the first expression. You use too many brackets - they do not add to clarity of the code, and just make it unreadable And there's a better way to write polynom calculation:

sum(c*x**pos for pos, c in enumerate(reversed(z)))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top