int main(){ double (*f)(double x)=&f3; double (*d)(double x)=&d3; double start=3; //double end=1.5; int decimal_accuracy=5; double error=pow(10,-decimal_accuracy); double answer; printf("The root of the function is: %.10lf\n", answer=newton_raphson(f,d,start,error)); printf("The value of the function is: %.10lf\n", (*f)(answer)); // printf("Bisection : %.10lf\n",iterative_bisection(f,3,start,error)); return 0; }
void newtonMain() { clock_t start, finish; double root, value, estimate, tolerance, time_taken; int iterations, max_iterations, error; char err_string[80]; get_conditions(estimate, tolerance, max_iterations); start = clock(); // Do the calculation 50000 times so that the time taken is a few // seconds, rather than a few milliseconds. for (unsigned long i = 0 ; i < 50000; i++) { error = newton_raphson(estimate, tolerance, max_iterations, iterations, root, value, err_string); } finish = clock(); time_taken = (finish - start) / double(CLOCKS_PER_SEC); // Print out the number of iterations, or an error message } if (error) { cout << "ERROR : " << err_string << endl; } else { cout << "Successful calculation, " << iterations << " iterations." << endl << "The root is " << root << endl << "The value of the function at this point is " << setiosflags(ios::fixed) << setprecision(2) << value << endl << "Time for 1000 Newton-Raphson calculations was " << time_taken << " seconds." << endl; } return 0; }
/**************************** EX10 ****************************/ void EX10(void) { double fluxo[2][ANOS] = {{0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0}, {-1000.0, 100.0, 200.0, 100.0, 200.0, 100.0, 200.0, 100.0, 200.0, 100.0, 200.0}}; double r = 0.0; int iter = 0; if (bisseccao(fluxo, 0.0, 1.0, r, iter)==0){ cout << "[EX10 out] Bissecção: TIR = " << r * 100.0 << "%" << endl; cout << "[EX10 out] Bissecção: Número de iterações = " << iter << endl; } if (newton_raphson(fluxo, 0.0, r, iter)==0){ cout << "[EX10 out] Newton-Raphson: TIR = " << r * 100.0 << "%" << endl; cout << "[EX10 out] Newton-Raphson: Número de iterações = " << iter << endl; } }
// Find zero points according to chapter (2.4.1) // Also stores wave numbers (zero-point / R) void find_zero_points(int l_max, double x_max, double x_min, double epsilon, double R) { int i, l; // Intervallgrenzen double width = fabs(x_max - x_min); // Anzahl Nullstellen von j_0 ( = sin(x)/x ) ist floor(width / pi): int N = floor(width / PI); // initialize zero points double zeros[N]; FILE *file_zp = fopen("results/zero-points/0.txt", "w+"), *file_wn = fopen("results/wave-numbers/0.txt", "w+"); for (i = 0; i < N; i++) { zeros[i] = (i+1) * PI; // i+1 = n fprintf(file_zp, "%f %e\n", zeros[i], 0.0); fprintf(file_wn, "%f %e\n", zeros[i]/R, 0.0); } fclose(file_zp); fclose(file_wn); bessel j; char fp_zp[100], fp_wn[100]; for (l = 1; l <= l_max; l++) { // open zero-point file snprintf(fp_zp, sizeof(fp_zp), "results/zero-points/%i.txt", l); printf("Opening file %s...\n", fp_zp); file_zp = fopen(fp_zp, "w+"); if (!file_zp) printf("ERROR: Cannot open file %s.", fp_zp); // open wave-numbers file snprintf(fp_wn, sizeof(fp_wn), "results/wave-numbers/%i.txt", l); printf("Opening file %s...\n", fp_wn); file_wn = fopen(fp_wn, "w+"); if (!file_wn) printf("ERROR: Cannot open file %s.", fp_wn); for (i = 0; i < N-1; i++) // only go up to N-1 ! { // start newton raphson at position x_start = (zeros[i] + zeros[i+1]) / 2 j = newton_raphson(epsilon, j_l(l, (zeros[i] + zeros[i+1])/2.0)); if (j.x < zeros[i+1] && j.x <= x_max) { fprintf(file_zp, "%f %e\n", j.x, j.val); fprintf(file_wn, "%f %e\n", j.x/R, j.val); zeros[i] = j.x; } else { printf("ERROR: Invalid value, j_%i(%f) = %f\n", j.l, j.x, j.val); } } // find last zero point if (N > 0) { // find approx. width of interval between two zero points double dN; if (N > 2) dN = zeros[N-1] - zeros[N-2]; else dN = x_max - zeros[N-1]; // use x_max as highest value instead // start at z_N + 1/2 dN j = newton_raphson(epsilon, j_l(l, zeros[N-1] + dN/2.0)); if (zeros[N-1] < j.x && j.x < x_max) { fprintf(file_zp, "%f %e\n", j.x, j.val); fprintf(file_wn, "%f %e\n", j.x/R, j.val); zeros[N-1] = j.x; } else { N--; // "drop" the last zero point } } fclose(file_zp); fclose(file_wn); } }
int main(void) { double temperature, init_pressure, final_pressure, num_increments, a, b, max_iterations, converg_criteria; char gas_type, output_filename[101]; int i = 0; double abs_pressure, press_single_incr, results[2], ideal; // get inputs get_double("Temperature (K)", &temperature); get_double("initial pressure (atm)", &init_pressure); get_double("final pressure (atm)", &final_pressure); get_double("number of pressure increments", &num_increments); get_char("gas type", &gas_type); if(gas_type == 'c') { a = 3.59200; b = 0.04267; } else if(gas_type == 'o') { a = 1.36000; b = 0.03183; } else { get_double("a (for a gas, if not carbon dioxide or oxygen)", &a); get_double("b (for a gas, if not carbon dioxide or oxygen)", &b); } get_double("maximum number of iterations", &max_iterations); get_double("convergence criteria", &converg_criteria); for(i = 0; i < 5; i++) { get_string("Output file name", output_filename); output = fopen(output_filename, "r"); if(output != NULL) { printf("Silly goose! '%s' already exists!\n", output_filename); fclose(output); } else { output = fopen(output_filename, "w"); break; } // the user has inputted 5 files that exist if(i == 4) { printf("Unable to make \"new\" output file after 5 attempts! Exiting!\n"); return 1; } } // output known data DBL_PRINT("\n\ninitial pressure (atm) = %lf\n", init_pressure); DBL_PRINT("final pressure (atm) = %lf\n", final_pressure); DBL_PRINT("number of pressure increments = %lf\n", num_increments); DBL_PRINT("\na = %lf\n", a); DBL_PRINT("b = %lf\n\n", b); DBL_PRINT("gas type = %c\n", gas_type); DBL_PRINT("maximum number of iterations = %lf\n", max_iterations); DBL_PRINT("convergence criteria = %lf\n", converg_criteria); DBL_PRINT("\n\n"); DBL_PRINT("Temperature (K)\t\tPressure (atm)\t\tMolal Volume,"); DBL_PRINT("\t\tNumber Iterations\t\tMolal Volume,\n"); DBL_PRINT("\t\t\t\t\t\t L/mol (vdW)\t\t\t\t\t\t L/mol (IGL)\n"); press_single_incr = (final_pressure - init_pressure) / num_increments; for(abs_pressure = init_pressure ; abs_pressure <= final_pressure ; abs_pressure += press_single_incr) { ideal = ideal_gas_law(abs_pressure, temperature); newton_raphson(results, ideal, abs_pressure, temperature, a, b, max_iterations, converg_criteria); DBL_PRINT(" %lf\t\t %10lf\t\t %10lf\t\t %10lf\t\t\t%10lf\n", temperature, abs_pressure, results[0], results[1], ideal); } fclose(output); return 0; }
double revised_temperature(double f(double,double,double), double epsilon_kl, double rho_kl, double theta_kl) { double theta1= newton_raphson(f,epsilon_kl,rho_kl,theta_kl); return theta1; }
double test_(double x) { return newton_raphson(0.001, parabola, parabola_, x); }