Scilab : Solving non linear equation with fsolve for dummies
Today I was stuck at solving a non linear equation in scilab for my textbook companion project. Solving linear equations or getting the roots of a polynomial was quite easy, but a non linear equation was a nightmare for me. I googled a lot, asked for friends for help but in vain. But at the end landed here http://www.infoclearinghouse.com/files/scilab/scilab6a.pdf which gave me a little hope.
At the start I didn't got a single word clearly but when read the whole stuff got across a nice function "fsolve" which makes the task very easy.
The function calling sequence is as "[x [,v [,info]]]=fsolve(x0,fct [,fjac] [,tol]) " YUKK what the hell is this ??
Let us look at the right hand side i.e fsolve(x0,fct [,fjac] [,tol]), fsolve takes four parameters namely :
- x0 means initial guess.
- fct means the function whose solution is to found out
- fjac means the jacobian (in case of multi-variable ) or derivative in case of single variable function.
- tol means the tolerance limit of the final answer.
- x : the final answer
- v : value of function at x
- info: how the function terminated
// function defnition
function[f] = F(x)
f = x^3 - 3*x -2;
x22 + x32 -3
Here comes trouble, as we can see that the answer is [ 1 1 1 ] for [x1 x2 x3] but how to do it in scilab.
// Define function as
function[f] = F(x)
f(1) = x(1)^2 +x(2)^2 + x(3)^2 - 3;
f(2) = x(1)*x(2)*x(3) - 1;
f(3) = x(1)^3 + x(2)^2 - 2*x(3)^2;
// Call the function
y = fsolve(x,F);
this will give the approximate answer in this case it will give correct answer
for more accurate answers calculate the Jacobian and supply it as an additional 3rd parameter.
//Jacobian of functions
function[j] = jacob(x)
j(1,1) = 2*x(1); j(1,2) = 2*x(2);j(1,3) = 2*x(3);
j(2,1) = x(2)*x(3); j(2,2) = x(1)*x(3);j(2,3) = x(1)*x(2);
j(3,1) = 3*x(1)^2; j(3,2) = 2*x(2);j(3,3) = -4*x(3);