Quiet@Remove["Global`*"];
$HistoryLength=2;
SetDirectory[NotebookDirectory[]];

?_

NestList[f,x0,5]

(*the simpliest iteration*)
Quiet@Remove["Global`*"];
(*we solve eqn Cos[x]==x*)
xNext[xPrevious_]:=Cos[xPrevious];
xStart=0.123;
sol=NestList[xNext,xStart,20]
ListPlot[sol]

(((-3))^3)

((-27)^(1/3))//N

Quiet@Remove["Global`*"];
(*we solve eqn 5(x^3)+2(x^2)+x+3==0*)
xNext[xPrevious_]:=-(3.-2(xPrevious^2)-5(xPrevious^3));
xStart=0;
sol=NestList[xNext,xStart,10]

Solve[5(x^3)+2(x^2)+x+3==0]//N

FindRoot[5(x^3)+2(x^2)+x+3==0,{x,0.2+0.7I}]

Quiet@Remove["Global`*"];
(*we solve eqn 5(x^3)+2(x^2)+x+3==0*)
xNext[xPrevious_]:=(((1./5)*(-2(xPrevious^2)-xPrevious-3))^(1/3));
xStart=-5;
sol=NestList[xNext,xStart,10]

Quiet@Remove["Global`*"];
(*Newton Method*)
(*we solve eqn f[x]==0*)
f[x_]:=0.5x+0.1Sin[x];
xNext[xPrevious_]:=xPrevious-((f[xPrevious])/(f'[xPrevious]));
xStart=-5;
sol=NestList[xNext,xStart,5]

?/;

f[-6]*f[6]

Quiet@Remove["Global`*"];
(*bisection*)
f[x_]:=0.5x+0.1Sin[x]+1;
nextInterval[{a_,b_}]:={a,((a+b)/2)}/;f[a]*f[((a+b)/2)]<0;
nextInterval[{a_,b_}]:={((a+b)/2),b}/;f[b]*f[((a+b)/2)]<0;
nextInterval[{a_,b_}]:={a,a}/;f[a]==0;
nextInterval[{a_,b_}]:={b,b}/;f[b]==0;
startingInterval={-6.,6.};
NestList[nextInterval,startingInterval,10]

(*optimization*)
ConstrainedMin[2 x-3 y,{x+y<10,x-y>2,x>1},{x,y}]

(*for linear problems use LinearPrograming*)
LinearProgramming[{2,-3},(({{-1, -1}, {1, -1}, {1, 0}})),{-10,2,1}]

(x^2)+(y^3)/.x->Pi

?/.

(*patterns and functions*)
Remove["Global`*"]
f[x:_]:=(x^2);
f[5]

Remove["Global`*"]
f[x_]:=(x^2);
f[5]

(*for other problems we use NMinimize , NMaximize, FindMinimum, FindMaximum*)
(*example of constrained minimum*)
(*clasic solution, we nearly newer use it*)
expr=x+(y^2)+Sin[x+y];
constr=(x^2)+(y^2)==1;
g=constr/.lhs_==rhs_:>lhs-rhs;
lagrange=expr+λ*g;
eqns={D[lagrange,x]==0,D[lagrange,y]==0,D[lagrange,λ]==0}
FindRoot[eqns,{x,0.5},{y,0.5},{λ,0.5}]
FindRoot[eqns,{x,-0.5},{y,-0.5},{λ,1}]

expr=x+(y^2)+Sin[x+y];
constr=(x^2)+(y^2)==1;
NMinimize[{expr,constr},{x,y}]
NMaximize[{expr,constr},{x,y}]

Plot[Tanh[20(x-2)]-Tanh[20(x-4)],{x,-3,6}]

Remove["Global`*"]
expr=x+(y^2)+Sin[x+y];
constr1=2<x<4;
constr2=5<y<6;
NMinimize[{expr,constr1,constr2},{x,y}]

penaltyY=(((y-5.5))^2)*(2-(Tanh[200(y-5)]-Tanh[200(y-6)]))
Plot[penaltyY,{y,4,7}]

penaltyX=(((x-3))^2)*(2-(Tanh[200(x-2)]-Tanh[200(x-4)]))
Plot[penaltyX,{x,-3,9}];

newExpr=expr+penaltyX+penaltyY;
FindMinimum[newExpr,{{x,0},{y,0}}]

Head[5]

Head[5.]

(7/13)-(11./19)

Quiet@Remove["Global`*"];
(*optimization for solution of equations*)
eqns={x+y==1,x-2y==5.};
Solve[eqns]

forMin=Total[eqns/.lhs_==rhs_:>(((lhs-rhs))^2)]
NMinimize[forMin,{x,y}]

{a,b,c}=={d,e,f}
Thread[{a,b,c}=={d,e,f}]

matrix.{x,y,z}==vector

Quiet@Remove["Global`*"];
(*overdetermined systems*)
matrix=Table[Random[],{5},{3}];
vector=Table[Random[],{5}];
eqns=Thread[matrix.{x,y,z}==vector];
forMin=Total[eqns/.lhs_==rhs_:>(((lhs-rhs))^2)];
NMinimize[forMin,{x,y,z}]
b=Transpose[matrix].vector;
A=Transpose[matrix].matrix;
LinearSolve[A,b]//N

{MatrixForm[matrix],MatrixForm[vector]}

matrix.{x,y,z}==vector
Thread[matrix.{x,y,z}==vector]

LinearSolve

MatrixForm[Transpose[matrix].matrix]
Det[Transpose[matrix].matrix]//N

Quiet@Remove["Global`*"];
(*overdetermined systems*)
matrix=Table[i+j+(Sqrt[i*j]),{i,5},{j,3}];
vector=Table[Prime[i],{i,5}];
{MatrixForm[matrix],MatrixForm[vector]};
eqns=Thread[matrix.{x,y,z}==vector];
forMin=Total[eqns/.lhs_==rhs_:>(((lhs-rhs))^2)];
NMinimize[forMin,{x,y,z}]
b=Transpose[matrix].vector;
A=Transpose[matrix].matrix;
LinearSolve[A,b]//N

Quiet@Remove["Global`*"];
points={a,b,c,d}={{-1,-1.5},{3,1.},{1.,-2},{-1.5,-1.5}};
myPoint={myX,myY}={5,5};
distance[point:{x_,y_}]:=(Sqrt[(((myX-x))^2)+(((myY-y))^2)]);
distances=distance/@points

distance2[point:{x_,y_}]:=(Sqrt[(((myx-x))^2)+(((myy-y))^2)]);
error[point_,distance_]:=(((distance2[point]-distance))^2);
totalerror=error[a,8.9]+error[b,4.5]+error[c,8.1]+error[d,9.2];
NMinimize[totalerror,{myx,myy}]

Quiet@Remove["Global`*"];
(*a quadratic fit*)
originalFunction[x_]:=(x^2)-2x+1;
SeedRandom[1];
data=Table[{x,originalFunction[x]+RandomReal[{-0.5,0.5}]},{x,-2,3,0.2}];
pl1=ListPlot[data,PlotStyle->{Red,PointSize[0.015]}]

(*least squares*)

pl2=Plot[originalFunction[x],{x,-2,3}];
Show[pl1,pl2];
error[{x_,y_}]:=(((y-(a*(x^2)+b*x+c)))^2);
totalError=(∑_(i=1)^(Length[data]))error[data[[i]]];
NMinimize[totalError,{a,b,c}]

totalError//Simplify

eqns={D[totalError,a]==0,D[totalError,b]==0,D[totalError,c]==0}//Simplify
Solve[eqns]

pl1=ListPlot[data,PlotStyle->{Red,PointSize[0.015]}];
fit=Fit[data,{1,x,(x^2),(x^3),(x^4),(x^5),(x^6),(x^7)},x]
pl3=Plot[fit,{x,-2,3}];
Show[pl1,pl3]

Quiet@Remove["Global`*"];
(*example Fourier Series*)
f=50;
T=(1/f);
u[t_]:=Tanh[2*Sin[2*Pi*f*(t-0.123T)]];
pl1=Plot[u[t],{t,0,2T}];
data=Table[{t,u[t]},{t,0,T,(T/300.)}];
n=7;
fces=Table[{Sin[i*2*Pi*f*t],Cos[i*2*Pi*f*t]},{i,1,n,1}]//Flatten;
fit=Fit[data,fces,t];
pl2=Plot[fit,{t,0,2T},PlotStyle->{Red,Dashed}];
Show[pl1,pl2];

Quiet@Remove["Global`*"];
(*nonlinear problems*)
f[t_]:=3-2*(E^((-t)/3));
pl1=Plot[f[t],{t,0,10},AxesOrigin->{0,0}];
data=Table[{t,f[t]},{t,0,10,0.2}];
err[{t_,u_}]:=(((u-(a+b*(E^((-t)/τ)))))^2);
totalError=Total[err/@data];
NMinimize[totalError,{a,b,τ}];
params=FindFit[data,a+b*(E^((-t)/τ)),{a,b,τ},t];
fit=a+b*(E^((-t)/τ))/.params;
pl2=ListPlot[data,PlotStyle->{Red,PointSize[0.015]}];
pl3=Plot[fit,{t,0,10}];
Show[pl2,pl3];

Quiet@Remove["Global`*"];
(*numerical integration*)
(*Simpson's rule, derivation*)
expr[t_]:=a*(t^2)+b*t+c;
eqns={expr[t0-Δt]==fMinus,expr[t0+Δt]==fPlus,expr[t0]==f0};
fit=expr[t]/.Solve[eqns,{a,b,c}][[1]];
integral=Integrate[fit,{t,t0-Δt,t0+Δt}]//Simplify

{OddQ[5],OddQ[4]}
{EvenQ[5],EvenQ[4]}

Integrate[Sin[t],{t,0,Pi}]

Quiet@Remove["Global`*"];
(*numerical integration*)
(*Simpson's rule, example*)
(*nPoints must be odd integer*)
simpson[f_,tmin_,tmax_,nPoints_?OddQ]:=Module[{tValues,Δt,functionValues,triples},
Δt=((tmax-tmin)/(nPoints-1));
tValues=Range[tmin,tmax,Δt];
functionValues=f/@tValues;
triples=Partition[functionValues,3,2];
((Δt)/3)Total[triples/.{a_?NumericQ,b_,c_}:>a+4b+c]
];

((simpson[Sin,0.,Pi,5]-2)/2)*100

list=f[#]&/@Range[11]
triples=Partition[list,3,2]
Total[triples/.{a_,b_,c_}:>a+4b+c]

f/@{1,2,3,4}

Quiet@Remove["Global`*"];
(*numerical integration*)
(*Simpson's rule, example*)
(*nPoints must be odd integer*)
simpson[f_,tmin_,tmax_,nIntervals_?EvenQ]:=Module[{tValues,Δt,functionValues,triples},
Δt=((tmax-tmin)/nIntervals);
tValues=Range[tmin,tmax,Δt];
functionValues=f/@tValues;
triples=Partition[functionValues,3,2];
((Δt)/3)Total[triples/.{a_?NumericQ,b_,c_}:>a+4b+c]
];

((simpson[Sin,0.,Pi,4]-2)/2)*100

Quiet@Remove["Global`*"];
(*numerical integration*)
(*Simpson's rule, example*)
(*nPoints must be odd integer*)
simpson[f_,tmin_,tmax_,nPoints_?OddQ]:=Module[{tValues,Δt,functionValues,triples},
Δt=((tmax-tmin)/(nPoints-1));
tValues=Range[tmin,tmax,Δt];
functionValues=f/@tValues;
triples=Partition[functionValues,3,2];
((Δt)/3)Total[triples/.{a_?NumericQ,b_,c_}:>a+4b+c]
];

error[nPoints_?OddQ]:=((simpson[Sin,0.,Pi,nPoints]-2)/2)*100;
nMin=7;
nMax=51;
data=Table[{n,error[n]},{n,nMin,nMax,2}];
pl1=ListPlot[data,PlotRange->All];
fit=Fit[data,{(1/(n^4))},n];
pl2=Plot[fit,{n,nMin,nMax},PlotRange->All];
Show[pl1,pl2,PlotLabel->fit]