Clear all symbols from previous evaluations to avoid problems

Clear["Global`*"]

Find a minimum of a given function

1D function

g[x_]:=x^4-10x^2;
Solve[D[g[x],x]==0,x]

f[x_]:=x[[1]]^4-10x[[1]]^2;
Plot[f[{x}],{x,-4,4}]
{point,value,niter}=MyMinimization[{0.0},{-0.2}, f, 10^(-10)]
N[{Sqrt[5],g[Sqrt[5]]}]

{point,value,niter}//InputForm

f[x_]:=Module[
{n,i},
n=Length[x];
Return[-Exp[-Sum[(x[[i]]-1.5)^2,{i,1,n}]]-Exp[-Sum[(x[[i]]-3.5)^2,{i,1,n}]]]
];
Plot3D[f[{x,y}],{x,0,5},{y,0,5}]

{point,value,niter}=MyMinimization[{4.5,0.5}, {0.01,0.01}, f, 10^(-10)]

f[x_]:=Module[
{n,i},
n=Length[x];
Return[Sin[x[[1]]*x[[2]]]]
];
Plot3D[f[{x,y}],{x,-5,5},{y,-5,5}]

{point,value,niter}=MyMinimization[{0.5,0.1}, {0.1,0.1}, f, 10^(-8)]

Nonlinear least-square fitting

Fitting a function with noise

myf[x_,p1_,p2_,p3_]:=p1*Exp[-p2*(x-p3)^2];
n=40;
d=0.1;
grid=Table[0.0+i*0.1,{i,1,n}];
data=Table[myf[grid[[i]],2.0,2.0,2.0]+RandomReal[{-d,d}],{i,1,n}];

f[par_]:=Module[
{i,fx,squares},
squares=0.0;
Do[
fx=myf[grid[[i]],par[[1]],par[[2]],par[[3]]]-data[[i]];
squares+=fx*fx,
{i,1,n}
];
Return[squares]
];
{point,value,niter}=MyMinimization[{2.0,2.0,2.0}, {0.01,0.01,0.01}, f, 10^(-8)]
{point,value,niter}=MyMinimization[{0.0,0.0,1.0}, {1.0,1.0,1.0}, f, 10^(-8)]

data2=Table[myf[grid[[i]],2.0,2.0,2.0],{i,1,n}];
data3=Table[myf[grid[[i]],Delete[point,0]],{i,1,n}];
ListPlot[{Transpose[{grid,data}],Transpose[{grid,data2}],Transpose[{grid,data3}]},Joined->{False, True,True}]