100
4 Functions and the Writing of Code
2. Find a mathematical expression for the straight line that goes through the points
(i, y i ) and (i + 1, y i+1 ).
3. Compute the y value by inserting the user’s time value in the expression for the
straight line.
a) Implement the linear interpolation technique in a function interpolate that
takes as input an array with the y i measurements, the time between them Δt, and
some time t, for which the interpolated value is requested. The function should
return the interpolated y value at time t.
b) Write another function find_y that finds and prints an interpolated y value at
times requested by the user. Let find_y use a loop in which the user is asked
for a time on the interval [0, N]. The loop can terminate when the user gives a
negative time.
c) Use the following measurements: 4.4, 2.0, 11.0, 21.5, 7.5, corresponding to
times 0, 1, . . . , 4 (min), and compute interpolated values at t = 2.5 and t = 3.1
min. Perform separate hand calculations to check that the output from the
program is correct.
Filename: linear_interpolation.py.
Exercise 4.11: Test Straight Line Requirement
Assume the straight line function f (x) = 4x +1. Write a script that tests the “pointslope” form for this line as follows. Within a chosen interval on the x-axis (for
example, for x between 0 and 10), randomly pick 100 points on the line and check
if the following requirement is fulfilled for each point:
f (x i ) − f (c)
x i − c
= a,
i = 1, 2, . . . , 100 ,
where a is the slope of the line and c defines a fixed point (c, f (c)) on the line. Let
c = 2 here.
Filename: test_straight_line.py.
Exercise 4.12: Fit Straight Line to Data
Assume some measurements y i , i = 1, 2, . . . , 5 have been collected, once every
second. Your task is to write a program that fits a straight line to those data.
a) Make a function that, for given measurements and parameter values a and
b, computes the error between the straight line f (x) = ax + b and the
measurements:
e =
5
i=1
(ax i + b − y i )
2 .
b) Make a function that, in a loop, asks the user to give a and b for the line. The
corresponding value of e should then be computed and printed to screen, and a
plot of the straight line f (x) = ax + b, together with the discrete measurements,
should be produced.
4 Functions and the Writing of Code
2. Find a mathematical expression for the straight line that goes through the points
(i, y i ) and (i + 1, y i+1 ).
3. Compute the y value by inserting the user’s time value in the expression for the
straight line.
a) Implement the linear interpolation technique in a function interpolate that
takes as input an array with the y i measurements, the time between them Δt, and
some time t, for which the interpolated value is requested. The function should
return the interpolated y value at time t.
b) Write another function find_y that finds and prints an interpolated y value at
times requested by the user. Let find_y use a loop in which the user is asked
for a time on the interval [0, N]. The loop can terminate when the user gives a
negative time.
c) Use the following measurements: 4.4, 2.0, 11.0, 21.5, 7.5, corresponding to
times 0, 1, . . . , 4 (min), and compute interpolated values at t = 2.5 and t = 3.1
min. Perform separate hand calculations to check that the output from the
program is correct.
Filename: linear_interpolation.py.
Exercise 4.11: Test Straight Line Requirement
Assume the straight line function f (x) = 4x +1. Write a script that tests the “pointslope” form for this line as follows. Within a chosen interval on the x-axis (for
example, for x between 0 and 10), randomly pick 100 points on the line and check
if the following requirement is fulfilled for each point:
f (x i ) − f (c)
x i − c
= a,
i = 1, 2, . . . , 100 ,
where a is the slope of the line and c defines a fixed point (c, f (c)) on the line. Let
c = 2 here.
Filename: test_straight_line.py.
Exercise 4.12: Fit Straight Line to Data
Assume some measurements y i , i = 1, 2, . . . , 5 have been collected, once every
second. Your task is to write a program that fits a straight line to those data.
a) Make a function that, for given measurements and parameter values a and
b, computes the error between the straight line f (x) = ax + b and the
measurements:
e =
5
i=1
(ax i + b − y i )
2 .
b) Make a function that, in a loop, asks the user to give a and b for the line. The
corresponding value of e should then be computed and printed to screen, and a
plot of the straight line f (x) = ax + b, together with the discrete measurements,
should be produced.
