Normal Distribution java program?

Ads

 
 
 

Share on Google+Share on Google+

Allaa S
Normal Distribution java program?
0 Answer(s)      5 years and 5 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

Ads
View Answers
Ads









Tutorials   
Java Spring Hibernate Struts Training java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile?

Ads

 
Advertisement null

Ads