Curve Fitting

Name:
Student #:

1. Climate Change

The data in the file global_tanomaly.dat represent the mean annual global atmospheric temperature anomaly from 1850 to 2015. The anomaly is the deviation from the mean. The file is available from the course web page in the "laboratory" section. The first column is time (year) and the second is the temperature anomaly (degree Celsius).

The data are obtained from
https://www.ncdc.noaa.gov/cag/time-series/global/globe/land_ocean/ytd/12/1880-2016

Import these data. You will probably need to change the working directory first. The command to do this is SetDirectory.

Remove["Global`*"]

?Directory

Directory[]

?SetDirectory

SetDirectory["~/Desktop"]  (or wherever you downloaded the file. The ~ is a short form for your home directory on Unix and MacOS opererating systems.)

?FileNames

FileNames[]
or
FileName[“gl*”] to get all files that begin with “gl”

?Import

Temp = Import[“global_tanomaly.dat”,“Table”];
(The second argument specifies that we are importing data in a table format.)

Plot the temperature anomaly as a function of time:

p1=ListPlot[Temp,PlotStyle->PointSize[0.015],AxesLabel->{"Years","Temperature anomaly ((()^o)C)"}]

Make a linear fit to these data using the Fit command.

Fit[Temp, {1, x}, x]
Fit1[x_] = Fit[Temp, {1, x}, x];

An other way of doing the fitting, one that allows you do find out more about the fit, is the command LinearModelFit.

?LinearModelFit

LM=LinearModelFit[Temp, {1, x}, x]

LM["BestFit"]

LM["BestFitParameters"]

The "residuals" are simply the difference between the data values and the fitted function at the same value of the independent variable(s).

ListPlot[LM["FitResiduals"]]

Note that calling FitResiduals yields a simple list of values (the original values of the independent variable are not stored here).

An estimate of the uncertainty in the best fit parameters is provided as well.

LM[“ParameterErrors”]
or
LM[“ParameterTable”]

What is the slope and the uncertainty in the slope of the line of best fit (include units; quote uncertainty to 1 sig. fig.)?

Answer:

The "coefficient of determination", or "correlation coefficient" or simply "R-Squared" value of the fit gives an esimtate of how well the fit describes the data.  The closer the value of this number is to 1, the better the fit.

LM["RSquared"]

Now make a second-order polynomial fit to the data:

Fit2[x_]=Fit[Temp,{BLANK},x];

LM2=LinearModelFit[Temp,{BLANK},x];

Let's check the "R-Squared value" to see if the fit has improved.

LM2["RSquared"]

Has the fit improved?

Plot the results of each fit onto a single figure and on top of the data. Use different colors to represent each fit.

p2 = Plot[{Fit1[x], Fit2[x]}, {x, Min[Temp[[All, 1]]], Max[Temp[[All, 1]]]}, PlotStyle -> {{Blue, Thickness[0.01]}, {Red, Thickness[0.01]}}]

Note the way we get the initial and final time values of our data with Min[Temp[[All,1]]] and Max[Temp[[All,1]]]

Show[p1, p2]

2. Solitons and Nonlinear Curve Fitting

A soliton is a localized pulselike wave of permanent form that exists in nonlinear systems. Solitons exists as water waves, as light pulses in fiber optics and have also been proposed as a model for elementary particles in Quantum Physics. The shape of a soliton propagating as a water wave is described by the following function:
	η(x)=a (sech^2)((x/λ))	(1)
where η is the surface displacement, a is the soliton amplitude and λ the soliton lengthscale. 
Examine the shape of a soliton of amplitude a=2 m and lengthscale  λ=10 m by executing the following commands
	Remove["Global`*"]
	a = 2; λ = 10; Plot[a ((Sech[x/λ])^2),{x,-50,50}]

The file soliton.dat, available from the course web page (laboratory section), provides measurements of the displacement of a water soliton as a function of distance. These are real measurements collected from a student for the senior laboratory course PHYS 3900. The measurements were collected from image analysis. 
Download this file from the course web page and import the data into Mathematica. The first column gives the distance in cm and the second column the vertical displacement of the water also in cm.

soliton = Import[“soliton.dat”,“Table”];

Now that the data have been imported, plot the measurements with the command,
	p1 = ListPlot[soliton,PlotStyle->PointSize[0.02], AxesLabel->{"x (cm)","Displacement (cm)"}]

We will now try to fit a nonlinear function of the form given by (1) to these measurements. For doing this we will use FindFit:
	?FindFit

In general, a nonlinear fit requires starting guesses for the parameters being found; different starting guesses may lead to different fitting parameters. We can specify the initial estimates for the fitting when we call FindFit.  In our example, the parameters are a, the amplitude, and λ, the lengthscale of the soliton.  Simply by inspecting the plotted data it seems that a = 2 cm would be a good initial guess for the amplitude, and λ = 5 cm a good guess for the lengthscale.  Before entering the FindFit command below, you will need to clear the variables λ and a, since we gave them definite values above (use Clear).

Clear[λ,a];

solfit=FindFit[soliton,a ((Sech[x/λ])^2), {{a,GUESSFORa},{λ,GUESSFORλ}},x]

Make the above function call without the initial parameter estimates.  In this particular example, you should see that providing an inital guess is not required.

We see that FindFit returns a replacement rule for the best fit parameters.  To plot the fitting function, type
	p2=Plot[a ((Sech[x/λ])^2) /.solfit, {x,-15,15},PlotStyle->{{Blue,Thickness[0.01]}}]

And now to plot it on top of the original data
	Show[p1,p2,PlotRange->{{-12,12},All}]