Read from a .txt file a character equation "y=m*x+c" and convert it into mathematical expression in Fortran code

Read from a .txt file a character equation "y=m*x+c" and convert it into mathematical expression in Fortran code

Has anyone any idea about it?

if i read it as character, there seem no way to convert into mathematical expression in fortran code. It stays as character.

12 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

What do you mean by "convert into mathematical expression"? There is no intrinsic variable type in Fortran called "mathematical expression" or endowed with the usual expected properties of mathematical expressions.

Are you trying to program a calculator in Fortran? Be aware that this has been done several times, but it is by no means simple to do.

Is this part of a computer science test problem? e.g. write a Fortran program to parse equations.

Jim Dempsey

Hi guy, I'm trying to write a program to conduct regression (or curve-fitting), I could not convert the read character into statement useable in Fortran . My problem as below:

subroutine set_eqn_and_initial_val1

use mod_share_GNA1
implicit none



        y=real(trim(scha1))     <===== this statement is wrong. Could someone help me to move forward?

end subroutine set_eqn_and_initial_val1

You might like to look up this:

  !------- -------- --------- --------- --------- --------- --------- --------- -------
  ! Fortran 90 function parser v1.1
  !------- -------- --------- --------- --------- --------- --------- --------- -------
  ! This function parser module is intended for applications where a set of mathematical
  ! fortran-style expressions is specified at runtime and is then evaluated for a large 
  ! number of variable values. This is done by compiling the set of function strings 
  ! into byte code, which is interpreted efficiently for the various variable values. 
  ! The source code is available from
  ! Please send comments, corrections or questions to the author:
  ! Roland Schmehl <>
  !------- -------- --------- --------- --------- --------- --------- --------- -------
  ! The function parser concept is based on a C++ class library written by  Juha 
  ! Nieminen <> available from


Eng Giap G. wrote:




        y=real(trim(scha1))     <===== this statement is wrong. Could someone help me to move forward?

The function REAL cannot be applied to character type arguments.

To answer your question regarding moving forward, we need to know the contents of the file. 

If you are satisfied with fitting only linear expressions or polynomials, it would be enough to structure the input data accordingly.

Linear regression does not require trial values of the fit coefficients.

Writing an expression parser(or even adopting an existing one)  is probably an overkill for your needs. Please explain what you want your program to do, and remember that Fortran is a compiled language.

It looks to me as if he simply wants to do linear regression on a set of points.

You could take a look at the code available at the NIST GAMS (Guide to Available Mathematical Software) site:

Use the key word search for terms such as "linear regression" or "curve fit" to see the available subroutines.  Maybe a routine will fit your need, or at least provide examples.

Regards, Greg 

The following Fortran program uses MKL/Lapack-95 GELS to obtain the least squares polynomial to fit input x,y data. The first line of the data file contains the degree and the number of input data pairs. The subsequent lines contain (x(i),y(i), i = 1, m) data.

program polyfit
use lapack95, only : gels
implicit none
double precision, allocatable :: x(:),y(:,:),c(:),A(:,:)
integer :: i,j,m,n,info
read(*,*)n,m                  ! degree and number of data pairs
read(*,*)(x(i),y(i,1),i=1,m)  ! data pairs
A(:,1) = 1d0
do j=2,n+1
   A(:,j) = A(:,j-1)*x
end do
call gels(A,y,'N',info)
if(info /= 0)then
   write(*,*)'*** INFO = ',info,' FROM GELS' 
c=y(1:n+1,1)                  ! coefficients of polynomial
10 format(I3,2x,ES12.4)
end program polyfit

 Sample data file:


Program results (coefficients):

  1    2.5433E+00
  2    1.4700E+00


This might be an assignment - I'm not sure that we should do students' assignments for them.

I agree, but one would hope that if this is a homework problem the instructor would have stipulated that the student must include a verbal description of the underlying linear algebra and an explanation of how the code implements the underlying algorithm.

Hi, guys, thanks for helping out. I will look into all the recommendation and suggestion. This is not a student assignment. I am a researcher and I'm trying to code to solve a scientific problem. The solution of the problem would most likely end up in the hand of researchers like field biologist (environmentalist) who has limited access to expensive software, and also the ability to customize equation to fit their needs and data management to save time. Thanks again for all those who are helping out. This would also help others encountering a similar problem.

Leave a Comment

Please sign in to add a comment. Not a member? Join today