Fortran code in MATLAB/Simulink.

3 ビュー (過去 30 日間)
afaq ahmad
afaq ahmad 2021 年 10 月 5 日
I have a Fortran code which solves a First order ODE using Euler method. I tried calling it in Simulink using C-mex in S function Block.
I have been able to successfully compile, link and run the Fortran code. However, after running the Simulink file only the first value of the Fortran code is being displayed in the Scope block. Here is the snippet of the Fortran Code:
do k = 1,n
x = x + h*f(t,x)
t = t + h
out=x !This is the output of the S function which I want to view.
end do
RETURN
END
function f(t,x)
real, intent(in)::x, t
f = 1.0 + x*x + t**3
end function f
Ideally,I want the out variable to display all the incremental values while running in the Do loop. However, only the first value of the out after the first iteration of the Do loop is being displayed.
Moreover, I tried declaring out as an Array in the Fortran and stored all values of Do loop in it, but Matlab crashes everytime it tries to simulate it. This I assume is from datatype inconsistencies between the datatypes declared in C-mex file and Fortran.
Any workaround this problem will be much appreciated.
Thanks!
I am also attaching a snippet of C-mex from where it calls the Fortran Subroutine:
static void mdlOutputs(SimStruct *S, int_T tid)
{
const double *in1 = (const double *) ssGetInputPortSignal(S,0);
double *out = (double *) ssGetOutputPortRealSignal(S,0);
int w = ssGetInputPortWidth(S,0);
int k;
float fin1, fout;
for (k=0; k<w; k++) {
/* set the input value */
fin1 = (float) in1[k];
/* call the Fortran routine using pass-by-reference */
diff_(&fin1, &fout);
/* format the outputs using the reference parameters */
out[k] = (double) fout;

回答 (0 件)

カテゴリ

Help Center および File ExchangeFortran with MATLAB についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by