What i need is to draw a X-Y graph and a line across the X-Y, a slope
accoring to a 20 points. the line should pass the points that create
this line, The least-square.
and they are y=mx+b
where m=(the sum of the products of the corresponding x and y values)-
(the sum of the x values)*(the average of y values)/(the sum of the
square of x values)-(the sum of the x values)*(the average of the x
values)
Also,
b is the slope, which is definde as b=( the average of y values)-m*
(the average of the x values)
I have found the m and i used this an dit did work
public class SquaresFit
{
public static void main(String[] args)
{
double [] a = { -4.91 , -3.84 , -2.41 , -2.62 ,-3.78 , -0.52 , -
1.83 , -2.01 , 0.28 , 1.08 , - 0.94 , 0.59 , 0.69 , 3.04 ,
1.01 , 3.6 , 4.53 , 5.13 , 4.43 , 4.12 };
// Insert values to array
double [] b = { -8.18 , -7.49 , -7.11 , -6.15 , -5.62 , -3.3 , -
2.05 , -2.83 , -1.16 , 0.52 , 0.21 , 1.73 , 3.96 , 4.26 ,
5.75 ,6.67 , 7.7 , 7.31 , 9.05 , 10.5 };
double sum_x = 0;
double sum_y = 0; // Declared variables
double sqr_x = 0;
double sum_xy = 0;
double m;
int i;
for ( i=0 ; i<=19 ; i++) // Loop to find sum of x&y , x*y
and square of x.
{
sum_x += a[i]; // sum_x = sum_x + a[i];
sum_y += b
[i]; // .
sum_xy += a[i] * b
[i]; // .
sqr_x += a[i] * a
[i]; // .
}
System.out.println("The sum of X : "
+ sum_x);
System.out.println("The sum of Y : "
+ sum_y); // Print values of elements.
System.out.println("The sum of Square X : "
+ sqr_x);
System.out.println("The sum of X*Y : "
+sum_xy);
double avg_x = sum_x /5 ; // To
find the average of X & Y.
double avg_y = sum_y /5 ;
System.out.println("The average of
X : " + avg_x);
System.out.println("The
average of Y : " + avg_y); // Print the averages.
m = (sum_xy - (sum_x * avg_y)) / (sqr_x -
(sum_x * avg_x)); // Culculate the last operation
// to test the method.
System.out.println("The result : "+ m); // the
result is = 1.818
}
So, what i need now is to find the "y" and the "b", which i will try
it. But, how can i use the APPLETS in this methods ? and how woudl i
draw the graph?
what classes i shoudl use, if you coudl give me hints, i would be
pleased.