0

I'm trying to find the root of the following equation using the bisection method:

x*(1 + (k*n)/(1 + k*x)) - L = 0.

the user inputs the values for k, n, and L and the program should solve for x. Can anyone tell me why my program isn't working? The problem is in the middle in my for loop because it's not calculatoin FP1 correctly but I don't know why. I inputted, k =1, n = 1, L = 3 and the root is 2.303 but my program said 0.0007, don't know why. Any help would be greatly appreciated! Thanks!

#include <iostream>
#include <math.h>
using namespace std;

int main () {
    double k1;
    double n1;
    double tolerance;
    int N;
    double L;
    double a, b;

    cout << "Welcome to the Root Finding Calculator using the Bisection Method!" << endl << endl;
    cout << "please enter a value for k1: ";
    cin >> k1;
    cout << "please enter a value for n1: ";
    cin >> n1;
    cout << "please enter the value for L: ";
    cin >> L;
    cout << "please enter an endpoint, a: ";
    cin >> a;
    cout << "please enter the other endpoint, b: ";
    cin >> b;
    cout << "please enter the level of tolerance: ";
    cin >> tolerance;
    cout << "please enter the maximum number of iterations to be performed: ";
    cin >> N;

    double FA = (a * (1 + (k1*n1)/(1 + (k1*a)))) - L;
    double FB = (b * (1 + (k1*n1)/(1 + (k1*b)))) - L;
    if ((FA * FB) > 0) {
        cout << "Improper interval, try again!" << endl;
        return 0;
    }


        for (int i = 1; i <= N; i++) {
            double p1 = ((a + b)/2);
            double FP1 = (p1 * (1 + (k1*n1)/(1 + (k1*p1)))) - L;
            if (FP1 = 0 || fabs((b - a)/2) < tolerance) {
                cout << "The root is " << p1 << endl;
                return 0;
            }
            else {
                i += 1;
            }
            cout << "Iter " << i << ": p1 = " << p1 << ", dp = " << ((b - a)/2) << ", a = " << a << ", b = " << b << ", f(p) = " << FP1 << endl;
            if (FA * FP1 > 0) {
                a = p1;
                FA = FP1;
            }
            else {
                b = p1;
                FB = FP1;
            }
        }
    cout << "This method failed after " << N << " iterations." << endl;
    return 0;
}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by WolfPack
0

Check the Parts in Red. And post code within tags next time.

#include <iostream>
#include <math.h>
using namespace std;

int main () {
	double k1;
	double n1;
	double tolerance;
	int N;
	double L;
	double a, b;

	cout << "Welcome to the Root Finding Calculator using the Bisection Method!" << endl << endl;
	cout << "please enter a value for k1: ";
	cin >> k1;
	cout << "please enter a value for n1: ";
	cin >> n1;
	cout << "please enter the value for L: ";
	cin >> L;
	cout << "please enter an endpoint, a: ";
	cin >> a;
	cout << "please enter the other endpoint, b: ";
	cin >> b;
	cout << "please enter the level of tolerance: ";
	cin >> tolerance;
	cout << "please enter the maximum number of iterations to be performed: ";
	cin >> N;

    double FA = (a * (1 + (k1*n1)/(1 + (k1*a)))) - L;
    double FB = (b * (1 + (k1*n1)/(1 + (k1*b)))) - L;
	if ((FA * FB) > 0) {
		cout << "Improper interval, try again!" << endl;
		return 0;
	}
	
	
		for (int i = 1; i <= N; i++) {
			double p1 = ((a + b)/2);
			double FP1 = (p1 * (1 + (k1*n1)/(1 + (k1*p1)))) - L;
			if (FP1 == 0 || fabs((b - a)/2) < tolerance) {
				cout << "The root is " << p1 << endl;
				return 0;
			}
			else { // You dont need this.
				i += 1;
			}
			cout << "Iter " << i << ": p1 = " << p1 << ", dp = " << ((b - a)/2) << ", a = " << a << ", b = " << b << ", f(p) = " << FP1 << endl;
			if (FA * FP1 > 0) {
				a = p1;
				FA = FP1;
			}
			else {
				b = p1;
				FB = FP1;
			}
		}
	cout << "This method failed after " << N << " iterations." << endl;
	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.