|
Алгоритм Свена
#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;
double f(double ) ;
int _tmain(int argc, _TCHAR* argv[])
{
double t,ll,e,l,xk,yk,a,b;
double x,delta,xp,x1,x2, k=1.0;
int p=0;
cout<< "enter x* ";//вводим приближенную точку минимума x*
cin>>x ;
cout<< "enter t ";//t — шаг
cin>>t;
x1=x-t;
x2=x+t;
if ((f(x-t) >=f(x)) && (f(x+t) >=f(x)))
{
a=x-t;
b=x+t;
cout<< "a="<<a<<"b="<<b<<endl ;
p=1;
};
if ((f(x-t) <=f(x)) && (f(x+t) <=f(x)))
{
cout<< "Funkziya ne unimodal'na "<<endl ;
p=1;
};
xp=x;
if ((f(x-t) >f(x)) && (f(x) >f(x+t)))
{
delta=t;
a=x ;
x=x+t;
}
if ((f(x-t) < f(x)) && (f(x) < f(x+t)))
{
delta=-t;
b=x ;
x=x-t;
}
while ((p!=1))
{
if ((f(x)< f(xp)) && (delta*t >0))
a=xp;
if ((f(x)< f(xp)) && (delta*t <0))
b=xp;
if ((f(x)> f(xp)) && (delta*t >0))
{
b=x;
p=1;
};
if ((f(x)> f(xp)) && (delta*t<0))
{
a=x;
p=1;
};
k++;
xp=x;
x=xp+pow(2.0,k)*delta;
}
cout << " a "<<a<< " b "<< b<< " k= " << k<< endl;
return 0;
}
double f(double x)
{
return 2*x*x+5*x-20;
} ;
Комментарии к тексту:
Александр, 09-03-2012, 11:28:16
Спасибо. Самый простой способ реализации!
Джигурда, 26-04-2012, 20:13:12
Самый явный алгоритм Свена
|
|