0% found this document useful (0 votes)
39 views

Bisection Method C Program

The program defines a function to calculate the given equation, implements the bisection method to find the root, initializes values, calls the bisection method and prints the result. It is modified to include iteration number, take coefficients as input, and display error percentage at each iteration.

Uploaded by

akib1875
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Bisection Method C Program

The program defines a function to calculate the given equation, implements the bisection method to find the root, initializes values, calls the bisection method and prints the result. It is modified to include iteration number, take coefficients as input, and display error percentage at each iteration.

Uploaded by

akib1875
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Bisection Method C Program

write c program for numerical solution in bisection method for the equation f(x) = x^3 -x -1

Certainly! Here's a simple C program for the numerical solution of the given equation using the bisection
method:

#include <stdio.h>
#include <math.h>

// Function to calculate f(x) for the given equation


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

// Bisection method implementation


double bisection(double a, double b, double tol) {
double c;

while ((b - a) >= tol) {


// Find the midpoint
c = (a + b) / 2;

// Check if the root is found


if (func(c) == 0.0)
break;

// Decide the side to repeat the search


if (func(c) * func(a) < 0)
b = c;
else
a = c;
}

return c;
}

int main() {
double a, b, tol, root;

// Initial guesses and tolerance


a = 1.0;
b = 2.0;
tol = 0.0001;

// Find the root using the bisection method


root = bisection(a, b, tol);

// Display the result


printf("The root of the equation is: %lf\n", root);

return 0;
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/10
This program defines the function `func` representing the given equation, and then implements the
bisection method in the `bisection` function. The main function initializes the initial guesses and
tolerance, calls the bisection method, and prints the result.

write c program for numerical solution in bisection method for the equation f(x) = ax^3 +bx +c

Certainly! Here's a C program for the numerical solution of the cubic equation f (x) = ax3 + bx + c using
the bisection method:

#include <stdio.h>
#include <math.h>

// Function to calculate f(x) for the cubic equation


double func(double a, double b, double c, double x) {
return a * x * x * x + b * x + c;
}

// Bisection method implementation


double bisection(double a, double b, double c, double left, double right, double tol) {
double mid;

while ((right - left) >= tol) {


// Find the midpoint
mid = (left + right) / 2;

// Check if the root is found


if (func(a, b, c, mid) == 0.0)
break;

// Decide the side to repeat the search


if (func(a, b, c, mid) * func(a, b, c, left) < 0)
right = mid;
else
left = mid;
}

return mid;
}

int main() {
double a, b, c, left, right, tol, root;

// Coefficients of the cubic equation


a = 1.0;
b = 0.0;
c = -1.0;

// Initial interval [left, right] and tolerance


left = -2.0;
right = 2.0;
tol = 0.0001;

// Find the root using the bisection method


root = bisection(a, b, c, left, right, tol);

// Display the result


printf("The root of the equation is: %lf\n", root);

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/10
return 0;
}

This program defines the function `func` representing the cubic equation, and then implements the
bisection method in the `bisection` function. The main function initializes the coefficients and the initial
interval, calls the bisection method, and prints the result.

also include iteration number

Certainly! Here's the modified C program that includes the iteration number:

#include <stdio.h>
#include <math.h>

// Function to calculate f(x) for the cubic equation


double func(double a, double b, double c, double x) {
return a * x * x * x + b * x + c;
}

// Bisection method implementation


double bisection(double a, double b, double c, double left, double right, double tol, int
*iterations) {
double mid;

while ((right - left) >= tol) {


// Find the midpoint
mid = (left + right) / 2;

// Check if the root is found


if (func(a, b, c, mid) == 0.0)
break;

// Decide the side to repeat the search


if (func(a, b, c, mid) * func(a, b, c, left) < 0)
right = mid;
else
left = mid;

// Increment the iteration count


(*iterations)++;
}

return mid;
}

int main() {
double a, b, c, left, right, tol, root;
int iterations = 0;

// Coefficients of the cubic equation


a = 1.0;
b = 0.0;
c = -1.0;

// Initial interval [left, right] and tolerance


left = -2.0;

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/10
right = 2.0;
tol = 0.0001;

// Find the root using the bisection method


root = bisection(a, b, c, left, right, tol, &iterations);

// Display the result and iteration count


printf("The root of the equation is: %lf\n", root);
printf("Number of iterations: %d\n", iterations);

return 0;
}

This version of the program includes an additional parameter in the `bisection` function to keep track
of the number of iterations. The `iterations` variable is then incremented inside the loop, and the final
count is displayed in the `main` function.

i want to get a,b , c as input

Certainly! You can modify the program to take the coefficients `a`, `b`, and `c` as input from the user.
Here's the modified program:

#include <stdio.h>
#include <math.h>

// Function to calculate f(x) for the cubic equation


double func(double a, double b, double c, double x) {
return a * x * x * x + b * x + c;
}

// Bisection method implementation


double bisection(double a, double b, double c, double left, double right, double tol, int
*iterations) {
double mid;

while ((right - left) >= tol) {


// Find the midpoint
mid = (left + right) / 2;

// Check if the root is found


if (func(a, b, c, mid) == 0.0)
break;

// Decide the side to repeat the search


if (func(a, b, c, mid) * func(a, b, c, left) < 0)
right = mid;
else
left = mid;

// Increment the iteration count


(*iterations)++;
}

return mid;
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/10
int main() {
double a, b, c, left, right, tol, root;
int iterations = 0;

// Get coefficients from user input


printf("Enter coefficient a: ");
scanf("%lf", &a);
printf("Enter coefficient b: ");
scanf("%lf", &b);
printf("Enter coefficient c: ");
scanf("%lf", &c);

// Initial interval [left, right] and tolerance


left = -2.0;
right = 2.0;
tol = 0.0001;

// Find the root using the bisection method


root = bisection(a, b, c, left, right, tol, &iterations);

// Display the result and iteration count


printf("The root of the equation is: %lf\n", root);
printf("Number of iterations: %d\n", iterations);

return 0;
}

This version of the program uses `scanf` to get the coefficients `a`, `b`, and `c` as input from the user
before proceeding with the bisection method.

Also display the error percentage of each successive iterations

Certainly! You can calculate and display the error percentage of each successive iteration. Here's the
modified program:

#include <stdio.h>
#include <math.h>

// Function to calculate f(x) for the cubic equation


double func(double a, double b, double c, double x) {
return a * x * x * x + b * x + c;
}

// Bisection method implementation


double bisection(double a, double b, double c, double left, double right, double tol, int
*iterations) {
double mid, prevMid = 0.0, errorPercentage;

while ((right - left) >= tol) {


// Find the midpoint
mid = (left + right) / 2;

// Check if the root is found


if (func(a, b, c, mid) == 0.0)
break;

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/10
// Decide the side to repeat the search
if (func(a, b, c, mid) * func(a, b, c, left) < 0)
right = mid;
else
left = mid;

// Calculate error percentage


if (*iterations > 0) {
errorPercentage = fabs((mid - prevMid) / mid) * 100.0;
printf("Iteration %d - Root: %lf, Error: %.6lf%%\n", *iterations, mid,
errorPercentage);
}

// Update previous midpoint


prevMid = mid;

// Increment the iteration count


(*iterations)++;
}

return mid;
}

int main() {
double a, b, c, left, right, tol, root;
int iterations = 0;

// Get coefficients from user input


printf("Enter coefficient a: ");
scanf("%lf", &a);
printf("Enter coefficient b: ");
scanf("%lf", &b);
printf("Enter coefficient c: ");
scanf("%lf", &c);

// Initial interval [left, right] and tolerance


left = -2.0;
right = 2.0;
tol = 0.0001;

// Find the root using the bisection method


root = bisection(a, b, c, left, right, tol, &iterations);

// Display the final result and iteration count


printf("The root of the equation is: %lf\n", root);
printf("Number of iterations: %d\n", iterations);

return 0;
}

In this version, the `errorPercentage` is calculated and displayed for each successive iteration after the
first iteration. The previous midpoint (`prevMid`) is used to calculate the error percentage.

write c program for solving the equation, f(x) = x^3 +4x^2 -10 using fixed point iteration method.
Also show iteration number along with error percentage for each iteration and display them in a
table.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/10
Certainly! Here's a C program for solving the equation f (x) = x3 + 4x2 − 10 using the fixed-point
iteration method and displaying the iteration number along with the error percentage for each iteration:

#include <stdio.h>
#include <math.h>

// Function representing the given equation


double func(double x) {
return x * x * x + 4 * x * x - 10;
}

// Derivative of the function for the fixed-point iteration method


double derivative(double x) {
return 3 * x * x + 8 * x;
}

// Fixed-point iteration method implementation


double fixedPointIteration(double initialGuess, double tol, int maxIterations) {
double x = initialGuess;
int iterations = 0;
double errorPercentage;

printf("| Iteration | x Value | Error Percentage |\n");


printf("|------------|--------------|-------------------|\n");

do {
double xNext = x - func(x) / derivative(x);

// Calculate error percentage


if (iterations > 0) {
errorPercentage = fabs((xNext - x) / xNext) * 100.0;
printf("| %-6d | %-12lf | %-17lf%% |\n", iterations, xNext, errorPercentage);
}

x = xNext;
iterations++;

// Check for convergence


if (iterations >= maxIterations) {
printf("Maximum number of iterations reached. The method may not have
converged.\n");
break;
}
} while (fabs(func(x)) >= tol);

return x;
}

int main() {
double initialGuess, tol, root;
int maxIterations;

// Get user input


printf("Enter initial guess: ");
scanf("%lf", &initialGuess);

printf("Enter tolerance: ");


scanf("%lf", &tol);

printf("Enter maximum number of iterations: ");


scanf("%d", &maxIterations);

// Find the root using fixed-point iteration


root = fixedPointIteration(initialGuess, tol, maxIterations);

// Display the final result


printf("\nThe root of the equation is: %lf\n", root);

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/10
return 0;
}

This program defines the function `func` representing the given equation, as well as the derivative
function needed for the fixed-point iteration method. The `fixedPointIteration` function implements
the fixed-point iteration method, displaying the iteration number and error percentage for each
iteration in a table format. The user is prompted to enter the initial guess, tolerance, and maximum
number of iterations.

write c program for solving the equation, f(x) = x^3 +4x^2 -10 using fixed point iteration method.
Also show iteration number along with error percentage for each iteration

Certainly! Here's a C program for solving the equation f (x) = x3 + 4x2 − 10 using the fixed-point
iteration method and displaying the iteration number along with the error percentage for each iteration:

#include <stdio.h>
#include <math.h>

// Function representing the given equation


double func(double x) {
return x * x * x + 4 * x * x - 10;
}

// Fixed-point iteration method implementation


double fixedPointIteration(double initialGuess, double tol, int maxIterations) {
double x = initialGuess;
int iterations = 0;
double errorPercentage;

printf("| Iteration | x Value | Error Percentage |\n");


printf("|------------|--------------|-------------------|\n");

do {
double xNext = sqrt(10 / (x + 4));

// Calculate error percentage


if (iterations > 0) {
errorPercentage = fabs((xNext - x) / xNext) * 100.0;
printf("| %-6d | %-12lf | %-17lf%% |\n", iterations, xNext, errorPercentage);
}

x = xNext;
iterations++;

// Check for convergence


if (iterations >= maxIterations) {
printf("Maximum number of iterations reached. The method may not have
converged.\n");
break;
}
} while (fabs(func(x)) >= tol);

return x;
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/10
int main() {
double initialGuess, tol, root;
int maxIterations;

// Get user input


printf("Enter initial guess: ");
scanf("%lf", &initialGuess);

printf("Enter tolerance: ");


scanf("%lf", &tol);

printf("Enter maximum number of iterations: ");


scanf("%d", &maxIterations);

// Find the root using fixed-point iteration


root = fixedPointIteration(initialGuess, tol, maxIterations);

// Display the final result


printf("\nThe root of the equation is: %lf\n", root);

return 0;
}

In this program, the fixed-point iteration method is implemented using the iteration formula xn+1 ​ =
10
xn +4

. The iteration number and error percentage for each iteration are displayed in a table format.
​ ​

The user is prompted to enter the initial guess, tolerance, and maximum number of iterations.

i dont want to input the max number of iteration. modify the code so that it adjusts the maximum
number of iteration automatically

Certainly! You can modify the program to adjust the maximum number of iterations automatically based
on the error tolerance. Here's the updated code:

#include <stdio.h>
#include <math.h>

// Function representing the given equation


double func(double x) {
return x * x * x + 4 * x * x - 10;
}

// Fixed-point iteration method implementation


double fixedPointIteration(double initialGuess, double tol) {
double x = initialGuess;
int iterations = 0;
double errorPercentage;

printf("| Iteration | x Value | Error Percentage |\n");


printf("|------------|--------------|-------------------|\n");

do {
double xNext = sqrt(10 / (x + 4));

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/10
// Calculate error percentage
if (iterations > 0) {
errorPercentage = fabs((xNext - x) / xNext) * 100.0;
printf("| %-6d | %-12lf | %-17lf%% |\n", iterations, xNext, errorPercentage);
}

x = xNext;
iterations++;

// Check for convergence (stop if error is below tolerance)


if (fabs(func(x)) < tol) {
break;
}
} while (errorPercentage >= tol);

return x;
}

int main() {
double initialGuess, tol, root;

// Get user input


printf("Enter initial guess: ");
scanf("%lf", &initialGuess);

printf("Enter tolerance: ");


scanf("%lf", &tol);

// Find the root using fixed-point iteration


root = fixedPointIteration(initialGuess, tol);

// Display the final result


printf("\nThe root of the equation is: %lf\n", root);

return 0;
}

In this version, the maximum number of iterations is not explicitly taken as input from the user. Instead,
the loop now continues until the error percentage falls below the specified tolerance (`tol`). The
program adjusts the maximum number of iterations automatically based on the given tolerance.

ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/10

You might also like