(*FUNCTIONS and their Plots*)

(*1*)

Clear[f,x];f=Sin[x]/x

f/.x->Pi/2 (*replacement /.  *)

Plot[f,{x,-8,8}]

(*2*)Clear[f]; f[x_]:=Sin[x]/x

f[y]

f[3.]

Plot[f[x],{x,-8,8}]; (*same output*)

(*3 - risky but fast; must clear x*) Clear[f,x]; f[x_]=Sin[x]/x

Plot[f[x],{x,-8,8}]; (*same*)

(*4 - pure function, note "&"*)Clear[f];f:=Sin[#]/# &

f[z]

f[Cos[x]]

(*plot same as before*)

(*for external graphics*)

disp:=Export["t.ps",#1,"EPS"]&

Plot[f[x],{x,-8,8},PlotStyle->Thick]

disp[%](*creates "t.ps" *)

(*plotting with options*)

Clear[f,ef,x];f=Sin[x];ef=D[f,x];Show[Plot[{f,ef},{x,-3,3},PlotStyle->{Black,Red,},PlotLegends->"Expressions",LabelStyle->Medium],PlotRange->{-1.5,1.5},GridLines->Automatic,AxesLabel->{x,y},AspectRatio->1]

disp[%]

(*MORE on PURE FUNCTIONS*)

Clear[f];f:=1/(1+#)&

f[x]

f[%]

(*continued fraction; Nest command does it*)

Nest[f,x,4]

(*Newton's method to find roots*)

der:=D[#,x]& (*derivative, but in x only*)

step:=#-(f/der[f]/.x->#)& (*one step in Newton's method*)

Clear[f];f=x^2-2; (*to find Sqrt[2]; 3 will be initial guess; 4 iterations*)

Nest[step,3,4]

%-Sqrt[2]//N (*after only 4 steps with a poor initial guess!!*)

(*NestList "remebers the intermediate steps; will improve the initial guess  to 2*)

NestList[step,2,6]

%-Sqrt[2.]

N[%%-Sqrt[2],20]

(*note:number of correct digits DOUBLES with every step*)

(*illustrative plot with a bad 1st guess*)

list=NestList[step,3,4];

flist=f/.x->list//N

Show[Plot[{f},{x,0,3},PlotStyle->{Black},PlotRange->{-2,7},GridLines->Automatic,AxesLabel->{"x","f"},LabelStyle->Medium],Graphics[{Red,Line[{{list[[1]],flist[[1]]},{list[[2]],0},{list[[2]],flist[[2]]},{list[[3]],0},{list[[3]],flist[[3]]}}]}],AspectRatio->1]