Skip to content

Instantly share code, notes, and snippets.

@naezith
Last active December 21, 2016 17:27
Show Gist options
  • Save naezith/c8bdba4c8cf0b147cab8c718cc736ca0 to your computer and use it in GitHub Desktop.
Save naezith/c8bdba4c8cf0b147cab8c718cc736ca0 to your computer and use it in GitHub Desktop.
Derivation and Percent Error calculation, http://imgur.com/a/r7n0z
import java.text.DecimalFormat;
import java.text.NumberFormat;
// Solution for the question here -> http://imgur.com/a/r7n0z
public class Demo {
public static void main(String[] args) {
NumberFormat formatter = new DecimalFormat("#0.00000");
final double T = 70;
final double L = 1;
final double G = 77.2;
final double c = 0.150;
final double exact_coef = 0.18568;
final double[] n_list = new double[] {4, 8, 20, 100};
System.out.println("n \taproximate \texact \t\tpercent error");
for(int m = 0; m < 4; ++m) {
final double exact_exp = exact_coef * T * L / (G * Math.pow(c, 4));
final double n = n_list[m];
double approx = .0;
// Derivation for n times
for (int i = 1; i <= n; ++i) {
double ri = (n + i - 0.5) * (c / n);
double Ji = (Math.PI * 0.5) * Math.pow(ri, 4);
approx += T * (L / n) / (G * Ji);
}
final double percent_error = 100 * (approx - exact_exp) / exact_exp; // Save percent error
// Print the result
System.out.println((int)n_list[m] + " \t" + formatter.format(approx) + " \t" + exact_coef + "\t\t" + formatter.format(percent_error));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment