Скачиваний:
13
Добавлен:
01.05.2014
Размер:
2.18 Кб
Скачать
#define X0 1
#define e  0.000001
#define H0 0.05
#define Ln 0.0001

#include<conio.h>
#include<stdio.h>
#include <math.h>
#include <iostream.h>

void Fibonacci2(double ,double );

double func(double x)
{
   double f;
//   f=10*x*x*x+3*x*x+x+5;
//   f=f*f;
f=2*x*x+3*exp(-x);
   return f;
}
///////////////////////////////////
double proizv(double x)
{
  return ((func(x+e)-func(x)) / e);
}
////////////////////////////////////
void Svenn(double* a,double* b)
{
  double h=H0,x1=X0,x2=x1+h;
  int i=0;
  if( func(x2) > func(x1) )
    {
      h=-h;
      x2=x1+h;
    }
  while( func(x1) > func(x2) )
    {
      x1=x2;
      h=2*h;
      x2=x1+h;
      i++;
    }
  *b=(x1-h/2>x2)?x1-h/2:x2;
  *a=(x1-h/2>x2)?x2:x1-h/2;
  cout<<"\n\n\n\n";
//  printf("\n\n  x1= %f ,x2 = %f \n",x1,x2);
//  printf("  a= %f ,b = %f ,i = %d",x1-h/2,x2,i);
}
//////////////////////////////////////////
void Bolcano(double a, double b)
{
  int j=0;
  double x;

  x=(a+b)/2;
  while(j!=10 && ((fabs(proizv(x)) >= e) && (fabs(b-a) >= e)) )
    {
      (proizv(x) > 0) ? b=x : a=x;
      j++;
      x=(a+b)/2;
    }
  cout<<"\n\n\n >>>>>>>>Bolcano: \n";
  cout<<"   X*= "<< x <<" , number of iterations J ="<< j;
  Fibonacci2(a,b);
}
///////////////////////////////////
void Fibonacci2(double a, double b)
{
  int k=0,n=0;
  double f0,f1,x,x1,x2;

 f0=f1=1;
 while( f1 < fabs(b-a)/Ln )
    {
       x=f0+f1;
       f0=f1;
       f1=x;
       n++;
    }
 cout<<endl;
 cout<<"\n\n\n>>>>>>> Metod Fibonacci 2";
 cout<<"\n Fibonacci number Fn = "<<f1<<" ,  Ln = "<<Ln<<" ,  n = "<<n;
 do{
     k++;
     x=f1-f0;
     f1=f0;
     f0=x;
     x1 = a +(f0/f1)*(b - a);
     x2 = a + b - x1;
     if(x1 < x2)
       (func(x1) < func(x2)) ? b=x2 : a=x1;
     else
       (func(x1) < func(x2)) ? a=x2 : b=x1;
   }while(k!=n-1);
   cout<<"\n Minimum of function = "<<(a+b)/2;
   cout<<"\n Number of iteration k = "<<k;
}
/////////////////////////////////
void main(void)
{
  double a,b;
  clrscr();
  Svenn(&a,&b);
  printf("\nSven a= %f ,b = %f ",a,b);
  Bolcano(a,b);
 // Fibonacci2(a,b);
  getch();
}
Соседние файлы в папке all