Allaa S
Normal Distribution java program?
0 Answer(s)      3 years and 6 months ago
Posted in : Java Beginners

For my last assignment i have to ace this to raise my grade and i wanna perfect it so i need help from anybody who knows java: Write a program to tabulate the cumulative normal distribution F(z), for values of z from 0:0 to 3:0 by 0:1 increments, accurate to 4 decimal places Define f(x) = 1 p2 eô???x2=2.  Let z = 0, F = 0:5, h = 0:05. Print out z and F.  Set up a loop where i goes from 1 to 30. â??? Set z = i=10:0. â??? Increment F by (h=3)(f(z) + 4f(z ô??? h) + f(z ô??? 2h)). â??? Print z and F.  Note that in Java â??? px is Math.sqrt(x) â???  is Math.PI â??? ex is Math.exp(x) You will want to define two DecimalFormat objects, one for formatting z and the other for formatting F.

so what i got is

public class Distribution {
public static double abs(double x) {
if (x == 0.0) return x; // for -0 and +0
else if (x > 0.0) return x;
else return -x;
}

public static double exp(double x) {
double term = 1.0;
double sum = 1.0;
for (int N = 1; sum != sum + term; N++) {
term = term * Math.abs(x) / N;
sum = sum + term;
}
if (x >= 0) return sum;
else return 1.0 / sum;
}

public static double sqrt(double c) {
if (c == 0) return c;
if (c < 0) return Double.NaN;
double EPSILON = 1E-15;
double t = c;
while (Math.abs(t - c/t) > EPSILON * t) {
t = (c/t + t) / 2.0;
}
return t;
}

public static double erf(double z) {
double t = 1.0 / (1.0 + 0.5 * Math.abs(z));

}

// fractional error less than x.xx * 10 ^ -4.
public static double erf2(double z) {
double t = 1.0 / (1.0 + 0.47047 * Math.abs(z));
double poly = t * (0.3480242 + t * (-0.0958798 + t * (0.7478556)));
double ans = 1.0 - poly * Math.exp(-z*z);
if (z >= 0) return ans;
else return -ans;
}

public static double phi(double x) {
return Math.exp(-0.5 * x * x) / Math.sqrt(2 * Math.PI);
}

public static double phi(double x, double mu, double sigma) {
return phi((x - mu) / sigma) / sigma;
}

public static double Phi2(double z) {
if (z > 8.0) return 1.0; 
if (z < -8.0) return 0.0; 
double sum = 0.0, term = z;
for (int i = 3; sum + term != sum; i += 2) {
sum = sum + term;
term = term * z * z / i;
}
return 0.5 + sum * phi(z);
}

public static double Phi(double z) {
return 0.5 * (1.0 + erf(z / (Math.sqrt(2.0))));
}

public static double Phi(double z, double mu, double sigma) {
return Phi((z - mu) / sigma);
}

}

public static double gaussian() {
double U = Math();
double V = Math();
return Math.sin(2 * Math.PI * V) * Math.sqrt((-2 * Math.log(1 - U)));
}

public static double gaussian(double mu, double sigma) {
return mu + sigma * gaussian();
}

how can i fix it because the output is suppose to be 0.0 0.5000 0.1 0.5398 0.2 0.5793 0.3 0.6179 0.4 0.6554 0.5 0.6915 0.6 0.7257 0.7 0.7580 0.8 0.7881 0.9 0.8159 1.0 0.8413 1.1 0.8643 1.2 0.8849 1.3 0.9032 1.4 0.9192 1.5 0.9332 1.6 0.9452 1.7 0.9554 1.8 0.9641 1.9 0.9713 2.0 0.9772 2.1 0.9821 2.2 0.9861 2.3 0.9893 2.4 0.9918 2.5 0.9938 2.6 0.9953 2.7 0.9965 2.8 0.9974 2.9 0.9981 3.0 0.9987

Advertisement
View Answers