Inputkoef();
Inputotrez();
double x,xk,yk,k;
k=0;
xk=a+((3-pow(5,0.5))/2)*(b-a);
yk=a+b-xk;
Out.open("Solution.txt");
cout << "Solution of the golden section method:\n";
Out << "Solution of the golden section method:\n";
cout << "k\ta\tb\tXk\tYk\tF(Xk)\tF(Yk)\n";
Out << "k\ta\tb\tXk\tYk\tF(Xk)\tF(Yk)\n";
do
{
cout << k << "\t" << a << "\t" << b << "\t" << xk << "\t" << yk << "\t" << f(xk) << "\t" << f(yk) << endl;
Out << k << "\t" << a << "\t" << b << "\t" << xk << "\t" << yk << "\t" << f(xk) << "\t" << f(yk) << endl;
if(f(xk)<=f(yk))
{
b=yk;
yk=xk;
xk=a+b-xk;
}
else if(f(xk)>f(yk))
{
a=xk;
xk=yk;
yk=a+b-yk;
}
k++;
}
while (abs(b-a)>E);
cout << k << "\t" << a << "\t" << b << "\t" << xk << "\t" << yk << "\t" << f(xk) << "\t" << f(yk) << endl;
Out << k << "\t" << a << "\t" << b << "\t" << xk << "\t" << yk << "\t" << f(xk) << "\t" << f(yk) << endl;
x=(a+b)/2;
cout<< "x = " << x << "\tf(x) = " << f(x);
Out<< "x = " << x << "\tf(x) = " << f(x);
Out.close();
return 0;
}
Результат виконання програми при введенні даних, що відповідать завданню цієї курсової роботи:
· Метод Фібоначчі
// fib.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double PoslidovnistFib(double);
double g[5]={0};
double a,b;
double E=0;
void Inputkoef()
{
for (int i=0;i<5;i++)
{
cout<<"Enter a value a["<<i+1<<"] : ";
cin>>g[i];
}
}
void Inputotrez()
{
cout<<"Enter the lower limit segment a=";
cin>>a;
cout<<"Enter the upper limit of the interval b=";
cin>>b;
cout<<"enter the calculation accuracy E=";
cin>>E;
}
double f(double);
double f(double x)
{
return (g[0]*pow(x,4)+g[1]*pow(x,3)+g[2]*pow(x,2)+g[3]*x+g[4]);
};
ofstream Out;
int main(int argc, char* argv[])
{
cout<<"function has the form: a1*x^4+a2*x^3+a3*x^2+a4*x+a5->min"<<endl;
Inputkoef();
Inputotrez();
int d,k=0,p=0;
double eps,x,y,N=-1,kk=1.0;
eps = 0.00001 ;
Out.open("Solution.txt");
cout << "Solution of Fibonachi method:\n";
Out << "Solution of Fibonachi method:\n";
double L0=b-a;
do
{
N++;
}
while (PoslidovnistFib(N)<(abs(L0)/E));
cout << "N = " << N << endl;
Out << "N = " << N << endl;
cout << "k\ta\tb\tXk\tYk\tF(Xk)\tF(Yk)\n";
Out << "k\ta\tb\tXk\tYk\tF(Xk)\tF(Yk)\n";
x=a+(PoslidovnistFib(N-2)/PoslidovnistFib(N))*(b-a);
y=a+(PoslidovnistFib(N-1)/PoslidovnistFib(N))*(b-a);
cout << k << "\t" << a << "\t" << b << "\t" << x << "\t" << y << "\t" << f(x) << "\t" << f(y) << endl;
Out << k << "\t" << a << "\t" << b << "\t" << x << "\t" << y << "\t" << f(x) << "\t" << f(y) << endl;
do
{
if (f(x)<=f(y))
{
b=y;
y=x;
x=a+(PoslidovnistFib(N-k-3)/PoslidovnistFib(N-k-1))*(b-a);
}
else if (f(x)>f(y))
{
a=x;
x=y;
y=a+(PoslidovnistFib(N-k-2)/PoslidovnistFib(N-k-1))*(b-a);
};
k++;
cout << k << "\t" << a << "\t" << b << "\t" << x << "\t" << y << "\t" << f(x) << "\t" << f(y) << endl;
Out << k << "\t" << a << "\t" << b << "\t" << x << "\t" << y << "\t" << f(x) << "\t" << f(y) << endl;
}
while(k!=N-3);
k = 0;
cout << "\tFi(N) = " << PoslidovnistFib << endl;
Out << "\tFi(N) = " << PoslidovnistFib << endl;
do
{
cout << "k = " << k << "\tFi(N-k-1) = " << PoslidovnistFib(N-k-1) << "\tFi(N-k-2) = " << PoslidovnistFib(N-k-2) << "\tFi(N-k-3) = " << PoslidovnistFib(N-k-3) << endl;
Out << "k = " << k << "\tFi(N-k-1) = " << PoslidovnistFib(N-k-1) << "\tFi(N-k-2) = " << PoslidovnistFib(N-k-2) << "\tFi(N-k-3) = " << PoslidovnistFib(N-k-3) << endl;
k++;
}
while(k!=N-3);
y=x+eps;
if (f(x)<=f(y))
{
b=y;
}
if (f(x)>f(y))
{
a=x;
};
x = (b+a)/2;
cout << "x = " << x << "\tf(x) = " << f(x) <<endl;
Out << "x = " << x << "\tf(x) = " << f(x) <<endl;
Out.close();
return 0;
}
double PoslidovnistFib(double n)
{
double f0,fk,p;
f0=1;
fk=1;
for(int i=2;i<=n;i++)
{
p=fk;
fk=fk+f0;
f0=p;
}
if (n<2)
{
fk=1;
}
return fk;
};
Результат виконання рограми:
Solution of Fibonachi method:
N = 17
kabXkYkF(Xk)F(Yk)
0020.7639321.23607-50.8575-59.9845
10.76393221.236071.52786-59.9845-53.5866
20.7639321.527861.055731.23607-58.9696-59.9845
31.055731.527861.236071.34752-59.9845-58.8184
41.055731.347521.167181.23607-59.998-59.9845
51.055731.236071.124611.16718-59.7535-59.998
61.124611.236071.167181.1935-59.998-60.0537
71.167181.236071.19351.20975-60.0537-60.0508
81.167181.209751.183441.1935-60.0411-60.0537
91.183441.209751.19351.19969-60.0537-60.056
101.19351.209751.199691.20356-60.056-60.0553
111.19351.203561.197371.19969-60.0556-60.056
121.197371.203561.199691.20124-60.056-60.0559
131.197371.201241.198921.19969-60.0559-60.056
141.198921.201241.199691.20046-60.056-60.056
Fi(N) = 004112A8
k = 0Fi(N-k-1) = 1597Fi(N-k-2) = 987Fi(N-k-3) = 610
k = 1Fi(N-k-1) = 987Fi(N-k-2) = 610Fi(N-k-3) = 377
k = 2Fi(N-k-1) = 610Fi(N-k-2) = 377Fi(N-k-3) = 233
k = 3Fi(N-k-1) = 377Fi(N-k-2) = 233Fi(N-k-3) = 144
k = 4Fi(N-k-1) = 233Fi(N-k-2) = 144Fi(N-k-3) = 89
k = 5Fi(N-k-1) = 144Fi(N-k-2) = 89Fi(N-k-3) = 55
k = 6Fi(N-k-1) = 89Fi(N-k-2) = 55Fi(N-k-3) = 34
k = 7Fi(N-k-1) = 55Fi(N-k-2) = 34Fi(N-k-3) = 21
k = 8Fi(N-k-1) = 34Fi(N-k-2) = 21Fi(N-k-3) = 13
k = 9Fi(N-k-1) = 21Fi(N-k-2) = 13Fi(N-k-3) = 8
k = 10Fi(N-k-1) = 13Fi(N-k-2) = 8Fi(N-k-3) = 5
k = 11Fi(N-k-1) = 8Fi(N-k-2) = 5Fi(N-k-3) = 3
k = 12Fi(N-k-1) = 5Fi(N-k-2) = 3Fi(N-k-3) = 2
k = 13Fi(N-k-1) = 3Fi(N-k-2) = 2Fi(N-k-3) = 1
x = 1.20046f(x) = -60.056
1. Таха, Хемди А. Введение в исследование операций, 7-е издание.: Пер. с англ. – М.: Издательский дом «Вильямс», 2005. – 912с.: ил. – Парал. тит. англ.
2. Сухарев А.Г., Тимохов А.В., Федоров В.В. Курс методов оптимизации: учеб. пособие-2-ое изд.-М.:ФИЗМАТЛИТ, 2005
3. Н.И. Глебов, Ю.А. Кочетов, А.В. Плясунов. Методы оптимизации: учебное пособие. - Новосибирск: Новосибирский государственный университет, 2000. – 105 с.
4. Васильев Ф.П. Методы оптимизации. – М.: Издательство «Факториал Пресс», 2002. – 824 с.
5. Волков И.К., Загоруйко Е.А. Исследование операций-М.: МГТУ им. Н.Э. Баумана, 2000
6. Бейко И.В. и др. Методы и алгоритмы решения задач оптимизации. К: Высш.шк., 1983. – 513с
7. Ю. А. Кочетов, А.В. Плясунов. Методы оптимизации: учебное пособие. - Новосибирск: Новосибирский государственный университет, 2000. – 105 с.