Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- double e=0.01;
- double a = 0;
- double b = 2;
- double f(double x)
- {
- return (sqrt(pow(x,2)+3)/(x+5));
- }
- double razrad()
- {
- double d,x,y;
- d=(b-a)/4;
- double x0=a;
- double f0=f(x0);
- label:
- double x1=x0+d;
- double f1=f(x1);
- if (f0>f1)
- {
- x0=x1;
- f0=f1;
- if(a<=x0 && x0<=b)
- goto label;
- }
- if (fabs(d)<=e)
- {
- x=x0;
- y=f(x0);
- }
- else
- {
- x0=x1;
- f0=f1;
- d=-d/4;
- goto label;
- }
- cout <<"rmin=" << x << endl;
- cout <<"rfmin=" << y << endl;
- return 0;
- }
- double dihot()
- {
- double s=0.001;
- double x1,x2,en,x,y;
- do
- {
- x1=(b+a-s)/2;
- x2=(b+a+s)/2;
- if(f(x1)<=f(x2)) b=x2;
- else a=x1;
- en=(b-a)/2;
- }
- while(en>e);
- x=(a+b)/2;
- y=f(x);
- cout << endl;
- cout << "dmin = " << x << endl;
- cout << "dfmin = " << y << endl;
- return 0;
- }
- double gold()
- {
- double x1,x2,f1,f2,t,l,en,x,y;
- t=(sqrt(5)-1)/2;
- l=b-a;
- x1 = b - t*l;
- x2 = a + t*l;
- f1 = f(x1);
- f2 = f(x2);
- while(l>2*e)
- {
- if(f1<=f2)
- {
- b = x2;
- l=b-a;
- x2=x1;
- f2=f1;
- x1=b-t*l;
- f1=f(x1);
- }
- else
- {
- a=x1;
- l=b-a;
- x1=x2;
- f1=f2;
- x2=a+t*l;
- f2=f(x2);
- }
- }
- x=(a+b)/2;
- y=f(x);
- cout << endl;
- cout << "gmin = " << x << endl;
- cout << "gfmin = " << y << endl;
- return 0;
- }
- double parabol()
- {
- double x1, x2, x3, f1, f2, f3, a1, a2, x, fx, xp;
- x1=(3-sqrt(5))*(b-a)/2;
- x3=(sqrt(5)-1)*(b-a)/2;
- x2=(x1+x3)/2;
- x=0;
- f1=f(x1);
- f2=f(x2);
- f3=f(x3);
- do
- {
- xp=x;
- a1=(f2-f1)/(x2-x1);
- a2=(1/(x3-x2))*( (f3-f1)/(x3-x1) - (f2-f1)/(x2-x1) );
- x=0.5*(x1+x2-(a1/a2));
- fx=f(x);
- if(x1<x<x2<x3)
- if(fx>=f2)
- {
- x1=x;
- f1=f(x1);
- }
- else
- {
- x3=x2;
- x2=x;
- f2=f(x2);
- f3=f(x3);
- }
- else if(x1<x2<x<x3)
- if(fx>=f2)
- {
- x3=x;
- f3=f(x3);
- }
- else
- {
- x1=x2;
- x2=x;
- f1=f(x1);
- f2=f(x2);
- }
- }
- while(abs(xp-x)>e);
- cout << endl;
- cout << "pmin = " << x << endl;
- cout << "pfmin = " << fx << endl;
- return 0;
- }
- int main()
- {
- razrad();
- dihot();
- gold();
- parabol();
- system ("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement