I'm creating logarithmic trendlines with the help of an Excel algorithm. However, I am unable to calculate the slope and intercept values as done in Excel. I believe my formula contained several errors.

This is my code

``` var X= [10, 25, 30, 40]; Y= [5, 4, 7, 12];

var Slope, Intercept, SX = 0, SY = 0,
SXX = 0, SXY = 0, SYY = 0,
SumProduct = 0, N = X.length;

for (var i = 0; i < N; i++) {
SX = SX + X[i];
SY = SY + Y[i];
SXY = SXY + X[i] * Y[i];
SXX = SXX + X[i] * X[i];
SYY = SYY + Y[i] * Y[i];
}

Slope = ((N * SXY) - (SX * SY)) / ((N * SXX) - (SX * SX));

Intercept = (SY - (Slope * SX)) / N;
```

Excel Formula:

Logarithmic Equation: y=(c*LN(x))+b

where:

```c = INDEX(LINEST(y,LN(x)),1)
b = INDEX(LINEST(y,LN(x)),1,2)
```

Screenshot for Excel output Please suggest how to derive the Excel formula in JavaScript.

Nov 8, 2022 in Others 622 views

## 1 answer to this question.

You missed Math.log() for ln() in Excel. Edit for parts like this.

```for (var i = 0; i < N; i++) {
SX = SX + Math.log(X[i]);
SY = SY + Y[i];
SXY = SXY + Math.log(X[i]) * Y[i];
SXX = SXX + Math.log(X[i]) * Math.log(X[i]);
SYY = SYY + Y[i] * Y[i];
}

```

I've verified the output is the same as Excel.

```> Slope

3.8860409979365333

> Intercept

-5.252238189415747```
• 63,160 points

## How is it possible for DynamoDB to support both Key-Value and Document database properties at the same time

As per DynamoDB's documentation, it supports both ...READ MORE

## Excel formula for searching two text in one cell and return values based on the result

You can include a second IF within ...READ MORE

## Create Excel file and save as PDF.

Office 2013 still has the Interop library and ...READ MORE

## How do you calculate the Quintile for groups of rows in Excel?

Use this formula: =MAX(1,ROUNDUP(10*PERCENTRANK(\$C:\$C,\$C2,4),0)) To divide into whichever many ...READ MORE

## Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

## Remove formulas from all worksheets in Excel using VBA

Try this : Option Explicit Sub test1() ...READ MORE

## Calculate monthly average from daily data without PivotTable

Assuming you have the months in column D enter ...READ MORE

## Automate compound annual growth rate (CAGR) calculation

The following PowerPivot DAX formulas worked for ...READ MORE