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

classic Classic list List threaded Threaded
8 messages Options
Hermes Hermes
Reply | Threaded
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
Denis Crété Denis Crété
Reply | Threaded
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é
Hermes Hermes
Reply | Threaded
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
<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
Denis Crété Denis Crété
Reply | Threaded
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
<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é
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: find a zero of a system of nonlinear functions

Hermes Hermes
Reply | Threaded
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:

<http://mailinglists.scilab.org/file/t497622/Scilab_has_stopped.jpg>
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
Denis Crété Denis Crété
Reply | Threaded
Open this post in threaded view
|

Re: find a zero of a system of nonlinear functions

This is a strong indication that Scilab is trying to solve a problem in the 401-dimension space (as opposed to solving 401 equations in 1D) when you give the vector 0:0.01:4 as first argument, as I suspected in my previous message.
In addition, I stress the point that looking at the result returned by fsolve, the roots are not contained in the so-called "range" that you think you define in the first argument of fsolve: either fsolve has a bug, or the first argument you give is NOT considered as a range, whatever expression you use.
Best regards
Denis

-----Message d'origine-----
De : users [mailto:[hidden email]] De la part de Hermes
Envoyé : mardi 28 novembre 2017 13:27
À : [hidden email]
Objet : Re: [Scilab-users] find a zero of a system of nonlinear functions

The problem that I present is that at times Scilab stops. And they show the next alert:

<http://mailinglists.scilab.org/file/t497622/Scilab_has_stopped.jpg>
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é
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: find a zero of a system of nonlinear functions

In reply to this post by Denis Crété
Le 28/11/2017 à 12:30, CRETE Denis a écrit :
> 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).
You are all right, Denis.
But this feature could actually be used to parallelize computations of
several roots, provided that the function is well vectorized. But it's
not the case: there is a mistake:
Le 28/11/2017 à 11:58, Hermes a écrit :
> .../...
> function y=f(x)
>      y=exp(-x.^2)*cos(4*x);
> endfunction
.* cos() should be used instead of *cos()

Le 28/11/2017 à 11:12, Hermes a écrit :
> .../...
> function y = poly_cubic(x)
>   y = 3 - 10*x + 2*x^2 + 3*x^3;
> endfunction

This one is definitely not vectorized. .^should be used instead of ^.

HTH
Samuel

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users