Answer to Question #161015 in C for Q

Question #161015

The equation f(x) ≡ (1 − x) cos x − sin x = 0 has at least one root between a = 0 and

b = 1 since f(a)f(b) < 0. The bisection method of finding the root proceeds as follows:

a. It finds the midpoint r = (a + b)/2.

b. If f(r) = 0, then r is the root. If |b − a| is very small less than , then also we can

take r as the root. In either of the cases, our job is done.

c. If f(r) 6= 0 and f(a)f(r) < 0, then the root lies between a and r. We assign r to b

and go to step a.

d. If f(r) 6= 0 and f(b)f(r) < 0, then the root lies between r and b. We assign r to a

and go to step a.

e. If the number of iterations is high, we may stop the process with appropriate message.

Write the following functions with the specifications mentioned.

1. Function func takes a real number x as argument and returns the value of f(x).

2. Function cbracket takes two real numbers a and b as arguments and returns 1 if at

least one real root of f(x) lies between a and b, and 0 otherwise.


3. Function rootb that takes three real numbers a, b, eps and an integer Nmax as argu-

ments. This function returns the root of f(x) = 0 using bisection method. If the number


of iteration is more than Nmax then the function terminates with appropriate message.

Write a C program using the above functions. This program accepts a, b, eps and Nmax

from the keyboard and prints out the root


1
Expert's answer
2021-02-04T08:01:57-0500
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double func(double x) {
    double q;
    
    q = (1.0 - x) * cos(x) - sin(x);
    return q;
}

double rootb(double a, double b, double eps, int Nmax) {
    double r;
    
    while ( Nmax > 0) {
        r = (a + b) / 2;
    
        if ( func(r) == 0 ) {
            return r;
        }         
    
        if ( fabs(b-a) < eps ) {
            return r;
        }    
        
        if ( func(a) * func(r) < 0 ) {
            b = r;
        }       
        else {
            a = r;
        }          
        Nmax--;                    
    }
    printf("Can\'t find root\n");
    exit(1);
}

int cbtracket(double a, double b) {
    if ( func(a) * func(b) <= 0.0 ) {
        return 1;
    }    
    else {
        return 0;
    }    
}        

int main() {
    double a, b, eps, x;
    int Nmax;
    
    printf("Enter real number a - ");
    scanf("%lf", &a);
    printf("Enter real number b - ");
    scanf("%lf", &b);
    printf("Enter real number eps - ");
    scanf("%lf", &eps);
    printf("Enter integer number Nmax - ");
    scanf("%d", &Nmax);
    
    
    if ( cbtracket(a, b) != 1 ) {
        printf("No root between a and b\n");
    }
    else {
        x = rootb(a, b, eps, Nmax);
        printf("The root between a and b is %f\n", x);
    }
    
    return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS