I have 2 arrays of equal length. The following function attempts to calculate the slope using these arrays. It returns the average of the slope between each points. For the following data set, I seem to be getting different values than Excel and Google Docs.
double[] x_values = { 1932, 1936, 1948, 1952, 1956, 1960, 1964, 1968,
1972, 1976, 1980 };
double[] y_values = { 197, 203, 198, 204, 212, 216, 218, 224, 223, 225,
236 };
public static double getSlope(double[] x_values, double[] y_values)
throws Exception {
if (x_values.length != y_values.length)
throw new Exception();
double slope = 0;
for (int i = 0; i < (x_values.length - 1); i++) {
double y_2 = y_values[i + 1];
double y_1 = y_values[i];
double delta_y = y_2 - y_1;
double x_2 = x_values[i + 1];
double x_1 = x_values[i];
double delta_x = x_2 - x_1;
slope += delta_y / delta_x;
}
System.out.println(x_values.length);
return slope / (x_values.length);
}
Output
Google: 0.755
getSlope(): 0.962121212121212
Excel: 0.7501
The slope formula is used to calculate the steepness of the incline of a line with a curve. The x and y coordinate values of the lines are used for calculating the slope of the lines. In this topic, the student will learn about slope and slope formula. Let us start learning! 1.1 What is the Slope? What is the Slope?
In coordinate geometry finding the slope of a straight line is very important computation. In geometrical problem solving it is essential. The slope formula is used to calculate the steepness of the incline of a line with a curve. The x and y coordinate values of the lines are used for calculating the slope of the lines.
The slope of a linear regression line is the vertical distance/the horizontal distance between any of the two points on this line. The Slope function returns the slope of a regression line based on the data points recognized by known_y_values and known_x_values SLOPE Formula in Excel
When you have 2 points on a line on a graph the slope is the change in y divided by the change in x. The slope of a line is a measure of how steep it is.
I bet the other two methods are computing the least-squares fit, whereas you are not.
When I verify this conjecture using R, I too get the slope of about 0.755:
> summary(lm(y~x))
Call:
lm(formula = y ~ x)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.265e+03 1.793e+02 -7.053 5.97e-05 ***
x 7.551e-01 9.155e-02 8.247 1.73e-05 ***
The relevant number is the 7.551e-01
. It is also worth noting that the line has an intercept of about -1265.
Here is a picture of the least-squares fit:
As to implementing this in your code, see Compute least squares using java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With