Most problems that require a computer program to solve them are too complex to sit down and work all the way through them in one go. Using subprograms allows you to tackle bite size pieces of a problem individually. Once each piece is working correctly you then put the pieces together to create the whole solution. To implement functions and subroutines, first write a main program that references all of the subprograms in the desired order and then start writing the subprograms. This is similar to composing an outline for an essay before writing the essay and will help keep you on track.
Creating Level 2 Fortran S-Functions To use the features of a level 2 S-function with Fortran code, you must write a skeleton S-function in C that has code for interfacing to Simulink and also calls your Fortran code. If instead your Fortran code already exists as a stand-alone simulation, there is some work to be done to identify parts of the code that need to be registered with Simulink, such as identifying continuous states if you are using variable-step solvers or getting rid of static variables if you want to have multiple copies of the S-function in a Simulink model see Porting Legacy Code. It works with a simple Fortran subroutine if you modify the Fortran subroutine name in the code. Mex Environment Remember that mex -setup needs to find both the C and the Fortran compilers. If you install or change compilers, you must run mex -setup.
Intrinsic functions are some common and important functions that are provided as a part of the Fortran language. We have already discussed some of these functions in the Arrays, Characters and String chapters. We have discussed the array functions in the Arrays chapter. In the following section we provide brief descriptions of all these functions from other categories.
This worksheet makes use of several examples programs that are all available for download from this website. The program sets up some arrays and then outputs them. At three stages in the program bolded , it asks whether it should continue; it stops if the answer is not 'y'.