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

ANOTHER METHOD TO SOLVE BISECTION METHOD IN C++ #2

Open
KunjShah95 opened this issue Jul 10, 2024 · 1 comment
Open

ANOTHER METHOD TO SOLVE BISECTION METHOD IN C++ #2

KunjShah95 opened this issue Jul 10, 2024 · 1 comment

Comments

@KunjShah95
Copy link

#include
#include
#include

// Function definition to calculate the value of the function at a given point
double get_fun(double res) {
return (res * res * res - 4 * res - 9);
}

// Function to perform the bisection method
double bisect(double int_st, double int_end, double err_all, int mx_iter_cnt) {
double mid_pt = (int_st + int_end) / 2;
int iter_cnt = 0;
std::cout << std::fixed << std::setprecision(6);
while (iter_cnt < mx_iter_cnt && fabs(int_st - int_end) > err_all) {
mid_pt = (int_st + int_end) / 2;
std::cout << "Iteration " << iter_cnt + 1 << ": " << mid_pt << std::endl;
if (get_fun(int_st) * get_fun(mid_pt) < 0) {
int_end = mid_pt;
} else {
int_st = mid_pt;
}
++iter_cnt;
}
return mid_pt;
}

int main() {
double int_st, int_end, err_all;
int mx_iter_cnt;
std::cout << "Enter the first starting point: ";
std::cin >> int_st;
std::cout << "Enter the second ending point: ";
std::cin >> int_end;
std::cout << "Enter the maximum iterations to be allowed: ";
std::cin >> mx_iter_cnt;
std::cout << "Input the number of allowed error points: ";
std::cin >> err_all;
double root = bisect(int_st, int_end, err_all, mx_iter_cnt);
std::cout << "The approximate root is: " << root << std::endl;
return 0;
}

@KunjShah95
Copy link
Author

#include
#include
#include

// Function definition to calculate the value of the function at a given point
double get_fun(double res) {
return (res * res * res - 4 * res - 9);
}

// Function to perform the bisection method
double bisect(double int_st, double int_end, double err_all, int mx_iter_cnt) {
double mid_pt = (int_st + int_end) / 2;
int iter_cnt = 0;
std::cout << std::fixed << std::setprecision(6);
while (iter_cnt < mx_iter_cnt && fabs(int_st - int_end) > err_all) {
mid_pt = (int_st + int_end) / 2;
std::cout << "Iteration " << iter_cnt + 1 << ": " << mid_pt << std::endl;
if (get_fun(int_st) * get_fun(mid_pt) < 0) {
int_end = mid_pt;
} else {
int_st = mid_pt;
}
++iter_cnt;
}
return mid_pt;
}

int main() {
double int_st, int_end, err_all;
int mx_iter_cnt;
std::cout << "Enter the first starting point: ";
std::cin >> int_st;
std::cout << "Enter the second ending point: ";
std::cin >> int_end;
std::cout << "Enter the maximum iterations to be allowed: ";
std::cin >> mx_iter_cnt;
std::cout << "Input the number of allowed error points: ";
std::cin >> err_all;
double root = bisect(int_st, int_end, err_all, mx_iter_cnt);
std::cout << "The approximate root is: " << root << std::endl;
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

No branches or pull requests

1 participant