0

I'm using the bisection method to find the root of function in the domain from 70*10^9 to 250*10^9, but the output is always the upper bound, i.e 250*10^9. The function is a definite integral, I don't know where I did wrong. Thanks in advance.

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

double I = 0.0225, W = 50000, L = 25, Y = 12000;
double x, E;
typedef double(*DfD)(double);
double g(double), h(double);
double pow(double a, double b);

double g(double x){
    return W*x*(x-L/2)-(Y*pow(x,3))/2;
}

double h(double x){
    return Y*pow(x,3)/2;
}

double midpoint_int(DfD f, double x0, double x1, int n) {
    int i;
    double x, dx, sum = 0.0;
    dx = (x1-x0)/n;
    for (i = 0, x = x0 + dx/2; i < n; i ++, x +=dx)
        sum += f(x);
    return sum*dx;
}

double deflection (DfD f, double int1, double int2){
    int1 = midpoint_int(g, L/2, L, 1000);
    int2 = midpoint_int(h, 0, L/2, 1000);
    return (int1 - int2)/(E*I)+0.5;
}

double bisection(DfD f, double a0, double a1, double tol ){
    double middle;
    for (;;){
        middle = (a0 + a1)/2.0;
        if (fabs(middle - a0) < tol)
            return middle;
        else if (f(middle) * f(a0) < 0.0)
            a1 = middle;
        else
            a0 = middle;
    }
}

int main (void){
    double E;
    E = bisection (deflection, 70*pow(10,9), 250*pow(10,9), 0.001);
    printf("The optimal elastic modulus is %fPa.\n", E);

    return;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
YYCCC
  • 1
  • Using gcc, i got a compiler warning : `passing argument 1 of ‘bisection’ from incompatible pointer type [enabled by default] main.c:33:8: note: expected ‘DfD’ but argument is of type ‘double (*)(double (*)(double), double, double)’` : it may explain your problem. – francis Apr 13 '15 at 16:36
  • 1
    Once deflection is defined as `double deflection (double E)`, the bisection algorithm seems to work fine...It always find 250GPa because `deflection(E)` is positive in the range 70--250GPa...print f(a0) and f(a1) ! The root is about 44.9GPa... My guess is that there is a little problem in your previous computations. Have you noticed that the integrals can be computed with a pen ? These are polynomial functions... The optimal Young modulus can be computed analytically...by hand ! – francis Apr 13 '15 at 17:35
  • Thanks so much! There's a typo in the instruction of this assignment! – YYCCC Apr 13 '15 at 21:13

0 Answers0