Computing Derivatives

Clear[f,df,ddf,F,x1,x2]
f[x1_,x2_]=x1 Sin[x1 + (x2^2)]-Cos[x2] ;
df[x1_,x2_]=D[f[x1,x2],{{x1,x2}}];
ddf[x1_,x2_]=D[df[x1,x2],{{x1,x2}}];
F[]={0,0,0};
F[n_][{x1_,x2_}]:=Switch[n,
0,F[]+={1,0,0};f[x1,x2],
1,F[]+={0,1,0};df[x1,x2],
2,F[]+={0,0,1};ddf[x1,x2]
]

F[2][{x1,x2}]
F[]

Tangent Plane and Quadratic Approximations

x0={-0.1,0.1};
LinearApprox = F[0][x0]+F[1][x0].({x1,x2}-x0)
QuadApprox = F[0][x0]+F[1][x0].({x1,x2}-x0)+(1/2)({x1,x2}-x0).F[2][x0].({x1,x2}-x0)

a=2;
{
ContourPlot[F[0][{x1,x2}],
{x1,-a,a},
{x2, -a,a},
Epilog->{PointSize[0.02], Red, Point[x0]},
Contours->Table[z,{z,-3,3,0.1}],
PlotRange->{-3,3}
],
ContourPlot[QuadApprox,
{x1,-a,a},
{x2, -a,a},
Epilog->{PointSize[0.02], Red, Point[x0]},
Contours->Table[z,{z,-3,3,0.1}],
PlotRange->{-3,3}
],
ContourPlot[LinearApprox,
{x1,-a,a},
{x2, -a,a},
Epilog->{PointSize[0.02], Red, Point[x0]},
Contours->Table[z,{z,-3,3,0.1}],
PlotRange->{-3,3}
]
}

F[1][x0]

Line Searches

x0={1.2,3.4};
p={1,-1};
Grad=F[1][x0]
H=F[2][x0]
LineF[t_]=F[0][x0+t p];
LinearLineApp[t_]=F[0][x0]+t p.Grad
QuadLineApp[t_]=F[0][x0]+t p.Grad+(1/2)(t^2) p .H.p
Plot[ {
F[0][x0+t p],
LinearLineApp[t],
QuadLineApp[t]
},{t, -1,1}]

Back To 2D

x0={-0.5,0.2};
LinearApprox = F[0][x0]+F[1][x0].({x1,x2}-x0)
QuadApprox = F[0][x0]+F[1][x0].({x1,x2}-x0)+(1/2)({x1,x2}-x0).F[2][x0].({x1,x2}-x0)

Plot3D[{
F[0][{x1,x2}],
QuadApprox
},
{x1,-1,1},
{x2,-1,1}
]

{
ContourPlot[
F[0][{x1,x2}],
{x1,-1,1},
{x2,-1,1},
Contours->Range[-1,1,0.1],
PlotRange->{-1,1},
Epilog->{PointSize[0.02],Point[x0]}
],
ContourPlot[
QuadApprox,
{x1,-1,1},
{x2,-1,1},
Contours->Range[-1,1,0.1],
PlotRange->{-1,1},
Epilog->{PointSize[0.02],Point[x0]}
]
}

F[]

Norm[Grad]

Eigensystem[H]

?Switch

F[0][x1,x2]

?Evaluate

F[0][1.2,3.4]
F[]

Attributes[Module]

?Evaluate

f[]
f[0][1.2,3.4]
f[1][1.2,3.4]
f[2][1.2,3.4]

Clear[f]
Head[f]
f[x_]=Sin[x]
Head[f]

?Definition

Clear[f,Df,x]
f[]=0;Df[]=0;
f[x_]:=(f[]=f[]+1;Sin[x])
Df[x_]:=(Df[]=Df[]+1; Derivative[1][f][x])
df[x_]:=With[{x},(Df[]=Df[]+1; Derivative[1][f][x])]
Derivative[2][f]

Clear[f]
f[]={0,0,0};
f[0]=Function[{x1,x2}, 
f[]=f[]+{1,0,0};
Sin[x1+x1 x2]
];
f[1]=Function[{x1,x2}, 
f[]=f[]+{0,1,0};
Evaluate[D[f[0][x1,x2],{{x1,x2}}]]
];
f[0][1.2,1.3]
f[0][1.3,1.4]
f[1][1.2,1.3]
f[]

f[0][x1,x2]

D[f[0],x1]

Derivative[1][g]

?D

df[1.2]

Clear[f,x]
f[x_]=Sin[x];
Derivative[1][f][x]

A-B

Clear[f,x1,x2]
f[{x1_,x2_}]= x1 Sin[x2+x1]
Df[{x1_,x2_}]=D[f[{x1,x2}], {{x1,x2}}]
DDf[{x1_,x2_}]=D[Df[{x1,x2}], {{x1,x2}}]

ContourPlot[f[{x1,x2}],
{x1,-6,6},
{x2,-6,6}
]

?D

Clear[f,x]
D[f[x],x]//FullForm

Table[
x=RandomReal[{0,1}];
{x, f[x],f[]},
{23}
]