Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I have written a code that uses the bisection method to find a root. The only

ID: 3633314 • Letter: S

Question

So I have written a code that uses the bisection method to find a root. The only problem is that it needs to test whether a root lies at either (or both) ends of the interval. The equation I am testing is just 3x which means the root is obviously at 0. I enter values for the lower and upper end, 0 and 1, and what it is supposed to do is just print out that the root is at the lower end value of x = 0 and that's it. It isn't supposed to actually go into the whole bisection method part. Can somebody PLEASE help me with this one small part? Everything else in the code works great. Thank you so so much!

#include <stdio.h>
#include <math.h>

double f (double x);

void main ()
{
double x, xl, xu, xr, xr_prev, lower, upper, test1, test2, rel_error, iteration, fx;

printf ("Please enter the lower end value: ");
scanf ("%lf",&xl);
printf (" Please enter the upper end value: ");
scanf ("%lf",&xu);

lower = f(xl);
upper = f(xu);

if (lower = 0)
printf ("The root lies at the lower end of the interval at x = %lf", xl);
if (upper = 0)
printf ("The root lies at the upper end of the interval at x = %lf", xu);

test1 = f(xl) * f(xu);
rel_error = 1.0;
iteration = -1.0;
xr_prev = 0;

while (test1 > 0) {
printf (" A root has not been bracketed. Please re-enter values:");
printf (" Lower end value: ");
scanf ("%lf",&xl);
printf (" Upper end value: ");
scanf ("%lf",&xu);
test1 = f(xl) * f(xu);
}

while (rel_error > .000001) {
xr = (xl + xu) / 2.0;
test2 = f(xl) * f(xr);
iteration = iteration + 1;
xr = (xu + xl) / 2.0;
rel_error = fabs((xr - xr_prev)/(xr));

printf (" Iteration = %lf", iteration);
printf (" Lower end of bracketing interval = %lf", xl);
printf (" Upper end of bracketing interval = %lf", xu);
printf (" Estimate of root = %lf", xr);
printf (" Relative approximate error = %lf%% ", rel_error*100.0);

if (test2 < 0) {
xr_prev = (xl + xu) / 2.0;
xl = xl;
xu = xr;
}

else {
xr_prev = (xl + xu) / 2.0;
xl = xr;
xu = xu;
}
}
}

double f (double x)
{
double fx;
fx = 3.0*x;
return (fx);
}

Explanation / Answer

please rate - thanks

I don't know the math but did find your tests for equality were wrong. you had = instead of ==

however after fixing that it went through the bisection. I tried fixing that in the 2nd half of the post-you can message me if needed


#include <stdio.h>
#include <math.h>
double f (double x);

void main ()
{
double x, xl, xu, xr, xr_prev, lower, upper, test1, test2, rel_error, iteration, fx;

printf ("Please enter the lower end value: ");
scanf ("%lf",&xl);
printf (" Please enter the upper end value: ");
scanf ("%lf",&xu);

lower = f(xl);
upper = f(xu);

if (lower ==0)
printf ("The root lies at the lower end of the interval at x = %lf", xl);
if (upper == 0)
printf ("The root lies at the upper end of the interval at x = %lf", xu);

test1 = f(xl) * f(xu);
rel_error = 1.0;
iteration = -1.0;
xr_prev = 0;

while (test1 > 0) {
printf (" A root has not been bracketed. Please re-enter values:");
printf (" Lower end value: ");
scanf ("%lf",&xl);
printf (" Upper end value: ");
scanf ("%lf",&xu);
test1 = f(xl) * f(xu);
}

while (rel_error > .000001) {
xr = (xl + xu) / 2.0;
test2 = f(xl) * f(xr);
iteration = iteration + 1;
xr = (xu + xl) / 2.0;
rel_error = fabs((xr - xr_prev)/(xr));

printf (" Iteration = %lf", iteration);
printf (" Lower end of bracketing interval = %lf", xl);
printf (" Upper end of bracketing interval = %lf", xu);
printf (" Estimate of root = %lf", xr);
printf (" Relative approximate error = %lf%% ", rel_error*100.0);

if (test2 < 0) {
xr_prev = (xl + xu) / 2.0;
xl = xl;
xu = xr;
}

else {
xr_prev = (xl + xu) / 2.0;
xl = xr;
xu = xu;
}
}
}

double f (double x)
{
double fx;
fx = 3.0*x;
return (fx);
}

------------------------------------------------------

to prevent it from continuing


#include <stdio.h>
#include <math.h>
double f (double x);

void main ()
{
double x, xl, xu, xr, xr_prev, lower, upper, test1, test2, rel_error, iteration, fx;

int done=0;

printf ("Please enter the lower end value: ");
scanf ("%lf",&xl);
printf (" Please enter the upper end value: ");
scanf ("%lf",&xu);

lower = f(xl);
upper = f(xu);

if (lower ==0)
     { printf ("The root lies at the lower end of the interval at x = %lf ", xl);
    done=1;
    }
if (upper == 0)
     {printf ("The root lies at the upper end of the interval at x = %lf ", xu);
      done=1;
    }
if(done==0)
{
test1 = f(xl) * f(xu);
rel_error = 1.0;
iteration = -1.0;
xr_prev = 0;

while (test1 > 0) {
printf (" A root has not been bracketed. Please re-enter values:");
printf (" Lower end value: ");
scanf ("%lf",&xl);
printf (" Upper end value: ");
scanf ("%lf",&xu);
test1 = f(xl) * f(xu);
}

while (rel_error > .000001) {
xr = (xl + xu) / 2.0;
test2 = f(xl) * f(xr);
iteration = iteration + 1;
xr = (xu + xl) / 2.0;
rel_error = fabs((xr - xr_prev)/(xr));

printf (" Iteration = %lf", iteration);
printf (" Lower end of bracketing interval = %lf", xl);
printf (" Upper end of bracketing interval = %lf", xu);
printf (" Estimate of root = %lf", xr);
printf (" Relative approximate error = %lf%% ", rel_error*100.0);

if (test2 < 0) {
xr_prev = (xl + xu) / 2.0;
xl = xl;
xu = xr;
}

else {
xr_prev = (xl + xu) / 2.0;
xl = xr;
xu = xu;
}
}
}
}
double f (double x)
{
double fx;
fx = 3.0*x;
return (fx);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote