Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Newton-Raphson.cpp #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KunjShah95
Copy link

#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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant