找回密码
 注册
Simdroid-非首页
查看: 58|回复: 1

Compare & Contrast MATLAB/FORTRAN

[复制链接]
发表于 2011-12-8 16:05:13 | 显示全部楼层 |阅读模式 来自 湖南长沙
自己最近需要将一个fortran程序转译成matlab的   
找了好久,终于找到一个比较有用的资料了  
兴奋之余,拿出来与大家分享,希望大家有类似有用的资料拿出来一起分享下。

MATLAB                   FORTRAN F77

--------------------------------------------------------------------------------
PROGRAMMING ENVIRIONMENT

Path for functions & M-files:          Path for libraries
  set in DOS                             set in DOS
    set matlabpath=???                     set lib=???
  set in matlabrc.m for Windows
    matlabpath['???']
  set in matlab program
    path('c:\', path)
  other path commands
    path, pwd, cd

Preferred usage:
  2 windows (editor, command)          2 windows (editor, DOS for compile-link-run)

Intepreter                             Compiler

Speed: Slow as a turtle                Fast as a rabbit
       See speed.m                     See speed.exe

--------------------------------------------------------------------------------
GENERAL SYNTAX

% comments                             c comments
Columns: all                           Columns 7-72 for statements
Case Sensitive                         Case insensitive
Ignore blank spaces and lines.         Ignore blank spaces and lines.
Everything is a matrix.                Scalar (default=real/integer)
  the whole vector/matrix: x, A          x, A
  an element: x(1), A(1,1)               x(1), A(1,1)
  dimension: <=2 (in v4.0)               dimension may be > 2  (e.g., A(1,2,3))
Array size is dynamically adjusted.    Array size must be declared/fixed
                                       (F90 allows adjustable array size.)
No type declaration                    REAL, INTEGER, CHARACTER, LOGICAL ...
(more efficient with preallocation)
  e.g., A = zeros(2,3)                   e.g., REAL A(2,3)
Matrix stored in column-wise order     Matrix stored in column-wise order
  A = [ 1 2 3; 4 5 6];                     data A/1, 4, 2, 5, 3, 6/
  fprintf('%i\n',A) ... column-wise order  print *, A ... column-wise order
Complex number: x = 1 + 2*i            COMPLEX x;  x = CMPLX(1., 2.)
Logical variable (none)                LOGICAL declaration
  1 for true; 0 for false                .true. .false.
Algebraic operators: + - * / ^         + - * / **
Element-wise operator: "."
Transpose:           '                 (user provided)
Logical operators:
  < <= > >= == ~=                      .lt. .le. .gt. .ge. .eq. .ne.
  & | ~                                .and. .or. .not.

MATLAB and FORTRAN share the same algebraic function names
  sin, cos, tan, asin, acos, atan, exp, abs, etc.
  exception:
    sqrt: negative augument is ok          sqrt: positive argument only
    (-27)^0.3333 gives 1.5+2.6i (not -3)   domain error
    log                                    alog   (i.e., natural logarithm)
    log10                                  alog10 (i.e., common logarithm)
    rem                                    mod

Other conversion functions
  int8, int16, int32                       int
  num2str, int2str
  uint8('A')                               ichar('A')

Continuation:   ...                    column 6
Multiple statements separated by:
  "," or ";"                           none

--------------------------------------------------------------------------------
I/O

x                                      print *, 'x = ', x
x+y                                    print *, 'ans = ', x+y
y=x+1; (assignment)                    y=x+1
disp(' ')                              print *, ' '    or   write(*,*)
disp('text string')                    print *, 'text string'
disp(x)                                print *, x
disp(['x = ', num2str(x)])             print *, 'x = ', x
x=input('enter x  ')                   print *, 'enter x'  ;  read *, x
resp=input('Y/N  ', 's')               print *, 'Y/N:' ;  read '(a)', resp
  (i.e., input a string; need to declare "CHARACTER resp" in FORTRAN)
sprintf('format string', x);           write(*,'(format string)') x
  where format: %e, %f, %g, %s
                %15.3e, %15.7f, %15.7g,  e15.3, f15.7, g15.7,
                %8s, %7i                 a8, i7
                \n (new line)               /
fprintf('filename', 'format', [x, y])  open (1, file='filename') ... one shot
                                       write(1,'(format)') x, y
load x.dat or load x.ext               read an ascii file into a variable x
save file variable_list /ascii         save the specified variables in a file

fid=fopen('filename', 'r' )            open(fid, 'filename', status='old')
fid=fopen('filename', 'w' )            open(fid, 'filename', status='new')
  where standard input  fid=0             standard input  fid=5
        standard output fid=1             standard output fid=6

x = fscanf(0, 'format', size)          read (5, '(format)') x
fprintf(1, 'format', x, y, z)          write(6, '(format)') x, y, z
fprintf('format', x, y, z)             write(*, '(format)') x, y, z

fclose(1)                              close(1)

--------------------------------------------------------------------------------
GRAPH

plot                                   No standard graphing functions,
axis([xmin,xmax,ymin,ymax])            but most compilers provide a system-
title('title')                         dependent set of graphing functions.
xlabel('xlabel'), ylabel('ylabel')
text
legend
subplot
In title, xlabel, etc., TeX commands are recognized: eg: _ ^ \alpha

--------------------------------------------------------------------------------
LOOP

DO-Loop
  for i=1:n                            do i=1, n
    :                                    :
    (return/keyboard/pause/break)        (return/stop/goto/exit)
  end                                  enddo

  One line format:
  for i=1:n statement; statement; end

  for i=n:-1:1    ... decrement i      do i=n, 1, -1    ... decrement i
    :                                    :
    (return/keyboard/pause/break)        (return/stop/goto/exit)
  end                                  enddo

WHILE
  while condition                      do while (condition)
    :                                    :
    (return/keyboard/pause/break)        (return/stop/goto/exit)
  end                                  enddo

--------------------------------------------------------------------------------
CONDITION

IF
  if condition                         if (condition) then
    :                                    :
  elseif condition                     elseif (condition) then
    :                                    :
  else                                 else
    :                                    :
  end                                  endif

SELECT-CASE (MATLAB v5)
  switch expression                    select case (resp)
    case 0                               case ('y')
      statements1                          statements1
    case 1                               case ('a', 'b')
      statements2                          statements2
    case 2                               case ('c':'f', 'h':'j')
      statements3                          statements3
    otherwise                            case default
      statements4                          statements4
  end                                  end select

--------------------------------------------------------------------------------
JUMP
  none                                 goto label

--------------------------------------------------------------------------------
MAIN/SUB PROGRAMS and STRUCTURE

Script file
  % comments                           program main
    (1st blank line)                   c comments
  % further comments                   statements
  statements                             :
  error('text string')                 stop 'text string'
                                       end
  (may have several script files)      (can have only one main program)

Subroutine without Returned Variables
  function dummy_name(arguments)       subroutine name(arguments)
  % comments                           c comments
       (1st blank line)                    statements
  % further comments                         :
  statements                                 :
  (return)                                 (return)
  Calling convention:
    foo(arguments)
    Similar to a plain script          end

Scalar function
  function y = dummy_name(arguments)   function y(arguments)
  % comments                           c comments
       (1st blank line)                    statements
  % further comments                         :
  statements                                 :
  y = ....                                 y = ...
  (Assignment is made to y.)               (Assignment is made to function name)
  (y may be a matrix.)                     end

Multiple-output function
  function [y1, y2] = dummy_name(arguments)
  % comments
       (1st blank line)
  % further comments
  statements                           Return multiple choices with FUNCTION F(..., choice)
  y1 = ....                            Return multiple variables with SUBROUTINE
  y2 = ....
  (All return variables must be on LHS
  grouped in "[?,?]" .)
  The LHS is NOT a matrix; thus, the
  dimensions of y1 and y2 need not match.
  Calling convension (/w or /wo comma):
    [y1 y2]=foo(...)
    [y1,y2]=foo(...)

Alternative way to pass variables
  global x y z                         common x, y, z
                                       common /cblock/ x, y, z

  (Caution: v3 forced globalization within the entire MATLAB!  Choose names wisely.)
            v4 extends globalization within only the functions in which
            global is declared.)
  Function arguments should not appear in "global" or "common" list.

Funciton with no argument
  y = dummy_name ()                    function name()
  Calling convension: y=foo            Calling convension: y=name()
  (Note: no parentheses!)


Each function stored in a separate     Multiple subroutines in one file.
  M-file.                               (No restriction.)
A copy of the argument is passed       Address of the argument is passed.
  to a function.  Passing by value.      Passing by address/pointer.
  (Nothing is returned via arguments!)  (Subroutine/function can return variables
  See pass.m                             in the argument list)
      passfcn.m


(资料摘自网站)http://terpconnect.umd.edu/~nsw/ench250/for-mat.htm
希望给位多多补充 fortran 转译到matlab的相关有用资料,谢谢先!
 楼主| 发表于 2011-12-8 16:06:24 | 显示全部楼层 来自 湖南长沙
Simdroid开发平台
:'(格式变了好多啊
都没对齐  看不清楚了
大家可以进入http://terpconnect.umd.edu/~nsw/ench250/for-mat.htm 查看!~~
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-7-8 04:47 , Processed in 0.031796 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表