|
|
|
Friday, 17 February 2012 10:17 |
// Find X,Y intersection of 2 lines
public class findLineIntersection {
public static void main(String args[]){
//Ay = Bx + C
//Dy = Ex + F
double A,B,C,D,E,F;
A=1; B=2; C=3; D=4; E=5; F=6;
double X = findX(A,B,C,D,E,F);
double Y = findY(A,B,C,X);
System.out.println("("+X+","+Y+")");
}
public static double findX(double A, double B, double C, double D, double E, double F){
double neg =-1;
double Q,R,S;
if (A<0 ^ D<0){
//If A or D is negative add the values
neg = 1;
}
//Eliminate Y using multiple
double multiple = neg*(A/D);
D = D*multiple;
E = E*multiple;
F = F*multiple;
//Q will be 0
Q = A+D;
R = B+E;
S = C+F;
return (-S)/R;
}
public static double findY(double A, double B, double C, double X){
return ((B*X) + C)/A;
}
}
 Read more: |
|
|
Friday, 17 February 2012 09:37 |
//Finds X, Y intercepts of a line
public class findIntercepts {
//Finds X and Y intercepts for a line
public static void main (String args[]){
//input: Ax + By = C
double A =5;
double B =5;
double C =5;
System.out.println("X intercept: ("+findXIntercept(A,C)+ ",0)");
System.out.println("Y intercept: (0,"+findYIntercept(B,C)+")");
}
public static Object findXIntercept(double A, double C){
if (A==0){
return null;
}
else {
return C/A;
}
}
public static Object findYIntercept(double B, double C){
if (B==0){
return null;
}
else{
return C/B;
}
}
}
 Read more: |
|
Monday, 20 June 2011 02:25 |
this simple program will calculate the SD(standard deviation) for the given input.
public class StandardDeviation{
public static double mean ( double[] data )
{
double mean = 0;
final int n = data.length;
if ( n < 2 )
return Double.NaN;
for ( int i=0; i mean += data[i];
mean /= n;
double sum = 0;
for ( int i=0; i {
final double v = data[i] - mean;
sum += v * v;
}
return Math.sqrt( sum / ( n - 1 ) );
}
public static double sd ( double[] data ){
final int n = data.length;
if ( n < 2 )
return Double.NaN;
double avg = data[0];
double sum = 0;
for ( int i = 1; i < data.length; i++ )
{
double newavg = avg + ( data[i] - avg ) / ( i + 1 );
sum += ( data[i] - avg ) * ( data [i] -newavg ) ;
avg = newavg;
}
return Math.sqrt( sum / ( n - 1 ) );
}
public static void main ( String[] args )
{
double[] data = { 10, 100 , 50};
System.out.println(mean(data));
System.out.println(sd (data));
}
}
Know more theory about standard deviation Standard Deviation Calculator Read more: |
|
|
|
|
|
|
|
|