### Thread: Basic Programming Help Needed!!

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

Join Date
Aug 2013
Posts
2
Rep Power
0

#### 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. #### 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.

http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

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

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.
Code:
```#TIME,.CURRENT
#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.
Code:
```import sys     # supplies stderr
import pprint  # makes the result look good

filename = 'data1.txt'

with open(filename) as inf:  # step 1, open the file

# The file context of the with statement closed the file.

# parse the file content.  Separate it into lines, a list.
lines = characters.split('\n')

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()
# the floating point conversion is most likely to fail.
ms.append(float(fields[0]))
current.append(float(fields[1]))
except (ValueError, IndexError,):
sys.stdout.write(
'Bad file at the {}th line of data!  Fix {} and try again.'.
format(i, filename))
raise

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
Code:
```import sys     # supplies stderr
import pprint  # makes the result look good