View the step-by-step solution to:

/* * * * * * * * * * * * */ BISECTION ALGORITHM 2.1 To find a solution to f(x) = 0 given the continuous function f on the interval [a,b], where f(a)...

Hello,

I'm currently working on a code for my class and I need to manipulate it in a way that my interval will split into 3 even parts. So basically I take the bisection method and transform it into a new method. I haven't had much exposure to computer science so hints would be nice to guide me in the right direction. The attachment is the standard bisection method. I did a few edits on it but I'm not sure if they are even right. I'm currently compiling it using Command Prompt. 

/* * BISECTION ALGORITHM 2.1 * * To find a solution to f(x) = 0 given the continuous function * f on the interval [a,b], where f(a) and f(b) have * opposite signs: * * INPUT: endpoints a,b; tolerance TOL; * maximum number of iterations N0. * * OUTPUT: approximate solution p or * a message that the algorithm fails. */ #include<stdio.h> #include<math.h> #define ZERO 1.0E-20 #define true 1 #define false 0 main() { double A,FA,B,FB,C,P,FP,TOL,C1,C2; int I,NO,OK,FLAG; FILE *OUP[1]; double absval(double); double F(double); void INPUT(int *, double *, double *, double *, double *, double *, int *); void OUTPUT(FILE **, int *); INPUT(&OK, &A, &B, &FA, &FB, &TOL, &NO); if (OK) { OUTPUT(OUP, &FLAG); /* STEP 1 */ I = 1; /* STEP 2 */ OK = true; while ((I<=NO) && OK) { /* STEP 3 */ /* compute P(I) */ C = (B - A) / 2; C1 = (B - A) / 3.0; C2 = C1 + (B - A) / 3.0; printf("C1 = %8.6f C2 = %8.6f \n", C1, C2); P = A + C; /* STEP 4 */ FP = F(P); if (FLAG == 2) fprintf(*OUP,"%3d %15.8e %15.7e \n",I,P,FP); if ((absval(FP)<ZERO) || (C<TOL)) { /* procedure completed successfully */ fprintf(*OUP,"\nApproximate solution P = %11.8f \n",P); fprintf(*OUP,"with F(P) = %12.8f\n",FP); fprintf(*OUP,"Number of iterations = %3d",I); fprintf(*OUP," Tolerance = %15.8e\n",TOL); OK = false; } else { /* STEP 5 */ I++; /* STEP 6 */ /* compute A(I) and B(I) */
Background image of page 1
if ((FA*FP) > 0.0) { A = P; FA = FP; } else { B = P; FB = FP; } } } if (OK) { /* STEP 7 */ /* procedure completed unsuccessfully */ fprintf(*OUP,"\nIteration number %3d",NO); fprintf(*OUP," gave approximation %12.8f\n",P); fprintf(*OUP,"F(P) = %12.8f not within tolerance : %15.8e\n",FP,TOL); } fclose(*OUP); } return 0; } /* Change function F for a new problem */ double F(double X) { double f; f = ( X + 4.0 ) * X * X - 10.0; return f; } void INPUT(int *OK, double *A, double *B, double *FA, double *FB, double *TOL, int *NO) { double X; char AA; printf("This is the Bisection Method.\n"); printf("Has the function F been created in the program immediately preceding\n"); printf("the INPUT function?\n"); printf("Enter Y or N\n"); scanf("%c",&AA); if ((AA == 'Y') || (AA == 'y')) { *OK = false; while (!(*OK)) { printf("Input endpoints A < B separated by blank\n"); scanf("%lf %lf", A, B); if (*A > *B) { X = *A; *A = *B; *B = X; } if (*A == *B) printf("A cannot equal B\n"); else { *FA = F(*A); *FB = F(*B); if (*FA*(*FB) > 0.0) printf("F(A) and F(B) have same sign\n"); else *OK = true; } } *OK = false; while(!(*OK)) { printf("Input tolerance\n"); scanf("%lf", TOL); if (*TOL <= 0.0) printf("Tolerance must be positive\n"); else *OK = true; }
Background image of page 2
Show entire document
Sign up to view the entire interaction

Top Answer

View the full answer
10297427.zip - Preview not available

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online