Скачиваний:
13
Добавлен:
01.05.2014
Размер:
1.33 Кб
Скачать
#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <math.h>

float otv(float x)
{
 float f;
 f=4*x*x*x*x-14*x*x*x+60*x*x-70*x;
 return f;
}

void Swann(float &x1,float &x2)
{
 float h,f1,f2;
 h=0.001*x1;
 x2=x1+h;
 f1=otv(x1);
 f2=otv(x2);
 if (f1<f2)
 {
  x2=x1-h;
  f2=otv(x2);
  while (f2<f1)
  {
//   h*=2;
   x1=x2;
   x2-=h;
   f1=otv(x1);
   f2=otv(x2);
  }
  cout<<"Ќ з «м­л© Ё­вҐаў «: ["<<x2<<";"<<x1<<"]";
 }
 else
 {
  while (f1>f2)
  {
//   h*=2;
   x1=x2;
   x2+=h;
   f1=otv(x1);
   f2=otv(x2);
  }
  cout<<"Ќ з «м­л© Ё­вҐаў «: ["<<x1<<";"<<x2<<"]";
 }
 if(x1>x2)
 {
  h=x1;
  x1=x2;
  x2=h;
 }
}

void main()
{
 clrscr();
 float c,e,x1=2,x2=0,a,b,l1,l2=0.00000001,f1,f2,F1,F2,x;
 int n=2;
 Swann(x1,x2);
 a=x1;
 b=x2;
 l1=abs(a-b);
 f1=1;
 f2=1;
 while(f2<l1/l2)
 {
  c=f2;
  f2+=f1;
  f1=c;
  n++;
 }
// e=l1/(f2+f1);
 e=0.00001;
 if (n%2==0) x1=a+f1*l1/f2+e/f2;
 else x1=a+f1*l1/f2-e/f2;
 for (int k=0;k<=n;k++)
 {
  x2=a+b-x1;
  F1=otv(x1);
  F2=otv(x2);
  if((x1<x2) & (F1<F2)) b=x2;
  else if((x1>x2) & (F1<F2))
       {
	b=x1;
	x1=x2;
       }
       else if((x1<x2) & (F1>F2))
	    {
	     a=x1;
	     x1=x2;
	    }
	    else if((x1>x2) & (F1>F2)) a=x2;
 }
 x=x1;
 cout<<endl<<x;
 getch();
}
Соседние файлы в папке all