Using Matlab functions in Fortran -

In summary: However, you'll need to be very careful with communication between the two languages, as you'll need to be sure to pass back the correct values each time.
  • #1
confi999
19
0
Hello,
My work is on numerical techniques which require to solve sparse matrices etc. All my work is in fortran and I have almost no skill in Matlab.But I have just realized that Matlab can do some computations quite easily with its built-in functions and I want to take advantage of this. I have no time to learn Matlab deeply or work on matlab-fortran communication (very much off-focus issues and have deadline soon)

Some details:
I need to solve Ax=b system where A is very large and sparse matrix (20000x20000 or more). At every time step (iteration) I need to solve this system, get 'x' and reuse it for further computation in that step. [A] is fixed and formed before the iteration begins - but vector 'b' changes at every iteration. So in the iteration -- i) vector 'b' is formed, ii) solve Ax=b to find 'x' iii) use many other (explicit algebraic) equations to calculate some other values / fields (magnetic fields) iv) save field values for that step and move to the next step. Then these steps are repeated once and again. I am doing steps i), iii), iv) in fortran and want to do step ii) in matlab. Actually I have the whole code written in fortran but want to do some analysis by just doing the step ii) in Matlab. Can you please advise me if that is possible and if, how can I do that?

I have tried Matlab to solve Ax=b just for a single iteration. That means, I could read matrix [A], an instance of vector 'b' in MATLAB (after saving them in files from fortran). Then I solved them in Matlab command window using 'bicgstab' command (also used 'luinc' preconditioner etc, did some matrix blocking etc). But I need to repeatedly do this for the whole system and also calculate 'b' vector in fortran using the obtained solution. Can you please advise me on this. Thank you so much.
 
Physics news on Phys.org
  • #2
Using Fortran routines in Matlab is fairly straight forward, the opposite isn't true at all. I'd convert over to Matlab because of the functionality and it won't take too long to get up to speed.
 
  • #3


I can understand your dilemma of wanting to take advantage of Matlab's built-in functions while still working primarily in Fortran. My advice would be to try and find a way to integrate the two languages in your code, rather than trying to use them separately. This will not only save you time in the long run, but also allow for a more seamless and efficient workflow.

One potential solution could be to use the Matlab Engine API, which allows for calling Matlab functions from within Fortran code. This way, you can still use Fortran for the majority of your code, but easily incorporate the necessary Matlab functions for solving your sparse matrix system.

Another option could be to use a Fortran-Matlab interface, such as F2Matlab, which allows for easy communication between the two languages. This would allow you to pass data back and forth between Fortran and Matlab without having to save and read files.

Ultimately, it will require some effort and learning on your part to implement either of these solutions, but it may be worth it in the long run to have a more efficient and integrated workflow. I would also recommend reaching out to colleagues or experts in the field who may have experience with integrating Fortran and Matlab in their work.

Best of luck with your project and deadline!
 

1. How can I use Matlab functions in Fortran?

To use Matlab functions in Fortran, you need to first compile the Matlab code into a shared library using the Matlab Compiler. Then, you can call the functions from your Fortran code by linking to the shared library and using the "call" statement.

2. Can I pass data between Fortran and Matlab?

Yes, you can pass data between Fortran and Matlab by using the "mex" function in Matlab and the "call" statement in Fortran. This allows you to call Matlab functions from Fortran and pass data back and forth between the two programs.

3. Are there any limitations to using Matlab functions in Fortran?

There are a few limitations to using Matlab functions in Fortran. For example, you cannot use Fortran's array syntax to access Matlab arrays, and you cannot call certain types of Matlab functions that use advanced features such as cell arrays or structures. Additionally, there may be performance differences between using Matlab functions in Fortran compared to native Matlab code.

4. Do I need to have a Matlab license to use Matlab functions in Fortran?

Yes, you will need a Matlab license in order to use Matlab functions in Fortran. This is because you will need to use the Matlab Compiler to compile your Matlab code into a shared library. However, you do not need to have the full version of Matlab - the Matlab Compiler is included in some versions of Matlab, such as Matlab Compiler Runtime.

5. Are there any resources available to help me learn how to use Matlab functions in Fortran?

Yes, there are several resources available to help you learn how to use Matlab functions in Fortran. These include tutorials and documentation provided by MathWorks, as well as online forums and communities where you can ask for help and advice from other users who have experience with using Matlab functions in Fortran.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
686
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top