Homework: To catch an exponent

Theory

The given data represents a function \(y = f(x)\), and if you plot it, you will see that \(y\) grows non-linearly as a function of \(x\). There are infinitely many functions that could represent the data, but in physics, the most common such models are

  • power-law growth \(y = b x^a\)

  • exponential growth \(y = b e^{ax}\).

We shall guess that one of these is at least a good approximation.

Taking a logarithm of the power law equation yields

\[\ln y = \ln bx^a = \ln b + \ln x^a = \ln b + a \ln x.\]

That means that if the data obeys a power-law, plotting \(\ln y\) as a function of \(\ln x\) should produce a straight line with slope \(a\) and constant \(\ln b\). For the exponential law, taking a logarithm results in

\[\ln y = \ln be^{ax} = \ln b + \ln e^{ax} = \ln b + ax.\]

This is a straight line when \(\ln y\) is plotted as a function of \(x\).

Now, we can use these results to figure out, what kind of growth law best describes our data. We should calculate logarithmic transformations of our \(x\) and \(y\) data and plot \(\ln y\) against both \(x\) and \(\ln x\). If the data falls on a straight line, it must obey the corresponding growth law.

Task

To complete this assignment, you need to complete the following tasks, submit your code as a plain .py file, and answer the questions. In the computing tasks, you must implement the incomplete functions so that they operate exactly as described in the documentation.

  • Computing task:
    1. Implement functions to read and write data.

    2. Implement functions to perform transformations (such as taking the logarithm) on data arrays. You need to implement two versions: one that returns the manipulated data and another that directly operates on a given array.

    3. Read the data in A1_data.txt, perform logarithmic transformations on its x and y data, and generate linear, logarithmic and lin-log plots.

    4. Fit and plot a linear model to all cases, determine the best model and solve values for all fitting parameters.

  • Questions to answer:
    1. Is the model better represented by a power-law or an exponential growth model? Explain! Include your plots in your answer and justify your answer with them.

    2. What is your best estimate for the function \(y = f(x)\)? You must explicitly solve for \(y\) and clearly mark the numeric values of all the fitting parameters.

A1_analysis.py

A1_analysis.linear(x, a, b)[source]

Calculates the linear function \(y=ax+b\) and returns the result.

Parameters:
  • x (float) – the variable

  • a (float) – the slope

  • b (float) – the constant term

Returns:

the result

Return type:

float

A1_analysis.linear_fit(xdata, ydata, name='linear', printout=True)[source]

Fit a linear function \(y = ax+b\) to the given xy data. Also return the optimal fit values obtained for slope a and constant b.

If printout is True, the fit values will also be printed on screen. To identify this information, you may also name the operation.

Parameters:
  • xdata (array) – x values

  • ydata (array) – y values

  • name (str) – identifier for printing the result

  • printout (bool) – if True, results are printed on screen

Returns:

slope, constant

Return type:

float, float

A1_analysis.linlin_analysis(x_data, y_data, printout=True)[source]

Performs a linear fit for x versus y.

Finds the values for constants \(a\) and \(b\) that minimize the sum of squares error between the given data and the function

\[y = a x + b\]
Parameters:
  • x_data (array) – x values

  • y_data (array) – y values, must be the same size as x_data

  • printout (bool) – if True, results are printed on screen and a plot is saved

Returns:

slope a, constant b

Return type:

float, float

A1_analysis.linlog_analysis(x_data, y_data, printout=True)[source]

Performs a linear fit for x versus ln(y).

Finds the values for constants \(a\) and \(b\) that minimize the sum of squares error between the given data and the function

\[\ln(y) = a x + b\]

Note

This function is incomplete!

Parameters:
  • x_data (array) – x values

  • y_data (array) – y values, must be the same size as x_data

  • printout (bool) – if True, results are printed on screen and a plot is saved

Returns:

slope a, constant b

Return type:

float, float

A1_analysis.loglog_analysis(x_data, y_data, printout=True)[source]

Performs a linear fit for ln(x) versus ln(y).

Finds the values for constants \(a\) and \(b\) that minimize the sum of squares error between the given data and the function

\[\ln(y) = a \ln(x) + b\]

Note

This function is incomplete!

Parameters:
  • x_data (array) – x values

  • y_data (array) – y values, must be the same size as x_data

  • printout (bool) – if True, results are printed on screen and a plot is saved

Returns:

slope a, constant b

Return type:

float, float

A1_analysis.main(filename)[source]

Reads xy data from the given file and performs linear and logarithmic analysis.

Parameters:

filename (str) – name of the file to read

A1_analysis.parse_xy_data(filename)[source]

Read the given file, parse the xy-data and return the x and y coordinates as two numpy-arrays.

Assume the file has xy-data so that each row has the x and y coordinates of a single data point, separated by a comma and a space. That is, the file format is

x1, y1
x2, y2
...

Note

This function is incomplete!

Parameters:

filename (str) – name of the file to be read

Returns:

xdata, ydata

Return type:

array, array

A1_analysis.plot_line(xdata, slope, constant)[source]

Plot the linear function y = slope * x + constant from the first x value in xdata to the last.

The plot is created in memory. You still need to use show() or savefig() to actually see the plot.

Parameters:
  • xdata (array) – x values as a list or numpy array

  • slope (float) – slope a in a*x+b

  • constant (float) – constant b in a*x+b

A1_analysis.plot_xy_data(xdata, ydata, style='o')[source]

Plot the given xy data in the given style.

The plot is created in memory. You still need to use show() or savefig() to actually see the plot.

Parameters:
  • xdata (array) – x values as a list or numpy array

  • ydata (array) – y values as a list or numpy array

  • style (str) – matplotlib plotting style string

A1_analysis.save_plot(filename)[source]

Save all the plots currently in memory in a file and show them on screen. Afterwards, plots are cleared from memory.

Parameters:

filename (str) – name of the file to write

A1_analysis.transform_data(data, operation='ln')[source]

Transform the given data array by operating on it element-by-element with a given function.

Does not alter the original data vector. Instead, returns the transformed version as a new numpy array.

The function also returns True or False according to whether the given operation was one of the accepted types.

If an invalid operation type is given, returns a copy of the original data.

example: for [1.0,2.0] and ‘exp’, returns [2.718281828, 7.389056099], True

example: for [1.0,2.0] and ‘pow’, returns [1.0,2.0], False

Note

This function is incomplete!

Parameters:
  • data (array) – the data

  • operation (str) – transformation type, one of ‘ln’, ‘exp’, ‘sin’, ‘cos’

Returns:

the transformed data, operation success

Return type:

array, bool

A1_analysis.transform_data_in_place(data, operation='ln')[source]

Transform the given data array by operating on it element-by-element with a given function.

The new data is saved in place of the old data.

The function returns True or False according to whether the given operation was one of the accepted types.

If an invalid operation type is given, the original data is kept.

example: for [1.0,2.0] and ‘exp’, returns True

example: for [1.0,2.0] and ‘pow’, returns False

Note

This function is incomplete!

Parameters:
  • data (array) – the data

  • operation (str) – transformation type, one of ‘ln’, ‘exp’, ‘sin’, ‘cos’

Returns:

either True for a successful operation or False otherwise

Return type:

bool

A1_analysis.write_data(xdata, ydata, filename)[source]

Write data in a file.

The format is the same as in parse_xy_data(),

x1, y1
x2, y2
...

Note

This function is incomplete!

Parameters:
  • xdata (array) – x values as a list or numpy array

  • ydata (array) – y values as a list or numpy array

  • filename (str) – name of the file to write

A1_analysis.write_file(text, filename)[source]

Write the given text to a new file.

Parameters:
  • text (str) – text to be written

  • filename (str) – name of the file to write