我一直在努力将我的MATLAB程序转换成Fortran (同时仍然利用MATLAB的一些特性)。我正在尝试利用IMSL中可用的例程。它提供了一个非线性方程求解器neqnf,但我还不能弄清楚如何传递根据子例程被调用的时间而变化的变量(例如,你可以使用MATLAB中的fsolve )。例如,下面是用Fortran编写的MATLAB mexFunction,它调用neqnf。子例程sub包含要求解的方程组。如何将变量通过neqnf传递到sub中,以获得这两个线性方程的系数和截距?
谢谢!
#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发布于 2013-05-30 18:48:00
为什么要转换到fortran?执行速度?
此外,如果您要进行大量此类转换,请考虑在文件交换时尝试使用matlab2fmex。它可以为您完成大量将数值matlab代码转换为fortran的繁忙工作。
https://stackoverflow.com/questions/16825155
复制相似问题