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.pdfwhich 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.
Parameters 1 and 2 are necessary but the parameters 3 and 4 may be given for better approximation in the answer.
Now let's have a look at the LHS, the output side [x [,v [,info]]], it has 3 outputs namely:
x : the final answer
v : value of function at x
info: how the function terminated
For almost correct answer the value of info should be 1, LHS all the parameters are optional
Let's jump to some problem solving.
Q1. Solve F(x) = x3 - 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
x = 100;
function[z] = D(x)
z= 3*x^2 - 3;
y = fsolve(x,F, D);
disp(y); // Will display answer as 2
this problem can be easily solved by making use of the function "roots(polynomial)" giving polynomial x3 - 3x - 2 as parameter it will give all the three roots namely -1 the double root and 2.
Q2 Solve :
F(x1,x2,x3) = x12 + x22 + x32 -3
G(x1,x2,x3) = x1x2x3 -1
H(x1,x2,x3) = x13 + x22 - 2x32
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