Вы находитесь на странице: 1из 35

INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO

1
ENVIRONMENT

• What is a Computer Program?


• Are there different languages at programming?
• What is a Syntax?
• What is a Source Code?

Hint:
A computer program written according to the Syntax of a particular program
language is called a Source Code.
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Steps to prepare a Computer Program

Library of
Functions

Program in Compiler of the Executable


Object
high level Programming + program or
program
language language Application

Hint:
A Library is a group of instructions to evaluate common functions, like trigonometric,
logarithmic functions, etc.
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Program Structure in FORTRAN

PROGRAM name
IMPLICIT NONE
[specification part]
[execution part]
[subprogram part]
END PROGRAM name

Hint:
! Means that the line is a comment. It’s recommended to use ! In order to have a
better explanation what the program does.
The Executable is portable ☺
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Flow Diagram or Pseudocode

Hint:
! Means that the line is a comment.
It’s recommended to use ! in order to have a better explanation what the program
does.
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Exercise #1:
• Prepare a flow diagram that
shows if a number is even or odd.

Exercise #2:
• Lets figure out what does these
flow diagrams do!

Exercise #3:
• Prepare a flow diagram that
shows Bisection method.

Hint:
Bisection Method is in the graph!

Source:
http://upload.wikimedia.org/wikipedia/commons/8/8c/Bisection_method.svg
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

START

Yes
Solution for the x%2 == 0
Exercise #1
No

Print “Even” Print “Odd”

END

Hint:
Even numbers: 2, 4, 6, 8, etc.
Odd numbers: 1, 3, 5, 7, etc.
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Solution for the


Exercise #2

Source:
http://people.revoledu.com/kardi/tutorial/BasicMat http://4.bp.blogspot.com/-f6-
h/Prime/image/Algorithm- Oha58oKU/Up76s15y3rI/AAAAAAAAA6I/AIILGpM_Ag8/s1600/FLOWCHA
RT+FOR+CHECKING+WEITHER+A+NUMBER+IS+PRIME+OR+NOT.PNG
PrimeFactor_clip_image002.jpg

Hint:
Prime numbers are numbers that have no additional divisors other than 1 and itself.
Ex: 2 (PRIME) 3 (PRIME) 4 (NOT PRIME)
2 divided by 1 is 2 3 divided by 1 is 3 4 divided by 1 is 4
2 divided by 2 is 1 3 divided by 2 is NOT INTEGER 4 divided by 2 is 2
3 divided by 3 is 1 4 divided by 3 is NOT INTEGER
4 divided by 4 is 1
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

START
Solution for the
Exercise #3 a = x₁
b = x₂

x = (a+b)/2 Bisection Interval

Yes f(a)*f(x)
>0 Decision Shape

No
a=x b=x Update interval

No

|b - a| < ε Decision Shape

Yes
Output:
Root “x”

STOP
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT
START Check your
Naval Application background
Input knowledge in
Prepare a flow diagram to i, n Numerical
assign the factor in numerical Yes Analysis! ☺
integration Simpson’s 1st rule.
i = 0 or i = n

No
Subindex goes from 0 to n
Factor=…..
If i=0 and n=3, then Factor=? i = even Factor=…

If i=1 and n=5, then Factor=?


If i=2 and n=2, then Factor=?
Factor=…..
If i=3 and n=7, then Factor=?

END

∑ [1f
1
+ 4f1 + 2f2 + 4f3 + 2f4 + ... 2fn −2 + 4fn −1 + 1fn ]
Hint: ∫ f ( x ) dx ≈ h
3
0

1st Simpson’s Rule


If you have 2 points, then the factors are: 1, 4, 1.
If you have 6 or more even points, the factors are: 1, 4, 2, 4, … , 4, 1.
n: number of points i: subindex
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Basic Operators
Operator Meaning

+ Addition

- Substraction

/ Division

** Raise to the power of…

* Multiplication

Hint:

INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Intrinsic Functions
Function Meaning
ABS(x) Absolute Value
ACOS(x) Inverse Cosine
ASIN(x) Inverse Sine
ATAN(x) Inverse Tangent
COS(x) Cosine (Remember that this value inside goes in radians)
EXP(x) Exponential function
LOG(x) Natural Logarithm
LOG10(x) Logarithm to base 10
MAX(x1,x2,…) Maximum value in a range
MIN(x1,x2,…) Minimum value in a range
MOD(x1,x2) x1-int(x1/x2)*x2; the same than the remainder
SQRT(x) Square root function
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Conditional Expressions

Operator Equivalence Meaning


( .lt. ) < Less than
( .le. ) <= Less than or Equal to
( .eq. ) == Equal
( .ge. ) >= Greater than or Equal to
( .gt. ) > Greater than
( .ne. ) /= Not Equal
( .not. ) Not
( .and. ) And
( .or. ) Inclusive or
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Data types

Operator Equivalence Example


real Real real*8 pi
pi= 3.14159265
(real*8 means quite accurate)
integer Integer int n
n=3
complex Complex complex comp
comp=(1,0,2,0)
comp=1+2i
character Alphanumeric Character hello
hello=‘Hola’
logical Logic Logical logic
Logic=.true.
(it can also be false) ☺
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Basic Control Structures

Decision with 1 alternative: Decision with 2 alternatives:


if (condition) then if (condition) then
… …
… else
… …
end if end if

Decision with multiple alternatives:


if (condition1) then

else if (condition2) then

else

end if
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Let’s start using FORTRAN

program example
! My first program
write(*,*) ’Hello there’
end program example
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Basic Control Structures

Do cycles:
do i=1,n (increment is 1)

end do

EXAMPLE
program cycles
implicit none
!define variables
integer :: n
do n = 1, 10
write(*,*) n
end do
end program cycles
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Basic Control Structures

Do while:
do while (condition)

end do

EXAMPLE
program cycles1
implicit none
!define variables
integer :: n=10
do while(n.gt.0)
write(*,*) n
n=n-1
end do
end program cycles1
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

EXERCISE 1: Test if a number is even or odd


program exercise1
implicit none
integer a
write(*,*) ‘Write an integer number’
read(*,*)a
if(mod(a,2).eq.0)then
write(*,*)a,” it’s an even number”
else
write(*,*)a,” it’s an odd number”
end if
end program example
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

EXERCISE 2: Test if an integer number is prime or not


program prime
implicit none
integer a,i,acounter
write(*,*) ‘Write an integer number’
read(*,*)a
If(a.eq.i)then
Write(*,*)a,”it’s not a prime number”
Stop
End if
Do while(a.ge.i)
If(mod(a,i).eq.0)then
Counter=counter+1
End if
End do
If(counter.gt.2)then
Write(*,*)a,” it’s not a prime number”
else
write(*,*)a,” it’s a prime number”
end if
end program prime
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

EXERCISE 3: Assignment for 1st Simpson Rule


program prime
implicit none
integer a,i,acounter
write(*,*) ‘Write an integer number’
read(*,*)a
If(a.eq.i)then
Write(*,*)a,”it’s not a prime number”
Stop
End if
Do while(a.ge.i)
If(mod(a,i).eq.0)then
Counter=counter+1
End if
End do
If(counter.gt.2)then
Write(*,*)a,” it’s not a prime number”
else
write(*,*)a,” it’s a prime number”
end if
end program prime
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

General Knowledge
read(stream, label [, end=end][, err=err]) list

write(stream, label) list

• stream is a number previously linked to a file, or a character variable, or *, where * here indicates
the default value, usually the screen of a terminal session. If stream is a character variable, the
result of the write is stored in that variable, and can be manipulated as such within the program.
• label is the number of a format statement, or * for free format.
• list is a list of items to be transferred, separated by commas, possibly including text strings
enclosed in quotation marks.
• The optional items end and err are so that you can provide statement labels end and err to which
control moves in the event that the end of data is reached prematurely (end) , or some error is
encountered (err).
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Input/Output with format

Do i=1,100
write(*,1) i,i*i,i**3
1 format(i4,i6,i8)
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Input/Output with format


Program formato
Implicit none

character *30 nship


real lpp,breadth,depth,dwt

nship=“Tanker ship *”
lpp=114,0
breadth=14,8
depth=8,1
dwt=6500,00
nprop=1

write(*,10)nship,lpp,breadth,depth,dwt,nprop
10 format(“ship:”,3x,a30,/,&
(“lpp:”,3x,f10.2,/,&
(“b:”,3x, f10.2,/,&
(“d:”,3x, f10.2,/,&
(“dwt:”,3x, f10.2,/,&
(“number of props:”,3x,i2)

End program formato


INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Arrays
TEST YOUR RESULT!
i=1
X=0,0+0,25*0,25
X=0,0625

i=2
X=0,0625+1,2*1,2
X=1,5025

i=3
X=1,5025+0,2*0,2
X=1,5425
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Arrays
Inside of the matrix:
3 # of rows
4 # of columns

! User enters the input for every value of the


matrix

Format
4 f 5.2
4 # of columns of information in the matrix

Fix the Format:


INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Arrays
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Arrays
Dimension icont(10) Single column with 10 values

Dimension x(0:10) Single column with 11 values

Dimension t(15,20) Matrix (15 rows , 20 columns)

Dimension p(10,20,30) Matrix(10 elements in x direction, 20 in y


direction, 30 in z direction)

Dimension x(0:10)
Data Data x/0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0/ or
Data x/11*0.0/
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Exercise
Using the data command, assign values to the C constant which is employed in the
DNV formulation, to calculate the effective width of primary elements (girders). In the
following table values of the constant C are presented, taken from the mentioned
classification rules, where r is the number of secondary elements (stiffeners) which
transmit the load from the plate to the primary elements.
The user must input the ratio a/b, and the number of stiffeners, and the program print
on the screen the effective width. For an interpretation of the geometry, see the
following figure, taken from the mentioned reference.
INTRODUCTION TO FORTRAN PROGRAMMING IN THE VISUAL STUDIO
ENVIRONMENT

Take a look on this website:

https://rules.dnvgl.com/docs/pdf/DNV/rulesship/2011-01/ts302.pdf
STRUCTURED PROGRAMMING 2

It is possible to solve problems dividing them in subproblems.

These are two kinds:


• Functions
and
• Subroutines
STRUCTURED PROGRAMMING

Functions

Advantages:

• The code to implement the calculation can be written just


once, but used many times.
• The functions can be tested separately from the rest of
the program to make sure they give the correct result; we
can then be confident when using them in the larger
program.
STRUCTURED PROGRAMMING

Functions

program name
implicit none
[specification part]
[execution part]
contains
[functions]
end program name

function name(arg1, arg2, ....)


[declarations, including those
for the arguments]
[executable statements]
end function [name]
STRUCTURED PROGRAMMING
Functions

Specification part

Execution part

Function #1

Function #2
STRUCTURED PROGRAMMING

Functions
STRUCTURED PROGRAMMING

Exercise

Taylor Series Expansion:

Вам также может понравиться