javascriptpythonmathpolynomial-mathcalculus# Find the tangent of any polynomial function at x

**Problem:**

I'm looking for a catch-all function that I can use to calculate the tangent of **any** polynomial function at x. I'm indifferent to the language used although JavaScript or Python would be prefered! I should be able to pass in any x value and an array of coefficients in the format, *a + bx + cx^2 + dx^3* ... and so on.

**Example function format:**

```
function findTangent(x, coefficients) {
// Do differential calculus here.
return [tangentIntercept, tangentSlope]
}
```

**Example function test:**

Say I have the function, *y = 2 + 7x + 5x^2 + x^3* and I want to to find the tangent at, *x = -2*. I could call this function like so, `findTangent(-2, [2, 7, 5, 1])`

and get a return value like this, `[-2, -1]`

representing the tangent, *y = -2 - x*.

**Notes:**

I have looked for an answer on the Math Stackexchange and Google search but all the results are in a mathematical syntax and not code. I want a programmatic solution, I am far more comfortable with loops and if statements than funny symbols and math jargon!

Solution

Okay so after a day of struggling with it I think I have got the solution in both JavaScript **and** Python!

```
function findTangent(x, coefficients) {
let slope = 0
let intercept = coefficients[0]
for (let i = 1; i < coefficients.length; i++) {
slope += coefficients[i] * i * Math.pow(x, i - 1)
intercept += coefficients[i] * Math.pow(x, i)
}
return [intercept - slope * x, slope]
}
```

```
def find_tangent(x, coefficients):
slope = 0
intercept = coefficients[0]
for i, coefficient in enumerate(coefficients):
if i != 0:
slope += coefficient * i * pow(x, i - 1)
intercept += coefficient * pow(x, i)
return [intercept - slope * x, slope]
```

I have tested the results against the Symbolab Tangent Calculator and they seem to be okay but **please let me know if you find any errors!** Also, I would love to see results in other languages so if you have a prefered language that's not mentioned here don't hesitate to post!

- How not to overwrite user data in FireStore for returning user when login using Angularifre?
- using spread operator in typescript
- javascript postMessage not working
- Is there a way to add/remove several classes in one single instruction with classList?
- Appending .js extension on relative import statements during Typescript compilation (ES6 modules)
- How do I "preventDefault" for custom events
- React Hooks useState+useEffect+event gives stale state
- How to check if a string ONLY contains specific strings
- How to prevent changes to a prototype?
- Creating a textarea with auto-resize
- Javascript file upload - PDF to image(s)
- Truncate number to two decimal places without rounding
- How browsers store data for autocomplete and where?
- Debt Snowball Function
- JSON Web Token (JWT) Error: Invalid Signature with RSA Key Pairs
- What a RegEx that can match text in parentheses with nested parentheses
- How would I get the full response from this Axios request?
- next js - the style of the tailwind class i get from API doesnt apply on my element
- How can I disable sort on a specific element?
- Is it a bad idea to use the meta tag in HTML to specify custom metadata for retrieval with JavaScript?
- Detecting when user scrolls to bottom of div with React js
- Mongoose partial update of an object
- in javascript, how do you sort a subset of an array?
- Firebase email link authentication leads to a page that says "Error encountered" - "The selected page mode is invalid"
- How to fire window.scrollY only one time
- How to call a method from child component to parent one in vue2?
- Switch value for clicked button from group of buttons in jQuery
- Smooth scroll to specific div on click
- How to use MaterializeCss with Vue.js?
- Can't find variable: React