Question

In: Computer Science

Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for...

Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU



1. Bisection Method

#include<bits/stdc++.h>
using namespace std;
#define EPSILON 0.01


double func(double x)
{
        return  2 * x * x * x - 11.7 * x * x + 2 + 17.7 * x - 5;
}

// Prints root of func(x) with error of EPSILON
void bisection(double a, double b)
{
        if (func(a) * func(b) >= 0)
        {
                cout << "You have not assumed right a and b\n";
                return;
        }

        double c = a;
        while ((b - a) >= EPSILON)
        {
                c = (a + b) / 2;

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

                else if (func(c)*func(a) < 0)
                        b = c;
                else
                        a = c;
        }
        cout << "The value of root is : " << c;
}

int main()
{
        double a = 0 , b = 4;
        bisection(a, b);
        return 0;
}

2. Newton Raphson Method

#include<bits/stdc++.h>
using namespace std;
#define EPSILON 0.01


double func(double x)
{
        return  2 * x * x * x - 11.7 * x * x  + 17.7 * x - 5;
}

// Prints root of func(x) with error of EPSILON
double derivFunc(double x) {
        6 * x * x - 23.14 * x + 17.7;
}
void newtonRaphson(double x)
{
        double h = func(x) / derivFunc(x);
        while (abs(h) >= EPSILON)
        {
                h = func(x) / derivFunc(x);

                // x(i+1) = x(i) - f(x) / f'(x)
                x = x - h;
        }

        cout << "The value of the root is : " << x;
}

int main()
{
        double x0 = -20; // Initial values assumed
        newtonRaphson(x0);
        return 0;
}

3.Secant Method

#include<bits/stdc++.h>
using namespace std;
const int E =  0.01;


double f(double x)
{
        return  2 * x * x * x - 11.7 * x * x  + 17.7 * x - 5;
}

void secant(float x1, float x2, float E)
{
        float n = 0, xm, x0, c;
        if (f(x1) * f(x2) < 0) {
                do {
                        x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));

                        c = f(x1) * f(x0);
                        x1 = x2;
                        x2 = x0;
                        n++;
                        if (c == 0)
                                break;
                        xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
                } while (fabs(xm - x0) >= E && n <= 100); 

                cout << "Root of the given equation=" << x0 << endl;
                cout << "No. of iterations = " << n << endl;
        } else
                cout << "Can not find a root in the given inteval";
}

// Driver code
int main()
{
        // initializing the values
        float x1 = 0, x2 = 1, E = 0.0001;
        secant(x1, x2, E);
        return 0;
}

4. False-Position Method

#include<bits/stdc++.h>
using namespace std;
const int E =  0.01;
const int MAX_ITER = 100;

double func(double x)
{
        return  2 * x * x * x - 11.7 * x * x  + 17.7 * x - 5;
}
void regulaFalsi(double a, double b)
{
        if (func(a) * func(b) >= 0)
        {
                cout << "You have not assumed right a and b\n";
                return;
        }

        double c = a;  // Initialize result

        for (int i = 0; i < MAX_ITER; i++)
        {
                // Find the point that touches x axis
                c = (a * func(b) - b * func(a)) / (func(b) - func(a));

                // Check if the above found point is root
                if (func(c) == 0)
                        break;

                // Decide the side to repeat the steps
                else if (func(c)*func(a) < 0)
                        b = c;
                else
                        a = c;
        }
        cout << "The value of root is : " << c;
}

// Driver program to test above function
int main()
{
        // Initial values assumed
        double a = 0, b = 4;
        regulaFalsi(a, b);
        return 0;
}


Related Solutions

in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make...
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations. (a) f(x) = 2x3 – 11.7x2 + 17.7x – 5 This function has 3 +ve roots, all of which lie between 0 and 4. Find the roots. Implement the...
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of...
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of scalar nonlinear equa-tions. Use these programs to compute approximations to real roots of the following equations: exp(x)−3x^2=0, (1) x^3=x^2+x+1, (2) exp(x) =1/(0.1 +x^2), (3) and x= 1 + 0.3 cos(x). (4) Use an error tolerance tol=10^(−12). Display the obtained approximations to the roots of these equations, and compare the number of iterations, starting with the same initial values x0 for bisection and Newton methods,...
Use Bisection and Newton Raphson methods to find roof for the following equation manually. F(x)=x^2 –...
Use Bisection and Newton Raphson methods to find roof for the following equation manually. F(x)=x^2 – 5 = 0 ε = 0.01
Modify/write your Newton-Raphson program in c language for single nonlinear equation to solve the following nonlinear...
Modify/write your Newton-Raphson program in c language for single nonlinear equation to solve the following nonlinear system f1(x,y)=x3+y−1 = 0 f2(x,y)=y3−x+1 = 0 using the Newton-Raphson method with initial solutions x(0) = 0.5 and y(0) = 0.5 and the convergence criterion max(|f1(x, y)|, |f2(x, y)|) < ε = 10−6.
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all...
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature function output = solve(f,options) % your code here end where the input is • f: the function in f(x) =0. options: is a struct type with the following fields o method: bisection, newton or secant tol: the tolerance for stopping the iterations. maximum_iterations: the maximum number of iterations allowed. initial_guess: that is P_0; if...
How do the Gauss-Seidel, Newton-Raphson, and Fast-Decoupled-Newton-Raphson iteration methods differ from task layout, iteration terminations, and...
How do the Gauss-Seidel, Newton-Raphson, and Fast-Decoupled-Newton-Raphson iteration methods differ from task layout, iteration terminations, and from the point of convergence? What does the Gauss-Seidel method acceleration factor means, how does it affect the calculation?
Write a funtion and apply multi-var. Newton-Raphson method.
Write a funtion and apply multi-var. Newton-Raphson method.
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy your Bisect code and modify).   Evaluation of F(x) and F'(x) should be done in a subprogram FCN(x).   The code should ask for input of: x0, TOL, maxIT (and should print output similar to Bisect code).   Debug on a simple problem, like x2−3 = 0.   Then use it to find root of F(x) in [1,2] with TOL=1.e-12. Now consider the problem of finding zeros of      ...
MODIFIED TRUE OR FALSE 1.If there are not enough corporate officers, the position of President can...
MODIFIED TRUE OR FALSE 1.If there are not enough corporate officers, the position of President can be merged with that of the operations officer. 2.A person may be both Secretary and Treasurer of a corporation at the same time. 3.In all corporations, a compliance officer and/or an independent director is required. 4.Mr. Graest, a director candidate was alledged to have committed sexual assault and as such is disqualified to be elected as director. 5.Mr. Xylon, a director candidate was accused...
Write a C-program to implement the Reader/Writer model for the attached pseudocode modified to produce only...
Write a C-program to implement the Reader/Writer model for the attached pseudocode modified to produce only one million output instead of an infinite amount. Use pthreads and Linux semaphores. Include ten readers with one writer. The writer should produce one million outputs. Verify that the readers receive an equal share of the information produced. Meaning each reader receives about 100,000 of information. Explain your results regardless of the balance. No use mutex or monitor functions. ****PSEUDOCODE**** /* program readersandwriters */...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT