Mathematica tool box 3

This is a short hands..on  introduction to Mathematica, part 3.

How to get data from files, put your file path on instead!!!!!!!

This is the beginning of the file  "mcd3c.txt"

SetDirectory["c:\PC07f"]



hcl=Import["hcl.txt", "Table"]

hcl2=ReadList["c:\\PC07f\\hcl.txt",Number,RecordLists->True];

ListPlot[hcl2,PlotJoined->True,FrameTicks->Automatic,PlotRange->All,AxesOrigin->{0,0},Frame->True,FrameLabel->{Energy,δα}]

SINGLE OBSERVABLE: see  average value, variance , standard deviation

MODELS with MULTI OBSERVABLE EXPRESSION: see propagation of error

MODELS TO BE:    LINEAR REGRESSION

We want to get a model with

<<LinearRegression`;

<<Statistics`LinearRegression`

we have x and y values, may  include repeated observations

data = {{0.055, 90}, {0.091, 97}, {0.138, 107},
 {0.167, 124}, {0.182, 142}, {0.211, 150},
 {0.232, 172}, {0.248, 189}, {0.284, 209},
 {0.351, 253}};

dplot=ListPlot[data]

func = Fit[data, {1, x^2}, x]

Or you may try a longer model, higher order polynom

func=Fit[data,{1,x,x^2,x^3,x^4},x]

Or you may want to try a different model (function)

func = Fit[data, {1, Exp[-x]+x^2}, x]

FindFit[data,{1,Exp[-x]+x^2},x]

When analyzing results you also want to have uncertainties (errors), standard error (standard deviation of mean), t-statistics, - we start with the first model and its two parameters

(regress=Regress[data,{1,x^2},x];
Chop[regress,10^(-6)])

also, you want predicted values, standard error for values and confidence intervals for predicted values

regress = Regress[data, {1, x^2}, x,
RegressionReport ->
 {FitResiduals, SinglePredictionCITable,
 ParameterConfidenceRegion}]

Similar approach is used for the second model

(regress=Regress[data,{1,x,x^2,x^3,x^4},x];
Chop[regress,10^(-6)])

regress = Regress[data, {1,x,x^2,x^3,x^4}, x,
RegressionReport ->
 {FitResiduals, SinglePredictionCITable,
 ParameterConfidenceRegion}]

YOU CAN NOW SHOW ALL OF THEM< input  , output and confidence intervals on one graph

errors = FitResiduals /. regress

{observed, predicted, se, ci} =
  Transpose[(SinglePredictionCITable /. regress)[[1]]];

(xval=Map[First,data];
predicted=Transpose[{xval,predicted}];
lowerCI=Transpose[{xval,Map[First,ci]}];
upperCI=Transpose[{xval,Map[Last,ci]}]);

<<Graphics`MultipleListPlot`

MultipleListPlot[
 data, predicted, lowerCI, upperCI,
 SymbolShape -> {PlotSymbol[Diamond], None, None,
 None},
 PlotJoined -> {False, True, True, True},
 PlotStyle -> {Automatic, Automatic,
 Dashing[{.05, .05}], Dashing[{.05, .05}]}
]

For a really NONLINEAR MODEL, what if you have more than one variable

Clear[data]

data

<<NonlinearRegression`;

<<Statistics`NonlinearFit`

now we have x1 and x2 values and the y values

data={{1.0,1.0,.126},{2.0,1.0,.219},{1.0,2.0,.076},{2.0,2.0,.126},{.1,.0,.186}};

NonlinearRegress[data,
theta1 theta3 x1 / (1 + theta1 x1 + theta2 x2),
{x1, x2}, {theta1, theta2, theta3}]

NonlinearFit[data,
theta1 theta3 x1 / (1 + theta1 x1 + theta2 x2),
{x1, x2}, {theta1, theta2, theta3}]

another model

NonlinearRegress[data,
x1 / (phi0 + phi1 x1 + phi2 x2),
{x1, x2}, {phi0, phi1, phi2},
RegressionReport -> {BestFit,
 AsymptoticCorrelationMatrix, FitCurvatureTable}]