Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
210 Views

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.

0 Kudos
11 Replies
Highlighted
Black Belt
210 Views

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.

0 Kudos
Highlighted
210 Views

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

Jim Dempsey

0 Kudos
Highlighted
Beginner
210 Views

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

character(999)::scha1
real(kind=srk)::y,m,x,c

open(unit=1000,file='data_input\\eqn_and_initial_value.txt',status='old',action='read')

        read(1000,*)scha1
        write(*,*)scha1
        
        y=real(trim(scha1))     <===== this statement is wrong. Could someone help me to move forward?
        
    
close(unit=1000)

end subroutine set_eqn_and_initial_val1

0 Kudos
Highlighted
Black Belt
210 Views

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 http://fparser.sourceforge.net
  !
  ! Please send comments, corrections or questions to the author:
  ! Roland Schmehl <roland.schmehl@alumni.uni-karlsruhe.de>
  !
  !------- -------- --------- --------- --------- --------- --------- --------- -------
  ! The function parser concept is based on a C++ class library written by  Juha 
  ! Nieminen <warp@iki.fi> available from http://warp.povusers.org/FunctionParser/

0 Kudos
Highlighted
Black Belt
210 Views

Eng Giap G. wrote:

character(999)::scha1

open(unit=1000,file='data_input\\eqn_and_initial_value.txt',status='old',action='read')

        read(1000,*)scha1

        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.

0 Kudos
Highlighted
New Contributor II
210 Views

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

0 Kudos
Highlighted
New Contributor III
210 Views

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

http://gams.nist.gov/

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 

0 Kudos
Highlighted
Black Belt
210 Views

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
allocate(x(m),y(m,1),c(n+1))
read(*,*)(x(i),y(i,1),i=1,m)  ! data pairs
allocate(A(m,n+1))
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' 
   stop
endif
c=y(1:n+1,1)                  ! coefficients of polynomial
write(*,10)(i,c(i),i=1,n+1)
10 format(I3,2x,ES12.4)
end program polyfit

 Sample data file:

1,3
1,4.01
2,5.49
3,6.95

Program results (coefficients):

  1    2.5433E+00
  2    1.4700E+00

 

0 Kudos
Highlighted
New Contributor II
210 Views

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

0 Kudos
Highlighted
Black Belt
210 Views

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.

0 Kudos
Highlighted
Beginner
210 Views

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.

0 Kudos