# Polynomial

## Overview

The `Polynomial` class provides a framework for representing and manipulating polynomials in one variable. It supports essential mathematical operations such as addition, multiplication, differentiation, and evaluation. The class is designed for single-threaded use.

## Functionality

This class enables the addition of polynomials or individual terms, allowing for the construction of complex polynomial expressions. It supports multiplication with other polynomials or individual terms, making it versatile for algebraic operations. Additionally, users can find roots of a polynomial using Newton's method, which provides precision control through configurable error margins and iteration limits.

The class also offers methods to compute and retrieve the derivative of a polynomial. The derivative can be further evaluated at specific points to analyze its behavior. Users can reset the polynomial to zero when needed, providing a clean state for new calculations.

## Methods Summarized

| Type                                                                                | Name                                                                             | Summary                                                                                                                   |
| ----------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| void                                                                                | [addPolynomial(polynomial)](#addpolynomial-polynomial)                           | Adds another polynomial to this polynomial.                                                                               |
| void                                                                                | [addTerm(coefficient, exponent)](#addterm-coefficient-exponent)                  | Adds a term to this polynomial.                                                                                           |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)                            | [findRoot(startValue, error, iterations)](#findroot-startvalue-error-iterations) | Finds a root of this polynomial using Newton's method, starting from an initial search value, and with a given precision. |
| [Polynomial](/reference/servoyextensions/server-plugins/amortization/polynomial.md) | [getDerivative()](#getderivative)                                                | Returns a polynomial that holds the derivative of this polynomial.                                                        |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)                            | [getDerivativeValue(x)](#getderivativevalue-x)                                   | Returns the value of the derivative of this polynomial in a certain point.                                                |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)                            | [getValue(x)](#getvalue-x)                                                       | Returns the value of this polynomial in a certain point.                                                                  |
| void                                                                                | [multiplyByPolynomial(polynomial)](#multiplybypolynomial-polynomial)             | Multiplies this polynomial with another polynomial.                                                                       |
| void                                                                                | [multiplyByTerm(coefficient, exponent)](#multiplybyterm-coefficient-exponent)    | Multiples this polynomial with a term.                                                                                    |
| void                                                                                | [setToZero()](#settozero)                                                        | Sets this polynomial to zero.                                                                                             |

## Methods Detailed

### addPolynomial(polynomial)

Adds another polynomial to this polynomial.

**Parameters**

* [Polynomial](/reference/servoyextensions/server-plugins/amortization/polynomial.md) **polynomial** ;

**Returns:** void

**Sample**

```js
// (x+1) + 2*(x+1)*x + 3*(x+1)*x^2 + 4*(x+1)*x^3
var eq = plugins.amortization.newPolynomial();
for (var i = 0; i < 4; i++)
{
	var base = plugins.amortization.newPolynomial();
	base.addTerm(1, 1);
	base.addTerm(1, 0);
	base.multiplyByTerm(1, i);
	base.multiplyByTerm(i + 1, 0);
	eq.addPolynomial(base);
}
application.output(eq.getValue(2));
```

### addTerm(coefficient, exponent)

Adds a term to this polynomial.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **coefficient** ;
* [Number](/reference/servoycore/dev-api/js-lib/number.md) **exponent** ;

**Returns:** void

**Sample**

```js
// (x+1) + 2*(x+1)*x + 3*(x+1)*x^2 + 4*(x+1)*x^3
var eq = plugins.amortization.newPolynomial();
for (var i = 0; i < 4; i++)
{
	var base = plugins.amortization.newPolynomial();
	base.addTerm(1, 1);
	base.addTerm(1, 0);
	base.multiplyByTerm(1, i);
	base.multiplyByTerm(i + 1, 0);
	eq.addPolynomial(base);
}
application.output(eq.getValue(2));
```

### findRoot(startValue, error, iterations)

Finds a root of this polynomial using Newton's method, starting from an initial search value, and with a given precision.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **startValue** ;
* [Number](/reference/servoycore/dev-api/js-lib/number.md) **error** ;
* [Number](/reference/servoycore/dev-api/js-lib/number.md) **iterations** ;

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) The root value after the specified number of iterations or as soon as the error condition is satisfied.\
Returns Double.NaN if the derivative was zero during the computation.

**Sample**

```js
// Model the quadratic equation -x^2 + 4x + 0.6 = 0
var eq = plugins.amortization.newPolynomial();
eq.addTerm(-1, 2);
eq.addTerm(4, 1);
eq.addTerm(0.6, 0);
// Find the roots of the equation.
r1 = eq.findRoot(100, 1E-5, 1000);
r2 = eq.findRoot(-100, 1E-5, 1000);
application.output("eq(" + r1 + ")=" + eq.getValue(r1));
application.output("eq(" + r2 + ")=" + eq.getValue(r2));
// Find the minimum/maximum point by zeroing the first derivative.
var deriv = eq.getDerivative();
rd = deriv.findRoot(0, 1E-5, 1000);
application.output("Min/max point: " + rd);
application.output("Min/max value: " + eq.getValue(rd));
if (deriv.getDerivativeValue(rd) < 0) application.output("Max point.");
else application.output("Min point.");
```

### getDerivative()

Returns a polynomial that holds the derivative of this polynomial.

**Returns:** [Polynomial](/reference/servoyextensions/server-plugins/amortization/polynomial.md) A polynomial representing the derivative of this polynomial.

**Sample**

```js
// Model the quadratic equation -x^2 + 4x + 0.6 = 0
var eq = plugins.amortization.newPolynomial();
eq.addTerm(-1, 2);
eq.addTerm(4, 1);
eq.addTerm(0.6, 0);
// Find the roots of the equation.
r1 = eq.findRoot(100, 1E-5, 1000);
r2 = eq.findRoot(-100, 1E-5, 1000);
application.output("eq(" + r1 + ")=" + eq.getValue(r1));
application.output("eq(" + r2 + ")=" + eq.getValue(r2));
// Find the minimum/maximum point by zeroing the first derivative.
var deriv = eq.getDerivative();
rd = deriv.findRoot(0, 1E-5, 1000);
application.output("Min/max point: " + rd);
application.output("Min/max value: " + eq.getValue(rd));
if (deriv.getDerivativeValue(rd) < 0) application.output("Max point.");
else application.output("Min point.");
```

### getDerivativeValue(x)

Returns the value of the derivative of this polynomial in a certain point.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **x** ;

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) The value of the derivative of this polynomial at the specified point.

**Sample**

```js
// Model the quadratic equation -x^2 + 4x + 0.6 = 0
var eq = plugins.amortization.newPolynomial();
eq.addTerm(-1, 2);
eq.addTerm(4, 1);
eq.addTerm(0.6, 0);
// Find the roots of the equation.
r1 = eq.findRoot(100, 1E-5, 1000);
r2 = eq.findRoot(-100, 1E-5, 1000);
application.output("eq(" + r1 + ")=" + eq.getValue(r1));
application.output("eq(" + r2 + ")=" + eq.getValue(r2));
// Find the minimum/maximum point by zeroing the first derivative.
var deriv = eq.getDerivative();
rd = deriv.findRoot(0, 1E-5, 1000);
application.output("Min/max point: " + rd);
application.output("Min/max value: " + eq.getValue(rd));
if (deriv.getDerivativeValue(rd) < 0) application.output("Max point.");
else application.output("Min point.");
```

### getValue(x)

Returns the value of this polynomial in a certain point.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **x** ;

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) The value of this polynomial at the specified point.

**Sample**

```js
// Model the quadratic equation -x^2 + 4x + 0.6 = 0
var eq = plugins.amortization.newPolynomial();
eq.addTerm(-1, 2);
eq.addTerm(4, 1);
eq.addTerm(0.6, 0);
// Find the roots of the equation.
r1 = eq.findRoot(100, 1E-5, 1000);
r2 = eq.findRoot(-100, 1E-5, 1000);
application.output("eq(" + r1 + ")=" + eq.getValue(r1));
application.output("eq(" + r2 + ")=" + eq.getValue(r2));
// Find the minimum/maximum point by zeroing the first derivative.
var deriv = eq.getDerivative();
rd = deriv.findRoot(0, 1E-5, 1000);
application.output("Min/max point: " + rd);
application.output("Min/max value: " + eq.getValue(rd));
if (deriv.getDerivativeValue(rd) < 0) application.output("Max point.");
else application.output("Min point.");
```

### multiplyByPolynomial(polynomial)

Multiplies this polynomial with another polynomial.

**Parameters**

* [Polynomial](/reference/servoyextensions/server-plugins/amortization/polynomial.md) **polynomial** ;

**Returns:** void

**Sample**

```js
// Model the quadratic equation (x+1)*(x+2) = 0
var eq = plugins.amortization.newPolynomial();
eq.addTerm(1, 1);
eq.addTerm(1, 0);
var eq2 = plugins.amortization.newPolynomial();
eq2.addTerm(1, 1);
eq2.addTerm(2, 0);
eq.multiplyByPolynomial(eq2);
// Find the roots of the equation.
r1 = eq.findRoot(100, 1E-5, 1000);
r2 = eq.findRoot(-100, 1E-5, 1000);
application.output("eq(" + r1 + ")=" + eq.getValue(r1));
application.output("eq(" + r2 + ")=" + eq.getValue(r2));
```

### multiplyByTerm(coefficient, exponent)

Multiples this polynomial with a term.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **coefficient** ;
* [Number](/reference/servoycore/dev-api/js-lib/number.md) **exponent** ;

**Returns:** void

**Sample**

```js
// (x+1) + 2*(x+1)*x + 3*(x+1)*x^2 + 4*(x+1)*x^3
var eq = plugins.amortization.newPolynomial();
for (var i = 0; i < 4; i++)
{
	var base = plugins.amortization.newPolynomial();
	base.addTerm(1, 1);
	base.addTerm(1, 0);
	base.multiplyByTerm(1, i);
	base.multiplyByTerm(i + 1, 0);
	eq.addPolynomial(base);
}
application.output(eq.getValue(2));
```

### setToZero()

Sets this polynomial to zero.

**Returns:** void

**Sample**

```js
var eq = plugins.amortization.newPolynomial();
eq.addTerm(2, 3);
application.output(eq.getValue(1.1));
eq.setToZero();
application.output(eq.getValue(1.1));
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoyextensions/server-plugins/amortization/polynomial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
