Interpolation and Root Finding

1.6k views Asked by At

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.

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.

    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"

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.

   #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 
  --->  12 r = brentq(d)
        13 print r

  TypeError: brentq() takes at least 3 arguments (1 given) 

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.

0

There are 0 answers