Question

In: Computer Science

Write the c++ program of Bisection Method and False Position Method and newton Raphson method in...

Write the c++ program of Bisection Method and False Position Method and newton Raphson method in a one program using switch condition .Like :

cout<<"1.Bisection Method \n 2.False Position Method\n3.Newton Raphson method"<<endl; if press 1 then work Bisection Method code if press 2 then workFalse Position Method if press 3. work Newton Raphson method .so please writhe the code in c++ using switch case.and the equation given down consider the equation in the given.Note: Must showt the all the step of output all the iteration step shown in the program .in program must shown all the step of iteration result like every iteration x1,x2,x3 must be shown in program.

f(x)=3x-cosx-1;

Please in every itaration must be shown the output then finally Got the root

Solutions

Expert Solution

Below is the code in c++:

#include<bits/stdc++.h>
#include<math.h>
using namespace std;
#define EPSILON 0.00001
#define MAX_ITER 20

double fun(double x)
{
    return 3*x-cos(x)-1;
}

double der_fun(double x)
{
    return 3+sin(x);
}

void bisection(double a, double b)
{
    if (fun(a) * fun(b) >= 0)
    {
        cout << "You have not assumed right a and b\n";
        return;
    }
    long i=0;
    double c = a;
    while ((b-a) >= EPSILON)
    {
        c = (a+b)/2;
        cout<<"\nIteration "<<++i<<":\n";
        cout<<"a = "<<a<<"\tb = "<<b<<"\tc = "<<c;
        if (fun(c) == 0.0)
            break;
        else if (fun(c)*fun(a) < 0)
            b = c;
        else
            a = c;
    }
    cout << "\n\n\t\tThe value of root is : " << c;
}

void regula_falsi(double a, double b)
{
    if (fun(a) * fun(b) >= 0)
    {
        cout << "\nYou have not assumed right a and b\n";
        return;
    }

    double c = a;

    for (int i=0; i < MAX_ITER; i++)
    {
        c = (a*fun(b) - b*fun(a))/ (fun(b) - fun(a));
        cout<<"\nIteration "<<i+1<<":\n";
        cout<<"a = "<<a<<"\tb = "<<b<<"\tc = "<<c;
        if (fun(c)==0)
            break;
        else if (fun(c)*fun(a) < 0)
            b = c;
        else
            a = c;
    }
    cout << "\n\n\t\tThe value of root is : " << c;
}


void newton_raph(double x)
{
    double h = fun(x) / der_fun(x);
    long i=0;
    while (abs(h) >= EPSILON)
    {
        h = fun(x)/der_fun(x);

        x = x - h;
        cout<<"\nIteration "<<i++<<":\n";
        cout<<"\t new x = "<<x;
    }

    cout << "\n\n\t\tThe value of the root is : " << x;
}



int main()
{
    double a =-200, b = 300;
    double x0 = -200;
    int num;
    cout<<"\n\n<------------------WELCOME---------------------->";
    cout<<"\n\nEquation is 3*x - cos(x) - 1 ";
    cout<<"\n\n\t\t<-----------------------MENU---------------------------->";
    cout<<"\n\n\t\t Enter 1 to find the ROOT of this equation with BISECTION method";
    cout<<"\n\n\t\t Enter 2 find the ROOT of this equation with regula_falsi method";
    cout<<"\n\n\t\t Enter 3 find the ROOT of this equation with NEWTON RAPHSON method";
    cout<<"\n\n\t\tNOW ENTER____:";
    cin>>num;
    switch(num)
    {
    case 1:
        bisection(a, b);
        break;
    case 2:
        regula_falsi(a, b);
        break;
    case 3:
        newton_raph(x0);
        break;
    default:
        cout<<"\nWrong Choice!!!!!!!!!LOL";
    }
    return 0;
}

output

DON'T FORGET TO GIVE A LIKE


Related Solutions

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.
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      ...
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 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
Let . If we use Accelerated Newton-Raphson method to approximate the root of the equation ,...
Let . If we use Accelerated Newton-Raphson method to approximate the root of the equation , which of the following(s) is/are ture: (I)  is multiple root of order (II) Accelerated Newton-Raphson formula is : (III) The sequence  obtained by the Accelerated Newton-Raphson method converge to the root  quadratically.
Newton-Raphson Method.Kindly explain the method and use illustrations for clear understanding of concepts
Newton-Raphson Method.Kindly explain the method and use illustrations for clear understanding of concepts
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for...
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for finding the roots of function. The signature of your function should look like def find_root(f,a,b,n): where n is the maximum number of iterations of to search for the root. The code should follow this algorithm: We are given a continuous function f and numbers a and b and with a<b with f(a)<0<f(b). From the intermediate value theorem we know that there exists a c...
Consider the Newton-Raphson method for finding root of a nonlinear function ??+1=??−?(??)?′(??), ?≥0. a) Prove that...
Consider the Newton-Raphson method for finding root of a nonlinear function ??+1=??−?(??)?′(??), ?≥0. a) Prove that if ? is simple zero of ?(?), then the N-R iteration has quadratic convergence. b) Prove that if ? is zero of multiplicity ? , then the N-R iteration has only linear convergence.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT