Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath> //sinus
- using namespace std;
- double f(double x) {
- return (x / (1.2 - sin(2 * x)));
- }
- double concave(double x1, double a, double b) { //1.21
- return ((-2)*(f(a)*(b-x1)+f(x1)*(a-b)+f(b)*(x1-a))/((b-x1)*(a-b)*(x1-a)));
- }
- double xTwo(double x1, double a, double b) { //1.22
- return ((f(a)*((b*b)-(x1*x1))
- +f(x1)*((a*a)-(b*b))
- +f(b)*((x1*x1)-(a*a)))/
- 2*(f(a)*(b-x1)
- +f(x1)*(a-b)
- +f(b)*(x1-a))
- );
- }
- int main() {
- int expansion_factor = 4, i=1;
- double accuracy = 0.000001;
- double x1[100], x2[100], a[100], b[100], shift1, shift2;
- x1[0] = 0.22; //initial point > 0
- cout << f(0) << " < " << f(x1[0]) << "\n\n"; //so
- a[1] = (-1)*expansion_factor*x1[0]; x1[1] = 0; b[1]=x1[0];
- cout << "i\ta\tx1\tx2\tb\n";
- do {
- while (concave(x1[i], a[i], b[i]) <= 0) { //1.21, 1.24
- cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
- cout << "\tconcave\n";
- a[i] = x1[i];
- x1[i] = b[i];
- b[i] = b[i] + expansion_factor*(b[i]-x1[i]);
- }
- x2[i] = xTwo(x1[i], a[i], b[i]);
- if (x2[i] < x1[i]) {
- cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
- cout << "\tx2<x1\n";
- shift1 = x2[i];
- x2[i] = x1[i];
- x1[i] = shift1;
- }
- if (x2[i] < a[i] || x2[i] > b[i]) { //1.25
- cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
- cout << "\tout of interval\n";
- shift1 = a[i];
- shift2 = x1[i];
- a[i] = x2[i];
- x1[i] = shift1;
- b[i] = shift2;
- };
- cout << i << "\t" << a[i] << "\t" << x1[i] << "\t" << x2[i] << "\t" << b[i];
- if (f(x1[i])>=f(x2[i])) { //1.11 - 1.13
- cout << "\ta deleted\n";
- a[i+1] = x1[i];
- b[i+1] = b[i];
- x1[i+1] = x2[i];
- }
- else { //1.15 - 1.18
- cout << "\tb deleted\n";
- a[i+1] = a[i];
- b[i+1] = x2[i];
- x1[i+1] = x1[i];
- }
- i++;
- } while (i<90);//x2[i]-x1[i] > accuracy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement