Sie sind auf Seite 1von 7

# Johan Saltos Montalvo

Applied Programming I
Coursework 3

1.- Prepare a source code in Fortran that shows the area under a curve, using the First and
Second Simpson Rule, using arrays. Choose any function.

Program area

Implicit none

Integer :: a,b,c,x0,xf,n,r,cont=0,i,h

real :: x(20),y(20)

real :: p1

real :: p2

real :: p3

real :: s

## write(*,*) " Input c"

write(*,*) " Do you want to use the 1st or 2nd Simpson's Rule? Input 1 or 2"

## write(*,*) " Input the final value of x"

Johan Saltos Montalvo

if (r==1) then

do

write(*,*) " Input the number of points you want to use for the area"

cont=cont+1

if(mod(n,2)==0) exit

## write(*,*) " Try again"

if (cont>0) cycle

end do

## write(*,*) " You did it on",cont," times"

h=(xf-x0)/n

x(1)=x0

x(n+1)=xf

do i=2,n

x(i)=x(i-1)+h

end do

do i=1,n+1

write(*,*) "x",i,":",x(i)

end do

write(*,*)

do i=1,n+1

y(i)=a*(x(i)**2)+b*(x(i))+c

end do
Johan Saltos Montalvo

do i=1,n+1

write(*,*) "y",i,":",y(i)

end do

p1=0

do i=1,n+1

if ((i.eq.1).or.(i.eq.n+1)) then

p1= y(i)+p1

end if

end do

p2=0

do i=2,n

if (mod(i,2)==0) then

p2= 4*y(i)+p2

else

p2= 2*y(i)+p2

end if

end do

s=(h*(p1+p2))/3

write(*,*) "The area under the curve of the function using the First Simpsons's Rule is: ",s

## else if (r==2) then

do

write(*,*) " Input the number of points you want to use for the area"

cont=cont+1
Johan Saltos Montalvo

if(mod(n,3)==0) exit

## write(*,*) " Try again"

if (cont>0) cycle

end do

## write(*,*) " You did it on",cont," times"

h=(xf-x0)/n

x(1)=x0

x(n+1)=xf

do i=2,n

x(i)=x(i-1)+h

end do

do i=1,n+1

write(*,*) "x",i,":",x(i)

end do

write(*,*)

do i=1,n+1

y(i)=a*(x(i)**2)+b*(x(i))+c

end do

do i=1,n+1

write(*,*) "y",i,":",y(i)

end do
Johan Saltos Montalvo

p1=0

do i=1,n+1

if ((i.eq.1).or.(i.eq.n+1)) then

p1= y(i)+p1

end if

end do

p2=0

do i=2,n

if (mod(i-1,3)==0) then

p2= 2*y(i)+p2

end if

end do

p3=0

do i=3,n+1

if (mod(i-1,3)/=0) then

p3= 3*y(i)+p3

end if

end do

s=(3*h)*(p1+p2+p3)/8

write(*,*) "The area under the curve of the function using the Second Simpsons's Rule is: ",s

else

write(*,*) "error"

end if

## end program area

Johan Saltos Montalvo

2.- According to your class notes, let´s do some research about what do the commands:
dimension and data; in the point 1,4 page 9. Let`s prepare 02 exercises about the
implementation of this commands in Fortran. You´ll have to present them in classes to the
group.

Arrays
An array is a set of scalar elements that have the same type and kind parameters. Any object
that is declared with an array specification is an array. Arrays can be declared by using a type
declaration statement, or by using a DIMENSION, COMMON, ALLOCATABLE, POINTER, or
TARGET statement.

The DIMENSION attribute specifies maximum rate values, and optionally, minimum, of each of
the dimensions. Thus, dimension 1 or d1
from general before syntax is replaced by [Lower_limit_d1] and [Upper_limit_d1] similarly for
other dimensions, if any.
If only the upper limit is identified, the lower limit is 1 by Default.

Data type
An array can have any intrinsic or derived type. The data type of an array (like any other
variable) is specified in a type declaration statement or implied by the first letter of its name.
All elements of the array have the same type and kind parameters. If a value assigned to an
individual array element is not the same as the type of the array, it is converted to the array’s
type.

Examples:

1.-
Program sum
Implicit none
Integer :: i,sum
Real :: v(20)

Dimension v(5)
Data v/1,2,3,4,5/
sum=0
do i=1,5
sum=sum+v(i)
end do
write(*,*) “The total sum is: “ ,sum
end program sum

2.-
Program factorial
Implicit none
Integer :: i,fact
real :: h(30)

dimension h(6)
data h/1,2,3,4,5,6/
fact=1
do i=1,6
fact=fact*h(i)
Johan Saltos Montalvo

end do
write(*,*) “The factorial is: “,fact
end program fact

Bibliography

Digital Equipment Corporation, Digital Fortran Language Reference Manual. Digital Equipment
Corporation, 1997.

## Departamento de matemática aplicada y ciencias de computación. Escuela Técnica Superior de

Ingenieros Industriales y de Telecomunicación. Universidad de Cantabria, 2008. Fundamentos
de informática y programación en Fortran