{ поиск минимума методом квадратичной интерполяции }
function findMinQuadApprox(xs, h, eps1, eps2: double;
max_step: longint): double;
var
x, f_x: array[0 .. 2] of double;
k: longint;
i_min, i_max: integer;
xn, f_xn, a1, a2: double;
begin
x[0] := xs; x[1] := xs + h;
if f(x[0]) > f(x[1]) then
begin
x[2] := xs + 2 * h
end
else
begin
x[2] := xs - h
end;
f_x[0] := f(x[0]); f_x[1] := f(x[1]); f_x[2] := f(x[2]);
for k := 0 to pred(max_step) do
begin
if f_x[0] < f_x[1] then
begin
if f_x[0] < f_x[2] then
i_min := 0
else i_min := 2
end
else
begin
if f_x[1] < f_x[2] then
i_min := 1
else i_min := 2;
end;
a1 := (f_x[1] - f_x[0]) / (x[1] - x[0]);
a2 := 1.0 / (x[2]-x[1])*((f_x[2]-f_x[0]) / (x[2]-x[0])-(f_x[1]-f_x[0]) / (x[1]-x[0]));
xn := (x[1]+x[0])*0.5-a1/(2*a2);
f_xn := f(xn);
if (abs((xn-x[i_min])/xn) < eps1) and
(abs((f_xn-f_x[i_min])/f_xn) < eps2) then break;
if f_x[0] >= f_x[1] then
begin
if f_x[0] > f_x[2] then
i_max := 0
else i_max := 2;
end
else
begin
if f_x[1] > f_x[2] then
i_max := 1
else i_max := 2;
end;
if f_xn < f_x[i_min] then
begin
x[i_max] := xn;
f_x[i_max] := f_xn;
end
else
begin
x[i_max] := 2*x[i_min]-xn;
f_x[i_max] := f(x[i_max]);
end;
end;
findMinQuadApprox := xn;
end;
помогите перекодировать в С++ из pascal
Ты начни сам: замени begin на {, end; на } , if поменяй, for попытайся. Потом уж поправим, что останется.
Код:
double findMinQuadAppropos(double xs, double h, double eps1, double eps2, long max_step) {
double x[3],f_x[3];
long k;
int i_min, i_max;
double xn, f_xn, a1, a2;
x[0]=xs; x[1]=xs+h;
if ( f(x[0]) > f(x[1]) )
x[2] = xs + 2*h;
else
x[2] = xs - h;
for (k=0; k<3; k++)
f_x[k] = f(x[k]);
for (k=0; k<max_step; k++) {
if (f_x[0] < f_x[1]) {
if (f_x[0] < f_x[2])
i_min = 0;
else
i_min = 2;
}
else {
if (f_x[1] < f_x[2])
i_min = 1;
else
i_min = 2;
}
a1 = (f_x[1] - f_x[0]) / (x[1] - x[0]);
a2 = 1.0 / (x[2]-x[1])*((f_x[2]-f_x[0]) / (x[2]-x[0])-(f_x[1]-f_x[0]) / (x[1]-x[0]));
xn = (x[1]+x[0])*0.5-a1/(2*a2);
f_xn = f(xn);
if ((abs((xn-x[i_min])/xn) < eps1) &&
(abs((f_xn-f_x[i_min])/f_xn) < eps2)) break;
if (f_x[0] >= f_x[1]) {
if (f_x[0] > f_x[2])
i_max = 0;
else
i_max = 2;
}
else {
if (f_x[1] > f_x[2]}
i_max = 1;
else
i_max = 2;
}
if (f_xn < f_x[i_min]) {
x[i_max] = xn;
f_x[i_max] = f_xn;
}
else{
x[i_max] = 2*x[i_min]-xn;
f_x[i_max] = f(x[i_max]);
}
}
return xn;
}
double x[3],f_x[3];
long k;
int i_min, i_max;
double xn, f_xn, a1, a2;
x[0]=xs; x[1]=xs+h;
if ( f(x[0]) > f(x[1]) )
x[2] = xs + 2*h;
else
x[2] = xs - h;
for (k=0; k<3; k++)
f_x[k] = f(x[k]);
for (k=0; k<max_step; k++) {
if (f_x[0] < f_x[1]) {
if (f_x[0] < f_x[2])
i_min = 0;
else
i_min = 2;
}
else {
if (f_x[1] < f_x[2])
i_min = 1;
else
i_min = 2;
}
a1 = (f_x[1] - f_x[0]) / (x[1] - x[0]);
a2 = 1.0 / (x[2]-x[1])*((f_x[2]-f_x[0]) / (x[2]-x[0])-(f_x[1]-f_x[0]) / (x[1]-x[0]));
xn = (x[1]+x[0])*0.5-a1/(2*a2);
f_xn = f(xn);
if ((abs((xn-x[i_min])/xn) < eps1) &&
(abs((f_xn-f_x[i_min])/f_xn) < eps2)) break;
if (f_x[0] >= f_x[1]) {
if (f_x[0] > f_x[2])
i_max = 0;
else
i_max = 2;
}
else {
if (f_x[1] > f_x[2]}
i_max = 1;
else
i_max = 2;
}
if (f_xn < f_x[i_min]) {
x[i_max] = xn;
f_x[i_max] = f_xn;
}
else{
x[i_max] = 2*x[i_min]-xn;
f_x[i_max] = f(x[i_max]);
}
}
return xn;
}
max_dark, сам перевел или программкой какой-нибудь?
В ручную. Просто бездельем маюсь :)
всем спасибо, осталось додумать )