Skip to main content

Example - Regula Falsi method

Task 1 

#include <stdio.h>

#include <math.h>

double f(double x) {

return cos(x) - x*x*x;

}

double FalsiMethod(double (*f)(double), double a, double b, double e, int m) {

double c, fc;

int n, side = 0;

double fa = f(a);

double fb = f(b);

W2

for (n = 0; n < m(WT1); n++) {

c = (fa * b - fb * a) / (fa - fb);

if (fabs(b - a) < e * fabs(b + a)(WT2))

break;

fc = f(c);

if (fc * fb > 0 (WT3)) {

b = c; fb = fc;

if (side == -1 (WT4))

fa /= 2;

side = -1;

} else if (fa * fc > 0 (WT5)) { 
   a = c; fa = fc;

if (side == +1 (WT6))

fb /= 2;

side = +1;

} else {

break;

}

}

return c;

}

int main(void) {

printf("%0.15f\n", FalsiMethod(&f, 0, 1, 5E-15, 100));

return 0;

}

 

 Section 9.3.3 of the Textbook (+ source code), up to the CFG.

 Task 2 

 source code with annotated instructions + Control Flow Graph 

 Something like: 

 for (i = 1 /* B1 */; i < length /* B2 */; ++i /* B3 */)      {         value = a[i]; /* B4 */     }