5. Исходный текст программы.
//---------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------
#include <conio.h>
#include <stdio.h>
#include <math.h>
//---------------------------------------------------------------
// Модуль вещественного числа
double abs(const double a){
return (a<0)? -a: a;
}
//---------------------------------------------------------------
//целевая функция
//возвращает значение функции в точке абсциссы
double fun(const double x){
return x*x*x*x-14*x*x*x+60*x*x-70*x;
}
//---------------------------------------------------------------
//алгоритм Свенна, выбирающий интервал неопределенности
bool Swann(float start_point,double *a,double *b){
double t=0.1,d,x1,x2;
int k=0;
if ((fun(start_point-t)>=fun(start_point))&&(fun(start_point)<=fun(start_point+t))){
*a=start_point-t; *b=start_point+t; return true;}
else if ((fun(start_point-t)<=fun(start_point))&&(fun(start_point)>=fun(start_point+t)))
return false;
if ((fun(start_point-t)>=fun(start_point))&&(fun(start_point)>=fun(start_point+t))) {
d=t; *a=start_point; x1=start_point+t; k++;}
else if ((fun(start_point-t)<=fun(start_point))&&(fun(start_point)<=fun(start_point+t))) {
d=-t; *b=start_point; x1=start_point-t; k++;}
x2=x1+2*d;
if (fun(x2)<fun(x1))
while (true){
if (d==t) *a=x1; else *b=x1;
k++;
x1=x2;
x2=x1+pow(2,k)*d;
if (fun(x2)>=fun(x1)) break;
}
if (k==1) if (d==t) *b=x2; else *a=x2;
else if (d==t) *b=x1; else *a=x1;
return true;
}
//---------------------------------------------------------------
//метод дихотомии
void Dixotomia(double &a, double &b, bool alone=false){
double x1,x2, eps=0.001, delta=0.00001,len;
int k=0;
printf("Method Dixotomia is working...\n\n");
Swann(2,&a,&b);
printf("Interval po Swann: [%.3f;%.3f]\n",a,b);
do {
k++;
len=(b+a)/2;
x1=len-delta/2; x2=len+delta/2;
if (fun(x1)<=fun(x2)) b=x2; else a=x1;
if (!alone && k>4) break; //делаем 5-8 итераций методом Дихотомии если надо
} while ((b-a)>=eps);
printf("%f - answer from Dihotomia \n0.780884 - right answer\n za %d steps\n\n", (a+b)/2,k);
printf("Interval after Dihotomia [%.5f;%.5f]\n\n",a,b);
}
//---------------------------------------------------------------
//метод Пауэлла
void Pauell(double &a,double &b, bool alone=false){
double x1,x2,x3,dx,f1,f2,f3,fd,d,fmin,xmin;
int k=0;
double eps=0.0000001;
printf("Method Pauella is working...\n\n");
if (alone) {Swann(2,&a,&b); printf("Interval po Swann: [%.3f;%.3f]\n",a,b);}
dx=(a<0)? 0.01*(-a): 0.01*a;
x2=(a+b)/2;
do {
if (k>0)x2=(f1<fd)? x1: d;
k++;
x1=x2-dx; x3=x2+dx;
f1=fun(x1); f2=fun(x2); f3=fun(x3);
if (f1<f3) {if (f2<f1) {fmin=f2;xmin=x2;} else {fmin=f1;xmin=x1;}}
else {if (f2<f3) {fmin=f2;xmin=x2;} else {fmin=f3;xmin=x3;}}
if (k==1) d=0.5*( (x2*x2-x3*x3)*f1+(x3*x3-x1*x1)*f2+(x1*x1-x2*x2)*f3 )/ ( (x2-x3)*f1+(x3-x1)*f2+(x1-x2)*f3 );
else d=(x1+x2)/2+0.5*(f1-f2)*(x2-x3)*(x3-x1)/(f1*(x2-x3)+f2*(x3-x1)+f3*(x1-x2));
fd=fun(d);
if (alone && k>19) break;
}while (abs((fmin-fd)/fd)>eps && abs((xmin-fd)/fd)>eps );
printf("%f - answer from Pauell\n0.780884 - right answer\n za %d steps \n\n",(x2+d)/2,k);
}
//---------------------------------------------------------------
//главная функция
int main() {
double a,b;
Dixotomia(a,b,true);
printf("press any key to continue..\n");
getch();
printf("#########################################\n\n");
Pauell(a,b,true);
printf("press any key to continue..\n");
getch();
printf("#########################################\n\n");
Dixotomia(a,b);
Pauell(a,b);
printf("\n<PRESS ANY KEY TO EXIT>");
getch();
}