Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- struct Vector {
- double x, y, z;
- Vector() {x = y = z = 0.;}
- Vector(double a, double b, double c) : x(a), y(b), z(c) {}
- Vector operator * (const double k) {
- Vector c(x * k, y * k, z * k);
- return c;
- }
- Vector operator - (const Vector &a) {
- Vector c(x - a.x, y - a.y, z - a.z);
- return c;
- }
- };
- double f(double x, double y, double z) { // u(x, y, z)
- double a = 4 * x - 5 * pow(y, 2) + z - 3.75;
- double b = 3 * pow(x, 2) + 2 * y - 2 * z - 3;
- double c = x + y + 2 * pow(z, 2) - 3.5;
- return pow(a, 2) + pow(b, 2) + pow(c, 2);
- }
- Vector df(double f(double x, double y, double z), Vector a) {
- const double d = 1e-10;
- Vector grad;
- grad.x = (f(a.x + d, a.y, a.z) - f(a.x, a.y, a.z)) / d;
- grad.y = (f(a.x, a.y + d, a.z) - f(a.x, a.y, a.z)) / d;
- grad.z = (f(a.x, a.y, a.z + d) - f(a.x, a.y, a.z)) / d;
- return grad;
- }
- double kNorma(Vector a) {
- return sqrt(pow(a.x, 2)) + (pow(a.y, 2)) + (pow(a.z, 2));
- }
- void solve(double u(double x, double y, double z), double EPS, double L) {
- Vector prevX, x;
- int i = 0;
- double d;
- do {
- prevX = x;
- d = kNorma(df(u, x));
- x = prevX - df(u, prevX) * L;
- cout << "Iteration :: " << i << "\t >>\tx = {" << x.x << ", " << x.y << ", " << x.z << "}";
- cout << "\t >>\td = " << d;
- if (d < EPS) cout << " < EPS";
- cout << endl;
- i++;
- } while (d >= EPS);
- return;
- }
- int main() {
- solve(f, 1e-4, 1e-3);
- return 0;
- }
Add Comment
Please, Sign In to add comment