Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Метод Фиббоначчи / Fibona4i1-dsk
.cpp
#include <iostream.h>
#include <conio.h>
#include <math.h>
//+++++++++++++++++++++++++++++++++++++++++++++
int k,k1,k2;
//+++++++++++++++++++++++++++++++++++++++++++++
double f (double x)
{
return (2*pow(x,2)-pow(2.718281828,x));
}
//+++++++++++++++++++++++++++++++++++++++++++++
long Fib (long x)
{
long s, tmp;
if(x>=1)
{
s=1;
tmp=0;
for(long k=0; k<x; k++)
{
s=s+tmp;
tmp = s-tmp;
}
return s;
}
else
return 1;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
void Svenn(double *a, double *b, double x, double h)
{
k=1;
if(f(x)<f(x+h))
h=-h;
while(f(x)<f(x-h))
{
h=2*h;
x=x+h;
k++;
}
*b=x-h-h/2;
*a=x;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
double F1(double a, double b, double e)
{
k=1;
double Ln=0.01*e,l,m;
long n=0;
while(Fib(n)<(b-a)/Ln)
n++;
l=a+(Fib(n-2)*(b-a))/(Fib(n));
m=a+(Fib(n-1)*(b-a))/(Fib(n));
while(k<n-1)
{
if(f(l)<f(m))
{
b=m;
m=l;
l=a+(Fib(n-k-2)*(b-a))/Fib(n-k);
}
else
{
a=l;
l=m;
m=a+(Fib(n-k-1)*(b-a))/Fib(n-k);
}
k++;
}
if(f(l)<=f(m))
return (a+m)/2;
else
return (l+b)/2;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double DSK(double *a,double *b, double *c,double x0, double h)
{
k=1;
k1=1;
double x1,d,x,e=0.01;
do
{
x1=x0+h;
if(f(x0)<f(x1))
{
h=-h;
x1=x0+h;
}
do
{
h=2*h;
x1=x1+h;
k++;
}
while(f(x1-h)>f(x1));
x=(x1+(x1-h))/2;
if(f(x)<f(x1-h))
{
*a=x1-h;
*b=x;
*c=x1;
}
else
{
*a=x1-1.5*h;
*b=x1-h;
*c=x;
}
if(*a>*c)
{
x=*a;
*a=*c;
*c=x;
}
d=(*b)+1/2*(((*b)-(*a))*(f(*a)-f(*c)))/(f(*a)-2*f(*b)+f(*c));
if(fabs((d-(*b))/(*b))<=e && fabs((f(d)-f(*b))/f(*b))<=e)
return ((*b)+d)/2;
else
{
if(f(*b)<f(d))
x0=(*b);
else
x0=d;
}
h=h/2;
k1++;
}
while(1);
}
//++++++++++++++++++++++++++++++++++++++++++++
void main()
{
// clrscr();
double h=0.01,a,b,c,x0=1,e=0.001;
cout<<"e="<<e<<" x0="<<x0<<endl;
cout<<endl;
cout<<"--- Svenn ---"<<endl;
Svenn(&a,&b,x0,h);
cout<<"a="<<a;
cout<<" b="<<b;
cout<<" k="<<k<<endl;
cout<<endl;
cout<<"--- Fibonachchi-1 ---"<<endl;
double x=x0;
x=F1(a,b,e);
cout<<"x="<<x;//<<endl;
cout<<" k="<<k<<endl;
cout<<endl;
cout<<"--- Svenn2 ---"<<endl;
DSK(&a,&b,&c,x0,h);
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
cout<<"k="<<k<<endl;
cout<<endl;
cout<<"--- DSK ---"<<endl;
cout<<"x="<<x<<endl;
cout<<"k="<<k1<<endl;
getch();
}
Соседние файлы в папке Метод Фиббоначчи