Quale versione del fortran utilizzi?
A cura di Giuseppe Ciaburro
Pubblicato il 14/11/2001
Un confronto tra le caratteristiche principali del Fortran 77 e del software Matlab
Se si conosce
un linguaggio di programmazione, è facile apprendere le basi di un qualsiasi
altro linguaggio.la logica sulla quale sono stati realizzati è pressoccchè
la stessa, naturalmente la sintassi è completamente differente.Di seguito
abbiamo confrontato le caratteristiche principali dei due linguaggi Fortran
77 e Matlab.
MATLAB FORTRAN F77Ambiente di programmazione
Path per funzioni & M-files: Path per librerie 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) Inteprete Compilatore Velocità: Lento come una tartaruga Veloce come un coniglioSintassi
% 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 ";" noneDirettve di 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)Grafici
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') textCicli
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 enddoStrutture condizionali
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 selectSalti
none goto labelProgrammi e strutture principali e subroutine
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 senzat variabili di ritorno 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 Funzioni Scalari 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 Funzioni con Multiple-output 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(...) Modo alternativo di passare le variabili 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. Funzioni senza argomenti 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 in the argument list)