Is there any command in Fortran that is similar to eval in MATLAB?

In summary, the conversation discusses how to use the Fortran subroutine to solve non-linear equations using Newton's method, specifically when the derivative is given by finite difference calculation. It is suggested to either include the function in the main program or in a module to be called as normal. The person also mentions using similar commands in MATLAB.
  • #1
phanlada
2
0
Hello,

I am trying to write a Fortran subroutine to solve non-linear equations (given by external function fnxname) using Newton's method, where the derivative is given by finite difference calculation. How may I use the function in the program.

Suppose the desired function name is given by a string "fnxname", I cannot write fnxname(x), can I? In MATLAB, I may write eval(cat('fnxname','(x)')) or something like that. Is there a similar command in Fortran?

Replies would be much appreciated. Thank you very much.
 
Physics news on Phys.org
  • #2
You can either just put the function inside of the main program's file, or put the function inside of a MODULE and INCLUDE that module in the main program. Then you can simply call the function as normal.
 

FAQ: Is there any command in Fortran that is similar to eval in MATLAB?

What is the purpose of the eval command in MATLAB?

The eval command in MATLAB is used to evaluate a string as an expression or statement. This allows for dynamic execution of code and is commonly used in scripts and functions to create more flexible programs.

Is there a similar command in Fortran?

Yes, Fortran has the EQUIVALENCE statement which serves a similar purpose to the eval command in MATLAB. It allows for the manipulation of variables and expressions at run-time.

How do I use the EQUIVALENCE statement in Fortran?

To use the EQUIVALENCE statement, you must first declare the variables you wish to manipulate as EQUIVALENT. Then, you can assign different values or expressions to these variables at run-time using the EQUIVALENCE statement.

Are there any differences between the eval command in MATLAB and the EQUIVALENCE statement in Fortran?

While they serve a similar purpose, there are some key differences between the two. The eval command in MATLAB is more versatile and can evaluate any string as an expression, while the EQUIVALENCE statement in Fortran is limited to manipulating declared variables. Additionally, the eval command is only available in MATLAB, while the EQUIVALENCE statement is specific to Fortran.

Can I use the EQUIVALENCE statement in Fortran to evaluate expressions like the eval command in MATLAB?

No, the EQUIVALENCE statement cannot directly evaluate expressions like the eval command in MATLAB. It can only manipulate declared variables. However, you can use the EVALUATE statement in Fortran to achieve a similar result to the eval command in MATLAB.

Similar threads

Replies
5
Views
5K
Replies
11
Views
5K
Replies
3
Views
6K
Replies
4
Views
3K
Replies
4
Views
10K
Replies
7
Views
2K
Replies
1
Views
2K
Back
Top