1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Rep Power

    Basic Programming Help Needed!!

    I am a new programmer, and am very unsure how to do my first assignment for a class I have to take. Here are the instructions.

    (1) Load the data from the file data1.txt.
    (2) Generate a function that will “interpolate” the data.
    (3) Produce a graph showing both the data (as points) and the interpolation
    function (as a line/curve).
    (4) Automatically determine the maximum value of current (which will probably be
    in between the measured values!) and the voltage at which this current would
    be measured.

    Using your program, analyze the data twice—once using linear interpolation (a
    linear spline) and once using a cubic spline. In your report, include a plot from each
    type of analysis. Arrange the plots side-by-side in your report, and include a caption
    beneath the plots.

    There is a data file included, but I can't even import that. Help needed very much!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Rep Power

    Not basic.

    This problem is in no way basic. You need calculus, linear algebra, and programming skill. Life is easier if you're allowed to use scipy.


    Using numpy to load data into an array I found
    http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html by searching the internet for
    scipy read data file.

    At any rate, there are a few steps you take to read data from a file. Open the file. Read some information. Parse it. Convert it to the correct data type.

    Let's say data1.txt has columns representing time and current. Time is milliseconds as a float. Current, in Amperes, also written as a decimal number. Sample data. Lines starting with # are commentary that we'll discard.
    #uS      Amps
    0.190086 0.891663
    0.199016 0.888594
    0.409382 0.716629
    0.605581   0.9962
    0.619391 0.477721
    0.654682 0.946355
    0.732158 0.925092
    0.925557  0.81822
    Here's a program to read and parse the data if we're not using one of the scipy data loaders.
    import sys     # supplies stderr
    import pprint  # makes the result look good
    filename = 'data1.txt'
    with open(filename) as inf:  # step 1, open the file
        characters = inf.read()     # step 2, read the data
    # The file context of the with statement closed the file.
    # parse the file content.  Separate it into lines, a list.
    lines = characters.split('\n')
    # parse the data.  Discard blank lines and lines with comments.
    data_lines = [line for line in lines if line and (not line.startswith('#'))]
    # parse each line separating by white space.
    # first we need a place to store the quantities.
    ms = []
    current = []
    for (i, line,) in enumerate(data_lines):
        fields = line.split()
        try: # some files contain bad information.  Do something about this!
            # the floating point conversion is most likely to fail.
        except (ValueError, IndexError,):
                'Bad file at the {}th line of data!  Fix {} and try again.'.
                format(i, filename))
    pprint.pprint(list(zip(ms, current)))
    This would be okay if the program were only to read the data. Reading input is just a tiny part of the whole. Next package the program as a function. Employing python syntax shorthand, and giving up the line number report in the bad data file error message, I come up with this load_data function
    import sys     # supplies stderr
    import pprint  # makes the result look good
    def load_data(filename):
            with open(filename) as inf:
                return list(list(map(float,line.split()))
                            for line in inf if line and not line.startswith('#'))
            sys.stderr.write('problem with input {}'.format(filename))
    (ms, current,) = zip(*load_data('data1.txt'))

    For the cubic splines, a "book" spline matches slope and value at the internal knots (2 slopes + 2 values = 4 degrees of freedom resolve the c0, c1, c2, and c3 polynomial coefficients.) What about the end points? There's no slope to match. Recipe for book spline is to set the second derivative to 0.
    Last edited by b49P23TIvg; August 26th, 2013 at 08:01 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo