首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将变量传递到Fortran IMSL neqnf非线性方程求解器中?

如何将变量传递到Fortran IMSL neqnf非线性方程求解器中?
EN

Stack Overflow用户
提问于 2013-05-30 06:45:31
回答 1查看 592关注 0票数 0

我一直在努力将我的MATLAB程序转换成Fortran (同时仍然利用MATLAB的一些特性)。我正在尝试利用IMSL中可用的例程。它提供了一个非线性方程求解器neqnf,但我还不能弄清楚如何传递根据子例程被调用的时间而变化的变量(例如,你可以使用MATLAB中的fsolve )。例如,下面是用Fortran编写的MATLAB mexFunction,它调用neqnf。子例程sub包含要求解的方程组。如何将变量通过neqnf传递到sub中,以获得这两个线性方程的系数和截距?

谢谢!

代码语言:javascript
复制
#include "fintrf.h"
#include "link_fnl_shared.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
    ! Declarations
    use NEQNF_INT
    implicit none
    external sub

    ! mexFunction arguments
    mwPointer plhs(*), prhs(*)
    integer*4 nlhs, nrhs

    ! mex declarations
    mwpointer mxGetPr,mxCreateNumericArray
    integer*4 mxClassIDFromClassName 

    ! Internal variables
    integer*4 myclassid

    ! Output variables
    mwpointer :: f_pr,x_pr
    double precision :: f(2),x(2)

    ! Create return arguments and assign pointers
    myclassid = mxClassIDFromClassName('double')
    plhs(1) = mxCreateNumericArray(1,2,myclassid,0)
    plhs(2) = mxCreateNumericArray(1,2,myclassid,0)
    f_pr = mxGetPr(plhs(1))
    x_pr = mxGetPr(plhs(2))

    ! Test nonlinear solver (Math.pdf, pg. 1238)
    call d_neqnf(sub,x)

    ! Assign output
    call mxCopyReal8toPtr(f,f_pr,2)
    call mxCopyReal8toPtr(x,x_pr,2)

end subroutine mexFunction

! Subroutine
subroutine sub(x,f,n)
    mwSize n
    double precision :: x(n) ! input
    double precision :: f(n) ! output
    f(1) = 2.d0*x(1) + 1
    f(2) = -1.d0*x(2) + 4
end subroutine sub
EN

回答 1

Stack Overflow用户

发布于 2013-05-30 18:48:00

为什么要转换到fortran?执行速度?

此外,如果您要进行大量此类转换,请考虑在文件交换时尝试使用matlab2fmex。它可以为您完成大量将数值matlab代码转换为fortran的繁忙工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16825155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档