-1

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
Mack
  • 665
  • 2
  • 11
  • 20

2 Answers2

2

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.

  • Oh, yes. See, I was trying to see whether or not I missed a *, neglecting to see if I missed a +. I fixed that, but now I am getting this message: IndexError: index 7 is out of bounds for axis 0 with size 7 – Mack Nov 27 '13 at 15:53
  • Is the error due to z[7]? I figured that that would be the y-intercept. – Mack Nov 27 '13 at 16:00
  • Yes, you are correct. I actually did this very thing. Thank you for the help. – Mack Nov 27 '13 at 16:11
0

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)))
volcano
  • 3,578
  • 21
  • 28