0% found this document useful (0 votes)
23 views14 pages

Lab 2

This lab report from Dhaka International University details the implementation of the Secant and Halley's methods for finding roots of the equation f(x) = x^3 - x - 1. The report includes source code for both methods, demonstrating their respective algorithms and convergence checks. The submission is made by Ali Azaz Rafe for the Numerical Analysis course, under the supervision of Md. Mostafijur Rahman.

Uploaded by

aliazazrafe2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views14 pages

Lab 2

This lab report from Dhaka International University details the implementation of the Secant and Halley's methods for finding roots of the equation f(x) = x^3 - x - 1. The report includes source code for both methods, demonstrating their respective algorithms and convergence checks. The submission is made by Ali Azaz Rafe for the Numerical Analysis course, under the supervision of Md. Mostafijur Rahman.

Uploaded by

aliazazrafe2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Dhaka International University

Department of Computer Science &


Engineering
Lab Report

Course Title: Numerical Analysis


Course Code: CSE-303

Lab 2: Learning Secant method ,Halley’s Method .

SUBMITTED TO:
Md.Mostafijur Rahman
Lecturer
Department of CSE
Dhaka International University

SUBMITTED BY:
Name: Ali Azaz Rafe
Batch: 76th
Roll No : 49
Registration Code : CS-D-76-127609-CT

Date of submission: 23-10-24


Given equation: f(x) = x3-x-1
Source code :
#include <iostream>
#include <cmath>

using namespace std;

// Function to find the value of f(x) = x^3 - x - 1


double f(double x) {
return x * x * x - x - 1;
}

// Secant method implementation


double secant(double x0, double x1, double tol, int max_iter) {
double x2;
for (int i = 0; i < max_iter; ++i) {
// Calculate the next approximation using the secant formula
x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));

// Check for convergence


if (fabs(x2 - x1) < tol) {
cout << "Root found: " << x2 << endl;
return x2;
}
// Update variables for the next iteration
x0 = x1;
x1 = x2;
}

cout << "Maximum iterations reached. Approximate root: " << x2 << endl;
return x2;
}

int main() {
double x0 = 1.0; // Initial guess 1
double x1 = 2.0; // Initial guess 2
double tol = 1e-6; // Tolerance
int max_iter = 100; // Maximum iterations

secant(x0, x1, tol, max_iter);


return 0;
}
Output Result :
Given equation: f(x) = x3-x-1
Source code :
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

// Function to calculate f(x)


double f(double x) {
return pow(x, 3) - x - 1;
}

// Function to calculate f'(x)


double df(double x) {
return 3 * pow(x, 2) - 1;
}

// Function to calculate f''(x)


double d2f(double x) {
return 6 * x;
}

// Halley's method to find a root of f(x)


double halleysMethod(double initialGuess, double tolerance, int maxIterations) {
double x = initialGuess;

for (int i = 0; i < maxIterations; ++i) {


double f_x = f(x);
double df_x = df(x);
double d2f_x = d2f(x);

// Halley's iteration
double denominator = 2 * df_x * df_x - f_x * d2f_x;
if (denominator == 0) {
cout << "Denominator is zero, stopping iteration." << endl;
break;
}

double x_new = x - (2 * f_x * df_x) / denominator;

// Check for convergence


if (fabs(x_new - x) < tolerance) {
return x_new;
}

x = x_new;
}

cout << "Max iterations reached without convergence." << endl;


return x;
}

int main() {
double initialGuess = 1.0; // Initial guess
double tolerance = 1e-7; // Tolerance for convergence
int maxIterations = 100; // Maximum number of iterations

double root = halleysMethod(initialGuess, tolerance, maxIterations);

cout << "Root found: " << fixed << setprecision(7) << root << endl;

return 0;
}
Result output :

You might also like