# [Scilab-users] Nested function definition

11 messages
Open this post in threaded view
|

## [Scilab-users] Nested function definition

 Hello, I would like some help with defining a nested function like below: URQ=urq(q,eta(q),zeta(q,eta(q))) The definitions are: eta=mg./(2*q) zeta=asinh(mg.*h./(2*q.*l.*sinh(eta)))+eta URQ=mg.*L0./(q.*l)-sinh(2*eta-zeta)-sinh(zeta) Values of mg, h, l, L0 are given. I would be very grateful for help as I tried different options but none of them is correct. Kind regards, Iza _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 global mg h l L0; mg = 0.1; h=0.1; l=0.1; L0=0.1; function e=eta(q)     e= mg./(2*q) endfunction function z=zeta(q)     z= asinh(mg.*h./(2*q.*l.*sinh(eta(q))))+eta(q); endfunction function y=URQ(q, e, z)     y= mg.*L0./(q.*l)-sinh(2*e-z)-sinh(z) endfunction   q=1; y=URQ(q,eta(q),zeta(q))   Rgds, Rafael _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 Hello, Le 09/04/2019 à 20:17, Rafael Guerra a écrit : global mg h l L0; The above line is useless no ? mg = 0.1; h=0.1; l=0.1; L0=0.1; function e=eta(q)     e= mg./(2*q) endfunction function z=zeta(q)     z= asinh(mg.*h./(2*q.*l.*sinh(eta(q))))+eta(q); endfunction function y=URQ(q, e, z)     y= mg.*L0./(q.*l)-sinh(2*e-z)-sinh(z) endfunction   q=1; y=URQ(q,eta(q),zeta(q))   Rgds, Rafael ```_______________________________________________ users mailing list [hidden email] https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users ``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 Thank you. Is there a way to use this formulation as an input function to fsolve? I don't know how to do it. In my another example I also had a nested function but I managed to define it in a form like in help page about function. It means that everything was inside one function. This formulation worked fine in fsolve, but with my current example I am not able to use it. Iza W dniu 09.04.2019 20:23, Stéphane Mottelet napisał(a): > Hello, > Le 09/04/2019 à 20:17, Rafael Guerra a écrit : > >> * >> >> global mg h l L0; > > The above line is useless no ? > >> mg = 0.1; h=0.1; l=0.1; L0=0.1; >> >> function E=eta(Q) >> >> E= mg./(2*Q) >> >> endfunction >> >> function Z=zeta(Q) >> >> Z= asinh(mg.*h./(2*Q.*l.*sinh(eta(Q))))+eta(Q); >> >> endfunction >> >> function Y=URQ(Q, E, Z) >> >> Y= mg.*L0./(Q.*l)-sinh(2*E-Z)-sinh(Z) >> >> endfunction >> >> q=1; >> >> y=URQ(q,eta(q),zeta(q)) >> >> Rgds, >> >> Rafael >> >> _______________________________________________ >> users mailing list >> [hidden email] >> > https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users> _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 In your example, why not using a simpler URQ function definition into fsolve? For example: mg = 0.1; h=0.1; l=0.1; L0=0.1; function y=URQ(q)     e = mg./(2*q);     z= asinh(mg.*h./(2*q.*l.*sinh(e)))+e;     y= mg.*L0./(q.*l)-sinh(2*e-z)-sinh(z) endfunction q=1; y=URQ(q) Rgds Rafael -----Original Message----- From: users <[hidden email]> On Behalf Of Izabela Wójcik-Grzaba Sent: Wednesday, April 10, 2019 3:01 PM To: Users mailing list for Scilab <[hidden email]> Subject: Re: [Scilab-users] Nested function definition Thank you. Is there a way to use this formulation as an input function to fsolve? I don't know how to do it. In my another example I also had a nested function but I managed to define it in a form like in help page about function. It means that everything was inside one function. This formulation worked fine in fsolve, but with my current example I am not able to use it. Iza _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 Ok, so why nested function in help is so complicated: //nested functions definition function y=foo(x)     a=sin(x)     function y=sq(x), y=x^2,endfunction     y=sq(a)+1 endfunction foo(%pi/3) Couldn't it be formulated like below: function y1=foo1(x)     a=sin(x);     y1=a^2+1; endfunction foo1(%pi/3) That's why I had problems with my functions and couldn't understand why it has to be so complicated. Thank you once more. Iza W dniu 10.04.2019 14:10, Rafael Guerra napisał(a): > In your example, why not using a simpler URQ function definition into > fsolve? > For example: > > mg = 0.1; h=0.1; l=0.1; L0=0.1; > function y=URQ(q) >     e = mg./(2*q); >     z= asinh(mg.*h./(2*q.*l.*sinh(e)))+e; >     y= mg.*L0./(q.*l)-sinh(2*e-z)-sinh(z) > endfunction > > q=1; > y=URQ(q) > > Rgds > Rafael > > -----Original Message----- > From: users <[hidden email]> On Behalf Of Izabela > Wójcik-Grzaba > Sent: Wednesday, April 10, 2019 3:01 PM > To: Users mailing list for Scilab <[hidden email]> > Subject: Re: [Scilab-users] Nested function definition > > Thank you. Is there a way to use this formulation as an input function > to fsolve? I don't know how to do it. > > In my another example I also had a nested function but I managed to > define it in a form like in help page about function. It means that > everything was inside one function. This formulation worked fine in > fsolve, but with my current example I am not able to use it. > > Iza > > > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 I think the simple examples serve to show the syntax only, and do not explain its utility. The nested function definition will be useful if called many times within a lengthy function, or just for sake of code clarity. Regards, Rafael -----Original Message----- From: users <[hidden email]> On Behalf Of Izabela Wójcik-Grzaba Sent: Wednesday, April 10, 2019 5:04 PM To: Users mailing list for Scilab <[hidden email]> Subject: Re: [Scilab-users] Nested function definition Ok, so why nested function in help is so complicated: //nested functions definition function y=foo(x)     a=sin(x)     function y=sq(x), y=x^2,endfunction     y=sq(a)+1 endfunction foo(%pi/3) Couldn't it be formulated like below: function y1=foo1(x)     a=sin(x);     y1=a^2+1; endfunction foo1(%pi/3) That's why I had problems with my functions and couldn't understand why it has to be so complicated. Thank you once more. Iza _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 In reply to this post by Iza Izabela, I have not clearly understood why you are speaking about "nested functions" in your example. A nested function is a function that is defined  in another one. About the example in the function help page: It is right, but with Scilab 6, it looks a bit outdated to me. Indeed, let's consider the following example: // Content of the File myTest.sci function myTest()    disp("myTest() is running")    myNextFun() endfunction function myNextFun()     disp("myNextFun() is running") endfunction // End of myTest.sci file When building a library (say "myLib"), this file is compiled, and With Scilab 5 : both functions myTest() and myNextFun() are registered in the library, and so are public: Both can be called from anywhere, noticeably from the top-level, the console. The only way to make myNextFun() a private function known only by myTest() is to define it IN myTest(), as a nested function. With Scilab 6: only myTest() is registered in myLib library, so is public, and can be called from anywhere. In the opposite, myNextFun() is NOT registered in the library so, is unknown from the console, is shared and can be called only by other functions defined in the same file. This is a more powerful implementation for the functions privacy, because then a private function (say myNextFun()) does no longer need to be recompiled each time that myTest() is called. IMO, this makes the code clearer This change in Scilab 6 could be documented in the --> help function page, in order to discourage true nested function. By the way, the example in the help misses being indented. However, out of libraries, nested functions can still be used in scripts.sce or in files.sci that are just exec()uted, for the same purpose: keeping nested functions private. HTH Regards Samuel Le 10/04/2019 à 16:04, Izabela Wójcik-Grząba a écrit : Ok, so why nested function in help is so complicated: //nested functions definition function y=foo(x)    a=sin(x)    function y=sq(x), y=x^2,endfunction    y=sq(a)+1 endfunction foo(%pi/3) Couldn't it be formulated like below: function y1=foo1(x)    a=sin(x);    y1=a^2+1; endfunction foo1(%pi/3) That's why I had problems with my functions and couldn't understand why it has to be so complicated. Thank you once more. Iza _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Nested function definition

 Ok, sorry. It's my mistake connected with using math terms not properly. Now I understand that nested function is more general term in Scilab help. The help example is similar to mine and this is the whole misunderstanding. I appreciate your comprehensive explanation. Now I am happy because the notation of my (mathematical) functions become simpler. Kind regards, Iza W dniu 10.04.2019 17:10, Samuel Gougeon napisał(a): > Izabela, > > I have not clearly understood why you are speaking about "nested > functions" in your example. > A nested function is a function that is _defined_  in another one. > > About the example in the function help page: > It is right, but with Scilab 6, it looks a bit outdated to me. > Indeed, let's consider the following example: > > // Content of the File myTest.sci > function myTest() >    disp("myTest() is running") >    myNextFun() > endfunction > function myNextFun() >     disp("myNextFun() is running") > endfunction > // End of myTest.sci file > > When building a library (say "myLib"), this file is compiled, and > > * With Scilab 5 : both functions myTest() and myNextFun() are > registered in the library, and so ARE PUBLIC: Both can be called from > anywhere, noticeably from the top-level, the console. > The only way to make myNextFun() a private function known only by > myTest() is to define it IN myTest(), as a nested function. > >   * With Scilab 6: only myTest() is registered in myLib library, so is > public, and can be called from anywhere. In the opposite, myNextFun() > >   * is NOT registered in the library >   * so, is unknown from the console, > > * is shared and can be called only by other functions defined in the > same file. > > This is a more powerful implementation for the functions privacy, > because then >   * a private function (say myNextFun()) does no longer need to be > recompiled each time that myTest() is called. > > * IMO, this makes the code clearer > >  This change in Scilab 6 could be documented in the --> help function > page, in order to discourage true nested function. By the way, the > example in the help misses being indented. > > However, out of libraries, nested functions can still be used in > scripts.sce or in files.sci that are just exec()uted, for the same > purpose: keeping nested functions private. > > HTH > Regards > Samuel > > Le 10/04/2019 à 16:04, Izabela Wójcik-Grząba a écrit : > >> Ok, so why nested function in help is so complicated: >> >> //nested functions definition >> function y=foo(x) >> a=sin(x) >> function y=sq(x), y=x^2,endfunction >> y=sq(a)+1 >> endfunction >> >> foo(%pi/3) >> >> Couldn't it be formulated like below: >> >> function y1=foo1(x) >> a=sin(x); >> y1=a^2+1; >> endfunction >> >> foo1(%pi/3) >> >> That's why I had problems with my functions and couldn't understand >> why it has to be so complicated. >> >> Thank you once more. >> >> Iza > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users