# Solve functions (Xsolve)

Spark solver to perform a *What-If Analysis* similar to [Goal Seek](https://support.microsoft.com/en-us/office/use-goal-seek-to-find-the-result-you-want-by-adjusting-an-input-value-320cb99e-f4a4-417f-b1c3-4f369d6e66c7) at each API call. This is useful to determine the needed value to achieve a target input. For example, Goal Seek could be used to determine the how long it would take pay off a certain loan amount.

## Map the solve

Syntax: `Xsolve_<parameter>`

To build a solve we suggest to use the [Coherent Assistant](https://docs.coherent.global/assistant/) has a [Solver](/assistant/solver/creating-a-solver.md) feature to create, manage and trace solves or start with the [#example-of-xsolve-mapping](#example-of-xsolve-mapping "mention") sample file.

Users can build a simple solver that works similar to Excel’s Goal seek function in order to perform a *What-If Analysis*. This solver is recognized by Spark and returns the forecasted value.

* For example, to reach a specific goal for the amount of annual premium paid by changing the sum assured. This is an effective method to find out how much an individual can be assured for, with a premium constraint.
* If a Spark Service has multiple solves, they will be executed in alphabetical order.

{% hint style="info" %}
Ensure that thorough testing is performed on the domain of inputs to ensure that the `Xsolve` is providing the correct calculations.
{% endhint %}

## Adjust the solve parameters

| Key                | Value                                                                                                                                                                                                                                                                                                                                                            |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Run if`           | A `TRUE` or `FALSE` value indicating whether or not this solve should be executed.                                                                                                                                                                                                                                                                               |
| `Target cell`      | <p>A link to the formulated cell that needs to reach the <code>Target value</code>. This is generally recommended to be defined as a difference between the desired <code>Target value</code> of <code>0</code>.<br><br>This must be a link to a cell on the same worksheet or a Named Range.<br><br>Equivalent to Goal Seek <em>Set cell</em>.</p>              |
| `Target value`     | <p>The value of the <code>Target cell</code> to achieve. This is best set to <code>0</code>.<br><br>Equivalent to Goal Seek <em>Target to value</em>.</p>                                                                                                                                                                                                        |
| `By changing`      | <p>A link to the cell that has to change in order to for the <code>Target cell</code> to reach the <code>Target value</code>.<br><br>This must be a link to a cell on the same worksheet or a Named Range.<br><br>Equivalent to  Goal Seek <em>By changing cell</em>.</p>                                                                                        |
| `Solve algorithm`  | The available options are `SECANT` and `BRENT`. We recommend using `BRENT` as it is able to solve more complex workbooks accurately.                                                                                                                                                                                                                             |
| `Max change`       | Maximum acceptable value of `\|Solve result - Target value\|` within `Max iterations`, default `1`.                                                                                                                                                                                                                                                              |
| `Max iterations`   | Maximum iterations for the number of solve, default `25`.                                                                                                                                                                                                                                                                                                        |
| `Initial guess`    | Provide an initial guess to help reach the `Target value` sooner.                                                                                                                                                                                                                                                                                                |
| `Lower bound`      | For the `BRENT`algorithm, the bounds of the solve can be defined at the start of the solve. When this is provided, the `Initial guess` is ignored.                                                                                                                                                                                                               |
| `Upper bound`      | For the `BRENT`algorithm, the bounds of the solve can be defined at the start of the solve. When this is provided, the `Initial guess` is ignored.                                                                                                                                                                                                               |
| `Solve started`    | <p><code>TRUE</code> or <code>FALSE</code> value.<br>Spark will write this into the cell during execution. This enables downstream calculations to use this value.</p>                                                                                                                                                                                           |
| `Solve target`     | <p>This is provided in order to help assess how close the solve was in reaching the target, given the solve can conclude within the <code>Max change</code>.<br>Spark will write this into the cell during execution. This enables downstream calculations to use this value.</p>                                                                                |
| `Solve iteration`  | Spark will write this into the cell during execution. This enables downstream calculations to use this value.                                                                                                                                                                                                                                                    |
| `Solve result`     | <p>This will contain either the successful solve value or <code>#N/A</code>.<br>Spark will write this into the cell during execution. This enables downstream calculations to use this value.</p>                                                                                                                                                                |
| `Solve successful` | <p><code>TRUE</code> or <code>FALSE</code> value.<br>Spark will write this into the cell during execution.<br><br>This can be useful in taking the initial solve result and applying a transformation on the solved result. For example, <code>IF</code> the solve is successful, take the <code>By changing</code> value and <code>ROUND</code> the result.</p> |

## Errors

* `#CALC!` is returned for the `Solve result` if:
  * The `Target value`, `Initial guess`, `Max change`, or `Max iterations` are not a proper number.
  * The maximum iterations are achieved.
* `#NUM!` will be returned for the `Solve result` if the solve algorithm encounters a zero slope error.

## Trace solve iterations

The `Xsolve` iterations can be shown by using the [Execute API (v3)](/spark-apis/execute-api/execute-api-v3.md) [Execute API (v3)](/spark-apis/execute-api/execute-api-v3.md#request_meta) `debug_solve`.

## Example of `Xsolve` mapping

This service can be uploaded to Spark to use as a simple example. It does not include all of the optional parameters that are described above.

{% file src="/files/BVVKMiGujP4sliANb8tH" %}

## Solve algorithm reference

### `BRENT`

The implementation aligns with the [Brent's method](https://en.wikipedia.org/wiki/Brent%27s_method). The Coherent implementation will bound the independent variable between `0` and `Initial guess * 100`. This can be overridden using the `Lower bound`and `Upper bound`parameters.

### `SECANT`

The implementation is inspired by the [Secant method](https://en.wikipedia.org/wiki/Secant_method). The attached Excel file traces how the algorithm determines a root for a cubic equation.

{% file src="/files/H118aRjZ8GS7Ro9hmVet" %}


---

# 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.coherent.global/build-spark-services/other-mapping-options/solve-functions.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.
