title: Interpolation and Root Finding
body: <p>For an assignment I had to interpolate data [linear interpolation and cubic interpolation] and create two graphs that showed the data as points and the interpolation as a line. I also had to plot the max value of the data. I got all of this to work, but was assigned a last part for extra credit and could not figure it out. V represents the voltage and I represents the current.</p>
  
  <p>Extra assignment: Expand your program so that it also determines the maximum current, and  the corresponding voltage, using a different method known as “root finding”. To accomplish  this, you will need to numerically differentiate the function. (The  numpy function  named  “diff”  will  be  useful.) Then to carry out the root finding, you can use the “brentq”  function From the scipy.optimize library.</p>
  
  <pre><code>    from pylab import *
      from scipy.interpolate import interp1d
      from scipy.optimize import brentq
  
      #load text into two variables V[Voltage] and I[Current]
      V, I = loadtxt('data1(1).txt', unpack = True, skiprows = 1)
  
      #voltage and current interpolated[linear]
      f_line = interp1d(V, I, 'linear')
      new_V = linspace(0, 12, 1000) #array of new voltages created
      new_I = f_line(new_V) #array of new currents created from interpolated data
  
      #voltage, current, new voltage, and new current plotted.
      plot(V, I, 'ro', new_V, new_I, 'b-')
      plot(max(new_I), 'go') #max current plotted 
      title("Linear Interpolation")
      xlabel("Voltage (V)")
      xlim(0,12)
      ylabel("Current (mA)")
      legend(['Data', 'linear Interp', 'Max Current'], loc = 'best')
      show()
  
      print "The maximum current is", max(new_I), "mA"
  
      #voltage and current interpolated[cubic]
      f_cube = interp1d(V, I, 'cubic')
      new_V = linspace(0, 12, 1000) #array of new voltages created
      new_I = f_cube(new_V) #array of new currents created from interpolated data
      index = argmax(new_I) #index of max current
  
      #voltage, current, new voltage, and new current plotted.
      plot(V, I, 'ro', new_V, new_I, 'b-')
      plot(new_V[index], max(new_I), 'go') #max current and voltage plotted
      title("Cubic Interpolation")
      xlabel("Voltage (V)")
      xlim(0,12)
      ylabel("Current (mA)")
      ylim(0,1.4)
      legend(['Data', 'linear Interp', 'Max Current'], loc = 'best')
      show()
  
      print "The maximum current is", max(new_I), "mA"
      print "The corresponding maxium voltage is", new_V[index], "V"
  </code></pre>
  
  <p>All the above code works, but it's the last part I'm unsure how to start on. I'd appreciate any suggestions or help. I did attempt it, but it threw an error and I was really unsure of how to handle it, as I don't know much about the functions (diff, brentq) being used, or how to use them to find the max current and voltage.</p>
  
  <pre><code>   #load text into two variables V[Voltage] and I[Current]
     V, I = loadtxt('data1(1).txt', unpack = True, skiprows = 1)
  
     f_line = interp1d(V, I, 'linear')
     new_V = linspace(0, 12, 1000) #array of new voltages created
     d = diff(new_V, 1)
  
     r = brentq(d)
     print r
  
  
          10 d = diff(new_V, 1)
          11 
    ---&gt;  12 r = brentq(d)
          13 print r
  
    TypeError: brentq() takes at least 3 arguments (1 given) 
  </code></pre>
  
  <p>I understand what the error is saying, but I don't really know know the correct arguments to give it in order to find the max current and voltage by "root finding" as was asked. Any suggestions are appreciated.</p>