Scilab : Solving non linear equation with fsolve for dummies
Posted by jaychakra on Monday, 25 June 2012
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.
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.
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 defnition
function[f] = F(x)
f = x^3 - 3*x -2;
endfunction
//initial guess
x = 100;
//Derivative
function[z] = D(x)
z= 3*x^2 - 3;
endfunction
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
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
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
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
// Give an initial guess
x = [2 100 1000];
// 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
[x,v,info]=fsolve(x,F,jacob);
disp(x);
Njoy !!