Calculus & more

Rules

In several cases the result of a computation is a rule.

Clear[a,b,c]
Rule[a,2]
FullForm[a->2]

Rules are good for replacing a part of an expression.

ReplaceAll[a^2+a+3,a->2]

Or in a shorter form:

a^2+a+3/.a->2
a^2+a+3/.a->x+y

a^2+b^2/.{a->3,b->4}
a^2+b^2/.{a->3,b->4+a}
a^2+b^2//.{a->b,b->4+a}

a^2+b^2/.{{a->3,b->4},{a->1,b->2},{a->7}}

Use it for parameters, instead of using variables:

params={a->1,b->2,c->3};
a+b/.params
b+c/.params

When a rules are dependent to each other use repeated replace to apply the rules repeatedly until no rule remains to be applied.

L={J->(U/(Subscript[R, t])),(Subscript[R, t])->(Subscript[R, 1])+(Subscript[R, 2]),(Subscript[U, 1])->J (Subscript[R, 1]),(Subscript[U, 2])->J (Subscript[R, 2]),(Subscript[R, 1])->10,(Subscript[R, 2])->3,U->3.3};

This is not what we want:

((Subscript[U, 1])/(Subscript[R, 1]))/.L

This is what we want:

ReplaceRepeated

((Subscript[U, 1])/(Subscript[R, 1]))//.L

(Subscript[U, 1])//.L

Solving Equations

Polynomial equations

The solutions are rules.

Solve[x==3]
Solve[x^2+2x-2==0]
Solve[x^2+2x-2==0.]

If there are other symbols (parameters) in the equation beside of the variable, one needs to explicitely state which symbol is the variable.

Solve[a x^2+b x+c==0]
Solve[a x^2+b x+c==0,x]

Reduce[a x^2+b x+c==0,x]

x/.Solve[x^2+2x-2==0]

Sin[x]/.Solve[x^2+2x-2==0]

Substituting back:

quad=a x^2+b x+c
quad/.Solve[quad==0,x]
%//Simplify

For higher order polynomials the roots represented by an abstract object:

Solve[x^5+x^3+x^2+x+1==0]
%//N

Can be calculated numerically as well:

NSolve[x^5+x^3+x^2+x+1==0]

Constrains on the solutions:

x/.NSolve[x^5+x^3+x^2+x+1==0,Reals]
x/.NSolve[x^5+x^3+x^2+x+1==0,Complexes]

Solve[24-32 x+10(x^2)==0]
Solve[24-32 x+10(x^2)==0,Integers]
Solve[24-32 x+10(x^2)==0,Rationals]

Visualizing the roots:

p=x^2+2x-3;
Point[{x,p}]/.{{x->3.5},{x->-1.2}}
Point[{x,p}]/.Solve[p==0,x]

Plot[p,{x,-5,3},Epilog->{PointSize[Large],Orange,Point[{x,p}]/.Solve[p==0]}]

System of linear equations

A=RandomInteger[{0,100},{4,4}]
B=RandomInteger[{0,100},4]
X={x1,x2,x3,x4}

A.X

Solve[A.X==B]
A.X/.%

Eliminating variables

Eliminate[{x+y==z,x-y==0},x]

Kirchoff equations for T circuit:

eqs={u[1]-i[1]R[1]-i[3]R[3]==0,u[2]-i[2]R[2]-i[3]R[3]==0,i[1]+i[2]-i[3]==0};

Hybrid parameters:

Solve[eqs,{u[1],i[2]}]

Eliminate[eqs,{i[3]}]
{u[1],i[2]}/.Solve[%,{u[1],i[2]}]//Flatten

Solve[eqs,{u[1],i[2]},{i[3]}]

Numeric Solutions

Solve[Log[x]==Exp[-x],x]

FindRoot[Log[x]==Exp[-x],{x,1.2}]
Plot[{Log[x],Exp[-x]},{x,0.3,2},Epilog->{PointSize[Large],Red,Point[{x,Log[x]}]/.%}]

Numeric solution:

Clear[f]
f[a_]:=x/.FindRoot[Log[x]==a Exp[-x],{x,1.}]

f[0]

f[a]

Plot[Evaluate@f[a],{a,0,10}]

FindRoot[f[a]==2,{a,5.}]

FindRoot tries to evaluate symbolically the function f, which does not work:

f[a]

One has to define the function so it requires a numeric input, therefore forcing f to evaluate on numerical values.

NumericQ[1]
NumericQ[a]

ClearAll[f]
f[a_?NumericQ]:=x/.FindRoot[Log[x]==a Exp[-x],{x,1.}]

f[a]
f[1.2]

FindRoot[f[a]==2,{a,5.}]

Differential and integral calculus

Differentiation

Clear[f,g]

D[3x,x]

D[f[x],x]
f'[x]

D[f[x]g[x],x]
D[f[g[x]],x]

D[f[x],x,x]
D[f[x],{x,2}]
f''[x]

D[(x^2) Exp[x],{x,3}]

Dt[a b Sin[γ],a]

D[(x^2)Exp[y]+(y^3)Cos[x],x]
D[(x^2)Exp[y]+(y^3)Cos[x],y]
D[(x^2)Exp[y]+(y^3)Cos[x],x,y,y]
D[(x^2)Exp[y]+(y^3)Cos[x],{x,1},{y,2}]

f[x_]:=Sin[x]
l[a_,x_]:=f[a]+f'[a](x-a);

Manipulate[
Plot[{Sin[x],l[a,x]},{x,-3,3},PlotRange->{{-3,3},{-1.5,1.5}}]
,{a,-3,3}]

Integration

Clear[a,b,n]
Integrate[(x^n),x]
Integrate[(x^n),{x,0,a}]
Integrate[(x^n),{x,0,a},Assumptions->{n≥0}]

Integrate[(x^2)Cos[x],x]

Numerical integration.

Integrate[(x^2)Cos[x],{x,-1,1}]
NIntegrate[(x^2)Cos[x],{x,-1,1}]

Integrate[Exp[-(x^2)],{x,-1,1}]
NIntegrate[Exp[-(x^2)],{x,-1,1}]

Taylor-series and Fourier-series

Taylor-series

Series[Exp[x],{x,0,3}]
%//Normal

Series[Exp[x],{x,0,3}]
%^2

Series[Exp[x],{x,0,3}]
%/.x->1.5

Series[Exp[x],{x,0,3}]
Normal[%]/.x->1.5

Clear[f,x]
n=5;
a=6;
f[x_]:=(x^2)Exp[-x]
fApprox[n_]:=Function[{x},Normal[Series[f[x],{x,a,n}]]]
Plot[{f[x],Evaluate[fApprox[n][x]]},{x,0,10},PlotRange->{0,0.7},Epilog->{Red,PointSize[Large],Point[{a,f[a]}]}]

Series[(x^3)+2(x^2)-x+3,{x,5,3}]

Symbolically

Clear[f,a,δ]
Series[f[x],{x,a,3}]

Series[f[x+δ],{δ,0,3}]

Normal[Series[(Sqrt[4+δ]),{δ,0,3}]]/.δ->1.0`5
(Sqrt[5.0`6])

Fourier-series

Clear[f,fs]
f[t_]:=Piecewise[{{Sin[t],0≤Mod[t,2π]<π}},0]
Plot[f[t],{t,0,4π}]

Clear[n]
fc=FourierCoefficient[f[t],t,n]

fc/.n->1
Limit[fc,n->1]

DiscretePlot[Abs[Limit[fc,n->k]],{k,-10,10}]

fs=FourierSeries[f[t],t,5]

fs//ExpToTrig
fs//Simplify
fs//FullSimplify

Or using TrigSeries:

fs=FourierTrigSeries[f[t],t,4]

Plot[{f[t],fs},{t,-2π,4π}]

FourierCosSeries[f[t],t,4]
FourierSinSeries[f[t],t,4]

An other example:

FourierTrigSeries[SquareWave[t/(2π)],t,5]//Expand
Plot[{SquareWave[t/(2π)],%},{t,0,4π},ExclusionsStyle->Dashed]

If the function has other period than 2π use the option FourierParameters.

Interpolation, curve fitting

Interpolation

data={{0,0},{1,2},{2,1},{3,3},{4,2}};

Clear[f]
InterpolatingPolynomial[data,t]//Expand
Plot[%,{t,-0.2,4.2},Epilog->{PointSize[Large],Red,Point[data]}]

Piece wise interpolation

f=Interpolation[data,t,InterpolationOrder->2]
Plot[f,{t,0,4},Epilog->{PointSize[Large],Red,Point[data]}]

f

f
f/.t->4.1

Spline interpolation

Clear[f]
data={{0,0},{1,2},{2,1},{3,3},{4,0}};
f [t_]=Interpolation[data,t,Method->"Spline",InterpolationOrder->2]
Plot[f[t],{t,0,4},Epilog->{PointSize[Large],Red,Point[data]}]
Plot[{f[t],f'[t],f''[t]},{t,0,4}]

For visualization.

ListLinePlot[data]
ListLinePlot[data,InterpolationOrder->2]

Curve fitting

Linear model fitting

Clear[data]
data={{0,2},{1,0.8},{2,1.9},{3,2.5},{4,4}};

ListPlot[data]

f1[t_]=Fit[data,{1,t},t]
f2[t_]=Fit[data,{t},t]
f3[t_]=Fit[data,{1,t,(t^2)},t]
Plot[{f1[t],f2[t],f3[t]},{t,0,4},PlotRange->{-1,4.5},PlotStyle->{Automatic,Black,Red},Epilog->{PointSize[Large],Darker@Blue,Point[data]}]

f4[t_]=Fit[data,{1,t,(t^2),Exp[t],Exp[-t]},t]
Plot[{f3[t],f4[t]},{t,0,4},PlotRange->{-1,4.5},Epilog->{PointSize[Large],Red,Point[data]}]

Map[Exp[-(((t-First[#]))^2)]&,data]

Clear[f5]
f5[t_]=Fit[data,Map[Exp[-(((t-First[#]))^2)]&,data],t]
Plot[{f1[t],f5[t]},{t,-3,7},PlotStyle->{Automatic,Red},PlotRange->{-1,4.5},Epilog->{PointSize[Large],Darker@Blue,Point[data]}]

Use LinearModelFit to obtain more statistical information.

data=Array[{#,0.5#+1+RandomReal[{-0.5,0.5}]}&,50,{0.,4.}];
ListPlot[data]

Clear[fm]
fm=LinearModelFit[data,{t},t]
LinearModelFit[data,{t},t,IncludeConstantBasis->False]

fm["Properties"]

fm["Data"]
fm["BestFit"]
fm["BasisFunctions"]
fm["ANOVATable"]
fm["RSquared"]

Plot[Evaluate@{fm["BestFit"]},{t,-1,5},PlotStyle->{{Red},{Black,Dashed},{Black,Dashed}},Epilog->{PointSize[Medium],Darker@Blue,Point[data]}]

Clear[fm]
Array[(t^(#))&,3]
fm=LinearModelFit[data,Array[(t^(#))&,3],t]
Plot[Evaluate@fm["BestFit"],{t,-1,5},PlotRange->{0,4},PlotStyle->{{Red},{Black,Dashed},{Black,Dashed}},Epilog->{PointSize[Medium],Darker@Blue,Point[data]}]

Non-linear model fitting

data={{0,2},{1,0.8},{2,1.9},{3,2.5},{4,4}};

Clear[f]
f[α_,a_,b_]:=Function[{t},a Exp[α t]+b]

f[3, 4, 5][t]
f[a, b, c][t]

FindFit[data,f[α,a,b][t],{α,a,b},t]
Plot[f[α,a,b][t]/.%,{t,-1,5},PlotRange->{-1,4.5},Epilog->{PointSize[Large],Red,Point[data]}]

Clear[fm,a,b,t]
NonlinearModelFit[data,f[α,a,b][t],{α,a,b},t]
%["BestFit"]

Clear[f]
f[α_,β_,a_,b_]:=Function[{t},a Sin[α t]+b Cos[β t]]

FindFit[data,f[α,β,a,b][t],{α,β,a,b},t]
Plot[f[α,β,a,b][t]/.%,{t,-5,20},PlotRange->All,Epilog->{PointSize[Large],Red,Point[data]}]

Fourier and Laplace Transform

Fourier Transform

FourierTransform[1,t,ω]
FourierTransform[DiracDelta[t],t,ω]

x[t_]:=UnitStep[t+1]-UnitStep[t-1]

Plot[x[t],{t,-2,2},Filling->Bottom]

FourierTransform[x[t],t,ω]
Plot[Abs[%],{ω,-30,30},PlotRange->All]

Discrete Fourier Transform

f=10;
Ts=1/(10f);
samples=1000;
x[t_]:=SawtoothWave[f t]-0.5
Plot[x[t],{t,0,3/f},ExclusionsStyle->Dashed]
data=Table[x[k Ts],{k,0.,samples}];
Fourier[data];
ListLinePlot[Abs[%],PlotRange->All,DataRange->{0,1/Ts}]

Module[{f=100,ω,Ts,samples=1000,x,data,fData},
ω=2π f;
Ts=1/(11f);
x[t_]=Sin[ω t]+0.5Sin[3 ω t]-0.1Sin[5ω t]+RandomVariate[NormalDistribution[0,2.0]];
data=Table[x[k Ts],{k,0.,samples}];
fData=Fourier[data];
{Plot[x[t],{t,0,3/f}],
ListLinePlot[Abs[fData],PlotRange->All,DataRange->{0,1/Ts}]}
]

InverseFourier[filtData]//Chop;
ListLinePlot[%]

Laplace Transform

LaplaceTransform[Exp[-α t],t,s]
LaplaceTransform[Exp[-2t]Cos [5t],t,s]

InverseLaplaceTransform[(3/(s(s+3))),s,t]//Expand

Solving Differential Equations using Laplace Transform

ClearAll[x]
LaplaceTransform[x[t],t,s]//TraditionalForm

ClearAll[x,t,s]
LaplaceTransform[x'[t]==x[t],t,s]
Solve[Flatten@{%,x[0]==1},LaplaceTransform[x[t],t,s],{x[0]}]
InverseLaplaceTransform[%,s,t]

Solving the resulting equation.

Clear[X,Y]
X=LaplaceTransform[x[t],t,s];
Y=LaplaceTransform[y[t],t,s];
LaplaceTransform[#,t,s]&/@{x'[t]==x[t]+3y[t]+t,y'[t]==-3x[t]+y[t]};
Solve[Flatten@{%,x[0]==1,y[0]==3},{X,Y},{x[0],y[0]}]
InverseLaplaceTransform[%,s,t]
%//ExpToTrig
%//FullSimplify

Differential equations

Symbolic solutions

Clear[x]

DSolve[x'[t]==k x[t],x[t],t]

With initial value:

DSolve[{x''[t]+2x'[t]+3x[t]==0,x[0]==1,x'[0]==0},x[t],t]

DSolveValue[x'[t]==k x[t],x[t],t]

Numeric solutions

NDSolve[{x'[t]==x[t],x[0]==0.1},x[t],{t,0,10}]

NDSolveValue[{x'[t]==x[t],x[0]==0.1},x[t],{t,0,10}]
Plot[%,{t,0,10}]

Parametric equations

sol=ParametricNDSolveValue[{x'[t]==k x[t],x[0]==x0},x,{t,0,10},{k,x0}]

sol[-0.5,1]

Plot[sol[-0.8,1][t],{t,0,10},PlotRange->All]

Manipulate[Plot[sol[k,x0][t],{t,0,10},PlotRange->{-2,2}],{k,-2,2},{x0,-2,2}]

Hybrid equations

Bouncing ball:

sol=NDSolveValue[{x''[t]==-10,x[0]==1,x'[0]==0,WhenEvent[x[t]==0,x'[t]->-0.9x'[t]]},x[t],{t,0,10}]

Plot[sol,{t,0,8}]

sol=NDSolveValue[{x''[t]==-10,x[0]==1,x'[0]==0,WhenEvent[x[t]==0,{"StopIntegration"}]},x[t],{t,0,∞}]

Plot[sol,{t,0,0.447}]