Physics with Mathematica

Eight Class

Are you all making progress on your homework problems??

Multi-column data files

Remove["Global`*"]

SetDirectory[NotebookDirectory[]];
data=Import["Class08_Data.dat"];

data

Length[data]

Length[data[[1]]]

data[[All,1]]

data[[All,2]]

dataPlot=Transpose[{data[[All,1]],data[[All,2]]}];
ListPlot[dataPlot]

Fitting data to a model

There are a number of different approaches, but we’ll stick to one simple example

Remove["Global`*"]

First, generate some “data”

xVals={-3,-2.5,-1.5,-0.3, 0.1,0.8,1.5,2.8};
yVals=Cosh[xVals];
data=Transpose[{xVals,yVals}];
ListPlot[data]

It looks like a parabola, although it isn’t, but we can try to fit it to one

f=a x^2+b x+c;
abc=FindFit[data,f,{a,b,c},x]

Does this look good? Try listing the difference between the data and the fit

func=f/.abc;
yFit=func/.x->xVals;
devs=yVals-yFit

OK, I guess. A nicer way to compare is to plot the fit along with the data

Combining data plots with fit results

This is simple. You already know how to do it.

xRange={-3.5,3.5};
yRange={0,12};
Show[
ListPlot[data,PlotRange->{xRange,yRange},
PlotStyle->Black,PlotMarkers->{Automatic,Medium}],
Plot[func,{x,-3,3},PlotRange->{xRange,yRange},
PlotStyle->Red]]