Write a C++ function that will find aproximate minimum of the function "F(x)" based of 2…
- User
Write a C++ function that will find aproximate minimum of the function "F(x)" based of 2 points on binare search "(a+b)/2"
- Assistant
- User
Write a C++ function that will find aproximate minimum of the function "F(x)" based of 2 points using binare search "(a+b)/2"
- Assistant
- User
make this code work better and change all "float"s to "double"s in this c++ code: "#include #include "vector" using namespace std; typedef vector VFloat;
float F(float x) { return sin(x); }
float Fi(float x, float h) { float fl, fr, fc, f2; fl = (F(x) - F(x - h)) / h; fr = (F(x + h) - F(x)) / h; fc = (F(x + h) - F(x - h)) / (2 * h); f2 = (F(x + h) - 2 * F(x) + F(x - h)) / (h * h); return f2; }
VFloat Dots(float h) { float n = 1; VFloat arr; float a = 0.0; float b = a + n * h; arr.push_back(a); arr.push_back(b); if (F(b)<F(a)) { n = 1; } else { n = -1; } while (true) { a = b; b = a + n * h; arr.push_back(b); if (F(b) < F(a) && n==1) { break; } else if (F(b) > F(a) && n == -1) { break; } } VFloat output; output.push_back(arr.back()); arr.pop_back(); output.push_back(arr.back()); return output; }
float D(float a, float b, float E, float h) { float af, bf; af = a; bf = b; while (true) { float x = (af + bf) / 2; std::cout << x << ' ' << abs(Fi(x, h)) << '\n'; if (abs(Fi(x, h))< E) { return x; } else if (Fi(x, h) * Fi(af, h) > 0) { af = x; } else if (Fi(x, h) * Fi(bf, h) > 0) { bf = x; } } }
- Assistant
- User
why does my code always loops and prints: "-1.5708 1.0002 -1.5708 -1.5708 1.0004 1.0004" in the console? my code: "#include #include "vector" using namespace std; typedef vector VDouble;
double F(double x) { return sin(x); }
double Fi(double x, double h) { double fl, fr, fc, f2; fl = (F(x) - F(x - h)) / h; fr = (F(x + h) - F(x)) / h; fc = (F(x + h) - F(x - h)) / (2 * h); f2 = (F(x + h) - 2 * F(x) + F(x - h)) / (h * h); return f2; }
VDouble Dots(double h) { double n = 1.0; VDouble arr; double a = 0.0; double b = a + n * h; arr.push_back(a); arr.push_back(b); if (F(b)<F(a)) { n = 1; } else { n = -1; } while (true) { a = b; b = a + n * h; arr.push_back(b); if (F(b) < F(a) && n==1) { break; } else if (F(b) > F(a) && n == -1) { break; } } VDouble output; output.push_back(arr.back()); arr.pop_back(); output.push_back(arr.back()); return output; }
- Assistant
- User
why can code in c++ wont make doubles below 4digits after point
- Assistant
- User
Code loops itself: "#include #include "vector" #include using namespace std; typedef vector VDouble;
double F(double x) { return sin(x); }
double Fi(double x, double h) { double fl, fr, fc, f2; fl = (F(x) - F(x - h)) / h; fr = (F(x + h) - F(x)) / h; fc = (F(x + h) - F(x - h)) / (2 * h); f2 = (F(x + h) - 2 * F(x) + F(x - h)) / (h * h); return f2; }
VDouble Dots(double h) { double n = 1.0; VDouble arr; double a = 0.0; double b = a + n * h; arr.push_back(a); arr.push_back(b); if (F(b)<F(a)) { n = 1; } else { n = -1; } while (true) { a = b; b = a + n * h; arr.push_back(b); if (F(b) < F(a) && n==1) { break; } else if (F(b) > F(a) && n == -1) { break; } } VDouble output; output.push_back(arr.back()); arr.pop_back(); output.push_back(arr.back()); return output; }
- Assistant