Submitted by jaychakra on

**Solving Non-Linear Equations With Scilab 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

^{3 }- 3x - 2. clearly the solution is 2 as we can see, we have to do it the scilab way.

// function defnition

function[f] = F(x)

f = x^3 - 3*x -2;

endfunction

^{3 }- 3x - 2 as parameter it will give all the three roots namely -1 the double root and 2.

^{2 }+

x2

^{2}+ x3

^{2 }-3

^{ }G(x1,x2,x3) = x1x2x3 -1

^{ }H(x1,x2,x3) = x1

^{3 }+ x2

^{2}- 2x3

^{2}

^{}

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.

No problem

Answer:

// 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;

endfunction

// Call the function

y = fsolve(x,F);

disp(y);

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);

endfunction

Njoy !!

- jaychakra's blog
- Log in or register to post comments