*This eighth article of the mathematical journey through open source, solves non-linear equations using linear algebra in octave.*

Hope you have found out the vegetable prices from the vegetable seller, who had placed various equal priced stacks for sell at ₹30. Recall: One stack had 4 lemons, 7 cucumbers, 9 tomatoes. Another had 2 lemons, 5 cucumbers, 27 tomatoes. And the third had just 9 cucumbers & 15 tomatoes. Prices would be ₹2.00 per lemon, ₹2.50 per cucumber, ₹0.50 per tomato, computed as follows:

```
$ octave -qf
octave:1> N = [
> 4 7 9
> 2 5 27
> 0 9 15
> ];
octave:2> inv(N) * [30; 30; 30]
ans =
2.00000
2.50000
0.50000
octave:3>
```

## Polynomial solving

Note that though in 3 variables, even this was a linear equation. How about solving higher order polynomial equations, meaning of squares, cubes, … of the variables. Say, we want a solution for x in x^{3} + 3x^{2} + 3x + 1 = 0. Simple! First define a function for this polynomial. And, then use the function solver *fsolve()* to solve it, as follows:

```
$ octave -qf
octave:1> function y = f(x)
> y = x^3 + 3*x^2 + 3*x + 1;
> endfunction
octave:2> [x, fval, info] = fsolve(@f, 0)
x = -0.99999
fval = 0
info = 1
octave:3>
```

This indicates the value of x as -0.99999 ≈ -1 as the solution to the function f(x), yielding a function value of 0, with info = 1 indicating that solution is obtained. And you may verify the answer by calling the function f with the variable x as f(x) on the *octave* prompt. The second parameter in *fsolve()* is the initial guess of the solution.

## Geometry solving

With the power in hand, why not solve more complex geometric problems? Last time we found the intersection point of two straight lines. How about intersection of a straight line and a circle? Let us have the following straight line and circle, defined in the Cartesian coordinate system, i.e. the x-y system:

4x + 3y = 24

x^{2} + y^{2} = 25

To be able to solve it using *fsolve()*, let’s consider the different variables *x* & *y* as fields of a vector *X*, say *x* as *X(1)*, *y* as *X(2)*. Then, the equations can be re-written as follows:

4 * X(1) + 3 * X(2) = 24

X(1)^2 + X(2)^2 = 25

and hence could be solved using *fsolve()* as follows:

```
$ octave -qf
octave:1> function Y = F(X)
> Y(1) = 4 * X(1) + 3 * X(2) - 24;
> Y(2) = X(1)^2 + X(2)^2 - 25;
> endfunction
octave:2> [Y, Fval, info] = fsolve(@F, [0; 0])
warning: matrix singular to machine precision, rcond = 0
warning: attempting to find minimum norm solution
warning: dgelsd: rank deficient 2x2 matrix, rank = 1
Y =
3.0000
4.0000
Fval =
0.0000e+00 2.6691e-07
info = 1
octave:3>
```

So, (3, 4) is the intersecting point – can be verified by substituting back into the above equations.

## Solve it

Equipped with this knowledge, here’s a couple of teasers for your brain:

- Find three numbers, product of which is 60; sum of their squares is 50; and their sum is 12.
- A sage came to a temple with some flowers and dipped all of them into the first magical pond of the temple and got those back, squared. Then, he offered some of those flowers in the temple and dipped the remaining flowers into the second magical pond to get those back, doubled. Then, he again offered the same number of flowers, as offered earlier, and dipped the remaining flowers into the third magical pond to get those back, tripled, which he took back with him as prasadam. Now, the number of flowers he took back with him, is same as in each one of his offerings. Also, what he took back with him is thrice the number of flowers he came with to the temple. How many flowers did he come in with?

If you think, you have got the *octave* code for solving the above, you may post the solution in the comments below. And as we move on, we would get into specifically playing with polynomials.