# [Scilab-users] find a zero of a system of nonlinear functions

8 messages
Open this post in threaded view
|

## [Scilab-users] find a zero of a system of nonlinear functions

 fsolve I find a zero of a system of nonlinear functions: y = 3 - 10 * x + 2 * x ^ 2 + 3 * x ^ 3 in the range x = -3: 0.01: 3 but fsolve returns as a solution a vector (in the scrip yr) of the same size as x. I have varied the precision tolerance h to several values 1.d-3 to% eps. but I always get that infinity of solutions. the only solution that I found is shown in the script. Is it possible to improve this? funcprot(0); function y = poly_cubic(x)  y = 3 - 10*x + 2*x^2 + 3*x^3; endfunction x=-3:0.01:3; clf() plot(x,poly_cubic); disp("-2.3027756 0.3333333 1.3027756  ", "Roots to y = 3 - 10*x + 2*x^2 + 3*x^3") h=%eps; yr=fsolve(x,poly_cubic,h) RR=strtod(unique(string(yr))); RR=gsort(RR,'g','i');//sort the elements of the array RR in the increasing order. disp(RR,"Roots values f(x)") plot(RR,poly_cubic,"ro") -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: find a zero of a system of nonlinear functions

 Hello The first argument in fsolve is not the range of the variable in which the solution is looked for. Instead it is a guessed value: // Start of code function y = poly_cubic(x) y = 3 - 10*x + 2*x^2 + 3*x^3; endfunction   yr=fsolve(1.3,poly_cubic,%eps) // yr  = 1.3027756   yr=fsolve(-2,poly_cubic,%eps) // yr  =  - 2.3027756   yr=fsolve(0,poly_cubic,%eps) // yr  =  0.3333333   // End of code By the way, for polynoms, it is more straightforward to do : p=[3 2 -10 3]; roots(p) // ans  =   - 2.3027756       1.3027756       0.3333333   HTH Denis -----Message d'origine----- De : users [mailto:[hidden email]] De la part de Hermes Envoyé : mardi 28 novembre 2017 11:13 À : [hidden email] Objet : [Scilab-users] find a zero of a system of nonlinear functions fsolve I find a zero of a system of nonlinear functions: y = 3 - 10 * x + 2 * x ^ 2 + 3 * x ^ 3 in the range x = -3: 0.01: 3 but fsolve returns as a solution a vector (in the scrip yr) of the same size as x. I have varied the precision tolerance h to several values 1.d-3 to% eps. but I always get that infinity of solutions. the only solution that I found is shown in the script. Is it possible to improve this? funcprot(0); function y = poly_cubic(x)  y = 3 - 10*x + 2*x^2 + 3*x^3; endfunction x=-3:0.01:3; clf() plot(x,poly_cubic); disp("-2.3027756 0.3333333 1.3027756  ", "Roots to y = 3 - 10*x + 2*x^2 + 3*x^3") h=%eps; yr=fsolve(x,poly_cubic,h) RR=strtod(unique(string(yr))); RR=gsort(RR,'g','i');//sort the elements of the array RR in the increasing order. disp(RR,"Roots values f(x)") plot(RR,poly_cubic,"ro") -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users Denis Crété
Open this post in threaded view
|

## Re: find a zero of a system of nonlinear functions

 I have tried to reproduce what can be read in this document, https://ljk.imag.fr/membres/Bernard.Ycart/mel/ds/ds.pdf  , on page 36.  I find it interesting to introduce a range as an initial value. I use  otras funciones y obtuve los resultados esperados. function y=f(x)     y=exp(-x.^2)*cos(4*x); endfunction function y = B(x)  y = besselj(40,x+cos(x)); endfunction I would have liked to replace this script with the fsolve function: function root=brute_force_root_finder(F, a, b, n)     x = linspace(a, b, n);     n=size(x,"*")-1;     y = feval(x,F);     root = [];     for i=1:n-1         if y(i)*y(i+1) < 0 then             root(\$+i) = x(i) - (x(i+1) - x(i))/(y(i+1) - y(i))*y(i);//root = x[i] - (x[i+1] - x[i])/(y[i+1] - y[i])*y[i]         end     end endfunction Gracias -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: find a zero of a system of nonlinear functions

 Hello, I am not convinced that in the quoted document the first argument is a range: the values returned by the function fsolve are generally not lying in the intended "range"; if you try: fsolve([0.4,0.45,0.5],f)  ans  =    - 1.    6.    228. If I understand correctly, the first argument is considered as a vector, i.e. that the function f takes a vector as argument and returns a vector of the same dimension, with elements sin(2*%pi*xi). In any case, it is quite strange to give 0.5 as initial value for this search of root, as at this particular point f=1 and its slope is zero !!! It explains why very strange values are returned for the roots. HTH Denis -----Message d'origine----- De : users [mailto:[hidden email]] De la part de Hermes Envoyé : mardi 28 novembre 2017 11:59 À : [hidden email] Objet : Re: [Scilab-users] find a zero of a system of nonlinear functions I have tried to reproduce what can be read in this document, https://ljk.imag.fr/membres/Bernard.Ycart/mel/ds/ds.pdf  , on page 36.  I find it interesting to introduce a range as an initial value. I use  otras funciones y obtuve los resultados esperados. function y=f(x)     y=exp(-x.^2)*cos(4*x); endfunction function y = B(x)  y = besselj(40,x+cos(x)); endfunction I would have liked to replace this script with the fsolve function: function root=brute_force_root_finder(F, a, b, n)     x = linspace(a, b, n);     n=size(x,"*")-1;     y = feval(x,F);     root = [];     for i=1:n-1         if y(i)*y(i+1) < 0 then             root(\$+i) = x(i) - (x(i+1) - x(i))/(y(i+1) - y(i))*y(i);//root = x[i] - (x[i+1] - x[i])/(y[i+1] - y[i])*y[i]         end     end endfunction Gracias -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users Denis Crété
Open this post in threaded view
|

## Re: find a zero of a system of nonlinear functions

 -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: find a zero of a system of nonlinear functions

 In reply to this post by Hermes The problem that I present is that at times Scilab stops. And they show the next alert: Gracias -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users