Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- double func(double x, double y) {
- return 6*x*x - x*y + 12*y + y*y - 8*x;
- }
- double funcdx(double x, double y) {
- return -y + 12*x - 8;
- }
- double funcdy(double x, double y) {
- return 2*y - x + 12;
- }
- void gradient(double x0, double y0, double h0, unsigned n) {
- double x = x0, y = y0, h = h0;
- double xn, yn;
- cout << 0 << "\t" << x << "\t" << y << "\t" << func(x, y) << "\t" << funcdx(x, y) << "\t" << funcdy(x, y) << "\t" << endl;
- for (unsigned i = 1; i <= n; i++) {
- xn = x - h*funcdx(x, y);
- yn = y - h*funcdy(x, y);
- if (func(xn, yn) >= func(x, y)) {
- h = h/2;
- } else {
- x = xn;
- y = yn;
- h = h*2;
- }
- cout << i << "\t" << x << "\t" << y << "\t" << func(x, y) << "\t" << funcdx(x, y) << "\t" << funcdy(x, y) << "\t" << endl;
- }
- }
- int main() {
- cout << setprecision(5) << fixed;
- const double x0 = 0;
- const double y0 = 0;
- const double h = 0.25;
- const unsigned num_it = 1;
- gradient(x0, y0, h, num_it);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement