Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- //---------------------------------------------------------------------------
- #pragma argsused
- #include<iostream.h>
- #include<conio.h>
- #include<math.h>
- int iter, funct;
- double m_x;
- double func_y(double x)
- {
- return pow(M_E,x)-(x*x*x)/3+2*x;
- }
- double general_search(double a, double b, double e)
- {
- iter=0;
- funct=0;
- int n=2/e+1;
- double h=fabs(a-b)/n;
- iter++;
- double m_y, temp_x, temp_y;
- m_x=a;
- m_y=func_y(m_x);
- funct++;
- temp_x=m_x;
- while(temp_x<b)
- {
- temp_x+=h;
- temp_y=func_y(temp_x);
- funct++;
- if(temp_y<m_y)
- {
- m_y=temp_y;
- m_x=temp_x;
- }
- }
- return m_y;
- }
- double interval_bisection(double a,double b,double e)
- {
- iter=0;
- funct=0;
- double x1, x2, x3, x4, x5;
- double y1, y2, y3, y4, y5;
- double m_y;
- x1 = a; x5 = b;
- x3 = x1 + (x5 - x1)/2.0;
- y3 = func_y(x3);
- funct++;
- while (fabs(x5 - x1) > e)
- {
- iter++;
- x2 = x1 + (x3 - x1)/2.0;
- x4 = x5 - (x5 - x3)/2.0;
- y2 = func_y(x2);
- y4 = func_y(x4);
- funct += 2;
- if (y2<y3)
- {
- x5 = x3;
- y5 = y3;
- x3=x2;
- y3=y2;
- m_x = x2;
- m_y = y2;
- continue;
- }
- if (y4<y3)
- {
- x1 = x3;
- y1 = y3;
- x3=x4;
- y3=y4;
- m_x = x4;
- m_y = y4;
- continue;
- }
- else
- {
- x1 = x2;
- y1 = y2;
- x5=x4;
- y5=y4;
- m_x = x3;
- m_y = y3;
- continue;
- }
- }
- return m_y;
- }
- double gold_section(double a, double b, double e)
- {
- double f = (sqrt(5) + 1)/2;
- double x1 = a, x4 = b, x2, x3;
- double y1 =func_y(x1), y4 = func_y(x4), y2, y3;
- double gold, m_y;
- funct = 2;
- iter = 0;
- gold = (x4 - x1)/f;
- x2 = x4 - gold;
- x3 = x1 + gold;
- y2 = func_y(x2);
- y3 = func_y(x3);
- funct+=2;
- while (fabs(x4 - x1) > e)
- {
- iter++;
- if (y2 < y3)
- {
- m_x = x2; m_y = y2;
- x4 = x3; y4 = y3;
- x3 = x2; y3 = y2;
- gold =(x4 - x1)/f;
- x2 = x4 - gold;
- y2 = func_y(x2);
- funct++;
- continue;
- }
- if (y3 < y2)
- {
- m_x = x3; m_y = y3;
- x1 = x2; y1 = y2;
- x2 = x3; y2 = y3;
- gold =(x4 - x1)/f;
- x3 = x1 + gold;
- y3 = func_y(x3);
- funct++;
- continue;
- }
- }
- return m_y;
- }
- void main()
- {
- cout.precision(10);
- cout<<"General search:\n\nf(x*)="<<general_search(-2,0,0.00001)<<endl;
- cout<<"x*="<<m_x<<endl;
- cout<<"Function usage counter: "<<funct<<endl;
- cout<<"Iteration counter: "<<iter<<endl;
- cout<<"\nInterval Bisection:\n\nf(x*)="<<interval_bisection(-2,0,0.00001)<<endl;
- cout<<"x*="<<m_x<<endl;
- cout<<"Function usage counter: "<<funct<<endl;
- cout<<"Iteration counter: "<<iter<<endl;
- cout<<"\nGold Section:\n\nf(x*)="<<gold_section(-2,0,0.00001)<<endl;
- cout<<"x*="<<m_x<<endl;
- cout<<"Function usage counter: "<<funct<<endl;
- cout<<"Iteration counter: "<<iter<<endl;
- getch();
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement