iamHrithikRaj / Numerical-Algorithm

In numerical analysis, a numerical method is a mathematical tool designed to solve numerical problems. The implementation of a numerical method with an appropriate convergence check in a programming language is called a numerical algorithm
GNU General Public License v3.0
10 stars 1 forks source link

Update Newton-Raphson.cpp #3

Open KunjShah95 opened 3 months ago

KunjShah95 commented 3 months ago

include

include

// Function to compute f(x) = x^2 - 2 double f(double x) { return x x x + x - 1 ; }

// Function to compute f'(x) = 2x double f_prime(double x) { return 3* x + 1 ; }

int main() { // Initial values double x0 = 1.0; // initial guess double epsilon = 1e-6; // tolerance int maxIter = 100; // maximum number of iterations

// Initialize variables
double x = x0;
double fx, f_prime_x, x_next;
int iter = 0;

// Newton Raphson method
while (iter < maxIter) {
    // Compute f(x) and f'(x)
    fx = f(x);
    f_prime_x = f_prime(x);

    // Compute x_next
    x_next = x - fx / f_prime_x;

    // Print iteration details
    std::cout << "Iteration " << iter << ": x = " << x << ", f(x) = " << fx << ", f'(x) = " << f_prime_x << ", x_next = " << x_next << std::endl;

    // Check for convergence
    if (std::abs(x_next - x) < epsilon) {
        std::cout << "Converged! The root is approximately " << x_next << std::endl;
        break;
    }

    // Update x
    x = x_next;
    iter++;
}

return 0;

}