(*Graphics*)

(*In this section,we introduce  basic plotting commands and show how to use them.*)

Plot[x^2+2,{x,-2,2},AxesLabel->{"x","y"},PlotLabel->"Plot of x^2+2"]

{Plot[x^2+2,{x,-2,2},AxesLabel->{"x","y"},PlotLabel->"Plot of x^2+2",GridLines->None,PlotRange->{{-1,1},{0,4}}],Plot[x^2+2,{x,-2,2},AxesLabel->{"x","y"},GridLines->Automatic],Plot[x^2+2,{x,-2,2},AxesLabel->{"x","y"},GridLines->Automatic,
AspectRatio->1],Plot[x^2+2,{x,-2,2},AxesLabel->{"x","y"},GridLines->Automatic,Axes-> None,
Frame->True]}

(*Plotting a graph using ListPlot command for discrete data:

The plot command works on "vectors" (lists) of numerical data.For example,supposed that you have experimental data set listed separately for the X and Y components, such as Xexp={1,2,3,4,5} and the corresponding response,Yexp={12,21,34,46,55},you can type:*)

Xexp={1,2,3,4,5};Yexp={12,21,34,46,55};

ListPlot[Yexp](*since Xexp is a natural list*)

(*or, create a list of {X,Y}pairs, which is the outcome of most experiments*)

(*let's have a look*){Xexp,Yexp}//MatrixForm

data=Transpose[{Xexp,Yexp}]

MatrixForm[data]

ListPlot[data,AxesOrigin->{0,0}](*gives the same*)

(*Plotting Multiple Curves in a plot
If you want to plot two or more graphs in a single plot,you can list the functios as a list (if they are in the same domain) or use the "Show" command to combine separate plots. For example,if you want to plot sin(x) and cos(x) curves in a single plot:*)

Plot[{Sin[x],Cos[x]}, {x,0,5Pi},PlotLabel->"Sin[x]  and Cos[x]"]

(*or*)Show[Plot[Sin[x], {x,0,5Pi}],Plot[Cos[x], {x,0,5Pi}]](*gives the same; you can adjust color/style of curves*)

(*similarly, you can define fig1=Plot[Sin[x], {x,0,5Pi}], fig2=Plot[Cos[x], {x,0,5Pi}] and then use Show[fig1,fig2 - check!]*)

(*Exporting your graphics*)

myfig=Show[Plot[Sin[x], {x,0,5Pi}],Plot[Cos[x], {x,0,5Pi}]];

Export["mySinCosfig.JPEG",myfig,"JPEG"](*the file "...JPEG is created outside Mathwmatica"*)

(*The following example shows you how to show together a discrete and a continous plots.In this example,supposed that you conducted a simple harmonic motion experiment for spring system,we have experimental data set of mass (mexp) vs.period (Texp) as shown below.*)
mexp={0.06,0.08,0.1,0.12,0.16};
Texp={4.5,5.2,5.8,6.3,7.2};

(*we first create pairs of data as you expect from experiment*)

mydata=Transpose[{mexp,Texp}]

mydata//MatrixForm

(*The spring constant (k) is 0.11.The equation of simple harmonic motion is given by T=2Pi Sqrt[m/k].The  code will be as following*)

Clear[T,m,k];k=.11;T[m_]:=2Pi Sqrt[m/k]

Show[Plot[T[m],{m,0,0.16}],ListPlot[mydata],AxesLabel->{"m, kg","T, s"}, PlotLabel->"Period (T) vs. Mass (m)"]

(*Note: if possible, for a simple known or assumed theoretical dependence it is better to use variables with a linear dependence; let us repeat the previous example: *)

mexp={0.06,0.08,0.1,0.12,0.16};
Texp={4.5,5.2,5.8,6.3,7.2};

Texp2=Texp^2

mydata2=Transpose[{mexp,Texp2}]

Clear[T2,m,k];k=.11;T2[m_]:=(2Pi Sqrt[m/k])^2

T2[m]

figTheo=Plot[T2[m],{m,0,0.16}]; figExp=ListPlot[mydata2];

Show[figTheo,figExp,AxesLabel->{"m, kg","T^2, s^2"}, PlotLabel->"Square of Period (T^2) vs. Mass (m)"]

(*Linear fitting of data*)

Fexp={1.9,3.9,5.9,6.9,7.8};
Dexp={0.03,0.1,0.16,0.20,0.23};

data=Transpose[{Dexp,Fexp}]

data//MatrixForm

LinearModelFit[data,x,x]

nfit=Normal[%]

Show[Plot[nfit,{x,0,0.25}],ListPlot[data]]

({{}})

dev=Fexp-(nfit/.x->Dexp)

ListStepPlot[Transpose[{Dexp,dev}]]

list=Fexp-Mean[Fexp]*Table[1,{Length[Fexp]}]

devMean=Norm[list]^2

totDev=Norm[dev]^2

r2=1-totDev/devMean

(*good!*)

(*Graphics  of vectors and other objects*)

?Arrow

myarr[xx_]:=Arrow[{{0,0},xx}](*always from origin*)

Graphics[myarr[{1,1}]]

Clear[A,B,c];

A={1,2};B={-1,3};c=A-B;

Graphics[{myarr[A],myarr[B],myarr[c]}]

?Text

Graphics[{myarr[A],myarr[B],myarr[c],Text["A",A/2-{0,.1}],Text["B",B/2-{0,.1}],Text["C",c/2-{0,.1}]}]

Show[%,GridLines->Automatic]

(*Plotting vector fields*)

(*we consider the simplest graphical example of the gravitational field around a planet: g = -G*M /r^2, with G - gravitational constant and M - the mass of the planet. For simplicity, assue G*M=1, and we use the vector "field" = - (OverVector[r]) /(r^3) . 
The same structure of field is observed for a negative 
electric charge*)

Clear[x,y];vecr={x,y}; r=Norm[vecr];

field=-vecr/r^3

FullSimplify[%,Assumptions->x>0 && y>0]

field=%

VectorPlot[field, {x,-1,1}, {y,-1,1}]

Show[%,Graphics[Disk[{0,0},.1]]]

?ListStreamPlot

StreamPlot[field, {x,-1,1},{y,-1,1}]

Show[%,Graphics[Disk[{0,0},.1]]]

(*Advanced: Mathematica modelling of Propagation of errors*)

myrand:=2RandomReal[]-1 (*random number between -1 and 1*)

Table[myrand,{10}]

X:=x+0.5myrand (*X is a random "measurement", scattered around the fixed value of x. The 0.5*myrand is "noise" (experimental error) *)

x=3;listX=Table[X,{50}] (*is the result of 50 measurements*)

xmean=Mean[listX](*close to 3 since 50 is a "big" number*)

Show[ListPlot[listX, Filling->xmean,PlotRange->{0,5}],Graphics[Line[{{0,xmean},{50, xmean}}]], AxesLabel->{"number of trial","X"}]

(*iin the above vertical lines indicate the distance of individual measurement from the mean value of X. The average of those distances, given by MeanDeviation, is "simple error". The average of squares of the distances is "variance", and the root of variance is StandardDeviation*)

errx=MeanDeviation[listX]

sigx=StandardDeviation[listX]

Y:=y+0.3myrand (*another "experimental" variable with smaller average and noise*)

y=1;listY=Table[Y,{50}];

erry=MeanDeviation[listY]

sigy=StandardDeviation[listY]

listPlus=listX+listY; (* we measure X+Y*)

listMinus=listX-listY;(* we measure X-Y*)

Mean[listPlus](*close to sum of means*)

Mean[listMinus](*close to the difference*)

errPlus=MeanDeviation[listPlus]

errMinus=MeanDeviation[listMinus]

errx+erry (*simple errors do not add up!*)

sigPlus=StandardDeviation[listPlus]

sigx

sigy

Sqrt[sigx^2+sigy^2](*close to sigPlus, but not very since 50 is not too "big"*)

(*we now try a larger number of trials*)

X:=x+.2*myrand

x=5; listX=Table[X, {100000}];

MeanDeviation[listX]

StandardDeviation[listX]

Y:=y+0.3 myrand

y=3; listY=Table[Y, {100000}];

listZp=listX+listY;

listZm=listX-listY;

Mean[listZp]

Mean[listZm]

MeanDeviation[listZp]

sigP=StandardDeviation[listZp]

sigM=StandardDeviation[listZm](*very close !!*)

sigX=StandardDeviation[listX]

sigY=StandardDeviation[listY]

Sqrt[sigX^2+sigY^2](*should be close to sigP and/or to sigM*)

sigMinus=StandardDeviation[listMinus](*about the same*)

(* PRODUCT  X*Y , RATIO X/Y, etc.*)

listZprod=listX*listY;

Length[listZprod]

listZrat=listX/listY;

sd:=StandardDeviation

{sd[listX]/Mean[listX],sd[listY]/Mean[listY],sd[listZprod]/Mean[listZprod],sd[listZrat]/Mean[listZrat]}

Sqrt[(%[[1]])^2+(%[[2]]^2)]

listZpow=listX^3*listY^2;

{3sd[listX]/Mean[listX],2sd[listY]/Mean[listY],sd[listZpow]/Mean[listZpow]}

Sqrt[(%[[1]])^2+(%[[2]]^2)]

(*General function of X*)

listZsin=Sin[listX];

{sd[listX],sd[listZsin]}

%[[1]] Cos[Mean[listX]]

({{}})