void findgraph (long double a, long double b, long double i, long double error, long double p, long double (*f) (long double, long double), char *name) { long double previous = f (a, p); long double current = f (a, p); unsigned long count = 0; printf ("Starting scan of %s on interval %Lf to %Lf with increment of %Lf\n\n", name, a, b, i); for (; a < b; a += i) { current = f (a, p); if (matchSign (previous, current)) { } else { printf (" Found sign change from %Lf to %Lf\n\n", a - i, a); bisect (a - i, a, error, p, f, name); secant (a - i, a, error, p, f, name); falseposition (a - i, a, error, p, f, name); } previous = current; count++; } printf ("Done scanning %s after %lu iterations\n\n", name, count); }
double search(int n) // Search algorithm { double root, i; for (i=-5; i<0; i = i + .01) // Searches Energy (i) between -5 and 0 { if (func(i,n)*func(i+1,n)<0) // When it crosses the axis 2 values multiplied together will give a negative result { root = falseposition(i, i+1,n); // Sends 2 values to the False position loop and returns with the roots } } printf("\n%.3f %d %f",a ,n , root); // prints energies }
int main () { //findgraph ( 2, 3, .1, .0001, 5, &fsqrt, "Find square Root of 5"); //findgraph (10,500, 5, .0000001, 5487, &fsqrt, "Find square Root of 5487"); findgraph ( 0, 1, .1, .0001, 0, &hw2p1, "hw2p1"); findgraph ( 1, 3.2, .1, .0001, 0, &hw2p2, "hw2p2"); findgraph ( 1, 2, .1, .0001, 0, &hw2p3, "hw2p3"); findgraph (-1, 0, .01, .001, 0, &hw2p4, "hw2p4"); secant (-1, 0, .001, 0, &hw2p4, "hw2p4"); falseposition (-1, 0, .001, 0, &hw2p4, "hw2p4"); findgraph (-1, 0, .01, .00001, 0, &hw2p5, "hw2p5 from -1 to 0"); findgraph ( 0, 1, .01, .00001, 0, &hw2p5, "hw2p5 from 0 to 1"); findgraph (-1 , 1, .1, .00001, 0, &hw2p5, "hw2p5 from 0 to 1"); findpoint (0, 2, 0, 1, .001, 8, &hw2p6, "hw2p6"); // testing the point function //findpoint (0, 2, 1, 1, .001, 8, &hw2p6, "hw2p6"); //findpoint (1, 3, 2, 4, .001, 8, &hw2p6, "hw2p6"); //findpoint (1, 4, 0, 2, .001, 8, &hw2p6, "hw2p6"); //findpoint (1, 8, 18, 0, .001, 8, &hw2p6, "hw2p6"); findgraph (0, 1, .1, .01, 12.4, &hw2p7a, "hw2p7 trough with 12.4 volume"); ///* printf ("finding a range of values for testing.\n"); long double i; for (i=0; i< 1; i=i+.1){ char name[40]; sprintf(name, "hw2p7 %Lf", i); //findgraph (0, 1, .01, .00001, i, &hw2p7, name); hw2p7(i, 0); hw2p7a(i, 0); } //*/ //findgraph (-1, 3, .1, .001, 38, &hw2ptri, "hw2ptri full"); //findgraph (-1, 3, .1, .001, 10, &hw2ptri, "hw2ptri half full"); //findgraph (-1, 3, .1, .001, .1, &hw2ptri, "hw2ptri empty"); //findgraph ( 0, 2, .1, .00000001, 0, &fex, "fex"); }