需要将以下FORTRAN代码转换为C++

0 c++ matlab fortran

我是一个非常差的程序员,我得到了一个程序,据说可以帮助我的空气动力学hw.但它在fortran,我试图使用MATLAB来运行这个程序.将它转换为matlab语言的任何帮助都能理解?(优选c ++)

      program joukow
c

c   computes joukowski airfoil and finds pressure coefficient

c   currently set up for symmetric airfoil with sharp trailing edge

c   and chord length equal to one.

c   profile is written onto prof.dat and cp onto cp.dat

c      implicit real*8(a-h,o-z)

      complex z,zeta,cw
      dimension uz(100),vz(100),xi(100),eta(100),cp(100)
      dimension xout(100),yout(100)
         open(unit=8,file='prof.dat',status='unknown')
         open(unit=9,file='cp.dat',status='unknown')
      b=1.d0
      write(6,98)
      format(2x,'input the radius of the a-circle in z plane')
      read(5,99)a
      format(f10.0)
      xl=2.*a-1.+1./(2.*a-1.)

c      xl=a+1./a

c      chord=2.*xl

      chord=2.+xl
      del=a-b

c      del =0.1d0
      do 50 i=1,100
      ri=i
      theta=6.2832d0*ri/101.d0
      x=-del+a*cos(theta)
      y=a*sin(theta)
      z=cmplx(x,y)
      zeta=z+b**2/z

c

c  xi and eta are coordinates of points on airfoil

c

      xi(i)=real(zeta)

      eta(i)=aimag(zeta)

      cw=(1.-a**2/(z+del)**2)/(1.-b**2/z**2)
c

c  uz and vz are velocity components on the airfoil assuming the free-stream

c  speed is one.
c
      uz(i)=real(cw)
      vz(i)=-aimag(cw)

c

c  xout and yout are airfoil coordinates where the leading edge is at (0,0)

c  and the chordlength is one.

c

      xout(i)=(xl+xi(i))/chord
      yout(i)=eta(i)/chord
      write(8,100)xout(i),yout(i)
      format(2x,2f10.4)
      continue

c

c  now calculate the pressure coefficient cp

c

      write(6,200)
      format(2x,'pressure coefficients')
      do 70 i=1,50
      cp(i)=1.-(uz(i)**2+vz(i)**2)
      write(9,100)xout(i),cp(i)
      continue
      stop
      end
Run Code Online (Sandbox Code Playgroud)

Hig*_*ark 8

Matlab了解Fortran就好了 - 查看文档.如果这不能满足你的要求,程序中进行任何计算的大多数行都可以输入Matlab控制台,只需很少的修改.如果你是一个糟糕的程序员,我建议你花时间将程序修改为Matlab而不是C++.如果你没有得到比我现在有时间更好的帮助,我会稍后再写.

编辑:首先,关于使用 Matlab的Fortran源文件的一些信息.如果你真的不想(或者不能或有没有这样做的性能原因)将Fortran重写为Matlab,那么将其转换为MEX文件.使用f2c(或其他任何东西,包括你自己的时间和精力)首先将Fortran翻译成C或C++对我来说似乎毫无意义.

如果你不喜欢这个想法,这里有一些关于将Fortran变成Matlab的想法.

首先,所有以C或c开头的行都是注释,因此您无需翻译它们.从您的代码开始:

  complex z,zeta,cw
  dimension uz(100),vz(100),xi(100),eta(100),cp(100)
  dimension xout(100),yout(100)
Run Code Online (Sandbox Code Playgroud)

这些行声明了许多变量.在Matlab中使用变量之前,不必声明变量,但有时候有充分的理由这样做.你不必在Fortran中,尽管现在普遍认为这是一个坏主意.您可以在Matlab中"声明"这些变量,例如:

uz = zeros(100,1); 
vz = zeros(100,1);
Run Code Online (Sandbox Code Playgroud)

通过在Matlab中预先声明这些,您可以为它们分配一次内存,并避免一些降低性能的问题.

接下来的两行:

     open(unit=8,file='prof.dat',status='unknown')
     open(unit=9,file='cp.dat',status='unknown')
Run Code Online (Sandbox Code Playgroud)

打开几个文件输出.它们稍后在write语句中使用- 忘记它们,编写Matlab语句等save xout.

下一行是Fortran,但在Matlab中是相同的:

  b=1.d0
Run Code Online (Sandbox Code Playgroud)

下一行从控制台获取半径值:

  write(6,98)
  format(2x,'input the radius of the a-circle in z plane')
  read(5,99)a
  format(f10.0)
Run Code Online (Sandbox Code Playgroud)

再次,我建议你忘记这些,只需使用Matlab控制台设置值a.更多不需要翻译的Fortran(虽然我建议您在不跟随0的情况下删除小数点或在它们之间放置一个空格,随后的* - .*是Matlab中的特定运算符):

  xl=2.*a-1.+1./(2.*a-1.)

  chord=2.+xl
  del=a-b
Run Code Online (Sandbox Code Playgroud)

Fortran do循环与Matlab for循环相同.改写:

  do 50 i=1,100
Run Code Online (Sandbox Code Playgroud)

for i = 1:100
Run Code Online (Sandbox Code Playgroud)

正如其他受访者之一所指出的那样,目前尚不清楚匹配结束语的位置,你必须弄明白这一点.请注意,我只是将Fortran逐行转换为Matlab.它不是写得好的Fortran,而且我没有提供写得很好的Matlab,我会把它留给你.

这批不需要翻译:

  ri=i
  theta=6.2832d0*ri/101.d0 
  x=-del+a*cos(theta)
  y=a*sin(theta)
Run Code Online (Sandbox Code Playgroud)

cmplx是一个Fortran函数,它返回一个复数,它具有实部x和虚部y:

  z=cmplx(x,y)
Run Code Online (Sandbox Code Playgroud)

在Matlab中,这将是z = x + y*i.Fortran使用**进行取幂,Matlab使用^

  zeta=z+b**2/z
Run Code Online (Sandbox Code Playgroud)

等等等等.

希望有所帮助.