[scilab-Users] fsolve with external fortran subroutine
Serge Steer
Serge.Steer at inria.fr
Mon Jul 16 17:15:51 CEST 2012
Le 16/07/2012 16:04, Eduardo Torrecillas a écrit :
>
> Hi everybody,
>
> I am trying to use fsolve with a fortran subroutine as argument. For
> example, consider the following calling sequence:
>
> X0=[0 0]
>
> tol=1E-15
>
> [X,v]=fsolve(X0,'fequil_fortran',tol)
>
> In this case 'fequil_fortran' is my external fortran subroutine, given
> below:
>
> --------------------------------------------------------------------
>
> subroutine fequil_fortran(n,x,v,iflag)
>
> implicit none
>
> integer,intent(in)::n,iflag
>
> real(kind=8),dimension(n),intent(in)::x
>
> real(kind=8),dimension(n),intent(out)::vreal(kind=8)::a,b
>
> a=1.0_dp
>
> b=2.0_dp
>
> v(1)=a*x(1)+b*x(2)-1.0_dp
>
> v(2)=2.0_dp*a*x(1)-b*x(2)-5.0_dp
>
> end subroutine fequil_fortran
>
> ---------------------------------------------------------------------
>
> It worked fine and fsolve found the solution. Now, suppose I have to
> pass arguments to the fortran subroutine, in addition to the default
> (n,x,v,iflag). For instance, imagine I had to pass the 'a' and 'b'
> variables from Scilab to the fortran subroutine in the code above. I
> have already performed something like this but with "optim". In fact,
> "optim" gives you the possibility to pass a list of integer, real or
> double variables to the external code. Is there anything similar to be
> used with fsolve?
>
The answer is no, the underlying code does not handle extra arguments
for the external function.
It is however possible to declare a and b as elements of a common and to
set their value using another fortran code called using the function call
something like
subroutine fequil_settings(a,b)
...
end
Also add the common declaration in fequil_fortran, compile and link both
function with Scilab
And then in Scilab you can do
a=...
b=...
call("fequil_settings",a,1,"d",b,2,"d","out",[1 1],"d");
[X,v]=fsolve(X0,'fequil_fortran',tol)
> Regards,
>
> --
> Eduardo Torrecillas
> AER-09
More information about the users
mailing list