29
Examples Introduction to Scientific & Engineering Computing

Examples Introduction to Scientific & Engineering Computing

  • View
    220

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Examples Introduction to Scientific & Engineering Computing

Examples

Introduction to Scientific & Engineering Computing

Page 2: Examples Introduction to Scientific & Engineering Computing

Program simpmathReal::x,yPrint*,»Please enter x and y values»Read*, x, yPrint*,x,» in ve », y,» un toplamı = «, x+yPrint*, x, « un ve « , y , « un farkı =«, x-yPrint*,x,» in ve «,y,» in carpımı = «, x*y Print*,x,» in ve «,y,» in bolumu = «,x/yPrint*,x,» in karekoku= «,sqrt(x),»y nin karekoku= «,sqrt(y),» karekoklerı toplamı ise= «,sqrt(x)+sqrt(y)End Program simpmath

Page 3: Examples Introduction to Scientific & Engineering Computing

Write a program to calculate cumulative sums of the integers from 1 through n (assume n has been assigned a value elsewhere)123…------- =n*(n+1)/2program toplamlarinteger::i,toplamtopla=0do i=1,100 topla=topla+1 print *,"1den ",i,"ye kadar sayilar toplami=",toplaenddoendprogram toplamlar

Page 4: Examples Introduction to Scientific & Engineering Computing

Program cumulative_sum integer ::i, n, sumdo sum = 0 print*,”Please enter a number to calculate cumulative of sum, to exit (0)” read*,nif (n==0) then exit endifdo i = 1, n sum = sum + i write(*,*) “i =“, i, “sum =“, sum end do end doend Program cumulative_sum

Page 5: Examples Introduction to Scientific & Engineering Computing

Write a program to find the sum of the successive odd integers

1, 3, ..., 399.

Page 6: Examples Introduction to Scientific & Engineering Computing

Write a program for a library in order to find the location of a book arranged with

respect to the first letter of its title. Do not neglect the titles beginning with a number.

"A":"F“ on the shelf 10."G":"L“ on the shelf 20."M":"R“ on the shelf 30."S":"Z“ on the shelf 40.Others on the shelf 50.

Select the correct Fortran arithmetic expression for the given algebraic expression.1)abx + cy + 3da) a * b ** x + c ** y + 3 * d b) a * b * x + c * y + 3d c) ab**x + c**y + 3 * d d) abx + cy + d*3 e) None of them

Page 7: Examples Introduction to Scientific & Engineering Computing

program library_case character(len=1)::title !Why (len=1)? print*,“Please enter the title of the book you are searching for:" read*,title select case (title) case("A":"F") print*,"You can find it on the shelf 10." case ("G":"L") print*,"You can find it on the shelf 20." case ("M":"R") print*,"You can find it on the shelf 30." case ("S":"Z") print*,"You can find it on the shelf 40." case default print*,"You can find it on the shelf 50." end select end program library_case

Page 8: Examples Introduction to Scientific & Engineering Computing

Calculate the surface area of a cylinder

Surface Area = (2 • π • r²) + (2 • π • r • height)

Volume   =   π • r² • height   =   ¼ • π • d² • height

Page 9: Examples Introduction to Scientific & Engineering Computing

program cylinder! Calculate the surface area of a cylinder.! Declare variables and constants.! constants=pi! variables=radius squared and height implicit none ! Require all variables to be explicitly declared integer :: ierr character(len=1) :: yn real :: radius, height, area real, parameter :: pi = 3.141592653589793 interactive_loop: do! Prompt the user for radius and height! and read them. write (*,*) "Enter radius and height." read (*,*,iostat=ierr) radius,height

The main reason for learning a programming language is to use the computer to solve…………………. ……… ……………………………… problems

Page 10: Examples Introduction to Scientific & Engineering Computing

! If radius and height could not be read from input,! then cycle through the loop. if (ierr /= 0) then write(*,*) "Error, invalid input." cycle interactive_loop end if! Compute area. The ** means "raise to a power." area = 2 * pi * (radius**2 + radius*height)! Write the input variables (radius, height)! and output (area) to the screen. write (*,"(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)") + "radius=",radius,"height=",height,"area=",area yn = " " yn_loop: do write(*,*) "Perform another calculation? y[n]" read(*,"(a1)") yn if (yn=="y" .or. yn=="Y") exit yn_loop if (yn=="n" .or. yn=="N" .or. yn==" ") exit interactive_loop end do yn_loop end do interactive_loop end program cylinder

Page 11: Examples Introduction to Scientific & Engineering Computing

Two examination papers are written at the end of the course. The final mark is either the

average of the two papers, or the average of the two papers and the class record mark (all

weighted equally), whichever is the higher. The program should reads in the class record mark

and the marks of the papers, computes the average, and shows PASS (>= 50%) or FAIL (<

50%).

Page 12: Examples Introduction to Scientific & Engineering Computing

! -------------------------------------------------------------! Two examination papers are written at the end of the course. ! The final mark is either the average of the two papers, or! the average of the two papers and the class record mark (all! weighted equally), whichever is the higher. The program! should reads in the class record mark and the marks of the! papers, computes the average, and shows PASS (>= 50%) or FAIL (< 50%).! -------------------------------------------------------------

PROGRAM FinalMark IMPLICIT NONE REAL :: Mark1, Mark2 ! the marks of the papers REAL :: Final ! the final marks REAL :: ClassRecordMark ! the class record mark REAL, PARAMETER :: PassLevel = 50.0 ! the pass level

PRINT*,"Please enter ClassRecordMark, Mark1, Mark2" READ(*,*) ClassRecordMark, Mark1, Mark2 Final = (Mark1 + Mark2) / 2.0 IF (Final <= ClassRecordMark) THEN Final = (Mark1 + Mark2 + ClassRecordMark) / 3.0 END IF WRITE(*,*) 'Class Record Mark : ', ClassRecordMark WRITE(*,*) 'Mark 1 : ', Mark1 WRITE(*,*) 'Mark 2 : ', Mark2 WRITE(*,*) 'Final Mark : ', Final IF (Final >= PassLevel) THEN WRITE(*,*) 'Pass Status : PASS' ELSE

WRITE(*,*) 'Pass Status : FAIL' END IFEND PROGRAM FinalMark

Page 13: Examples Introduction to Scientific & Engineering Computing

There had been given SO2 air pollution data of Izmir in “izmso2.dat” file like the following table. Read the data from file. Write a program to calculate average and sum of the SO2 for yearly and monthly. Show the result and also write it a file (“izmso2.son”).

Month 2000 2001 2002 2003 2004 Average Sum

January 17 8 5 9 9

February 16 10 5 7 10

March 8 5 7 7 5

April 8 10 6 7 4

May 6 2 3 4 3

June 2 4 3 3 3

July 4 2 2 3 3

August 3 4 3 1 5

September 4 4 2 2 4

October 5 5 5 4 6

November 5 8 5 5 6

December 9 9 7 5 6

Average

Sum

Page 14: Examples Introduction to Scientific & Engineering Computing

program aveofso2character (len=9),dimension(15)::title_col,title_rowreal,dimension(12,5)::so2real,dimension(8)::ave_colreal,dimension(15)::ave_rowinteger::sutun,satir,i,j,row_num,col_num,barow_num=12col_num=5!the file description and reading the first row (titles) open(unit=1,file="f:\Belgelerim\dersnotlari\fortran\so2.txt",status="old",action="read") read(unit=1,fmt=*)title_col(1:col_num+3)!reading data from file

Names used to identify programs, constants, variables, etc. Identifiers must begin with a letter. This can be followed by up to ….. …. letters, digits, underscores

Page 15: Examples Introduction to Scientific & Engineering Computing

do i=1,row_num read(unit=1,fmt=*)title_row(i),so2(i,1:col_num) end do read(unit=1,fmt=*)title_row(row_num+1:row_num+2)!calculating the sum do i=1,row_num ave_col(i)=sum(so2(i,1:col_num)) end do!calculating the averages do i=1,row_num ave_row(i)=sum(so2(i,1:col_num)) enddo!printig on screen print"(a10,3x,8a8))",title_col(1:col_num+3) do i=1,row_num print"(a,8f8.2)",title_row(i),so2(i,1:col_num),ave_row(i)/col_num,ave_row(i) enddo print"(a,8f8.2))",title_row(row_num),ave_row(1:col_num+2)

Page 16: Examples Introduction to Scientific & Engineering Computing

open(unit=2,file="f:\Belgelerim\dersnotlari\fortran\so2.ort",status="unknown", action="write")!writing the file write(unit=2,fmt="(a10,8a8)")title_col(1:col_num+3) do i=1,row_num write(unit=2,fmt="(a,8f8.2)")title_row(i),so2(i,1:col_num),ave_row(i)/col_num,ave_row(i) enddo write(unit=2,fmt="(a,8f8.2)")title_row(row_num),ave_row(1:col_num+2) end program aveofso2

Page 17: Examples Introduction to Scientific & Engineering Computing

1.Write a program which will read up to 20 integer numbers and print them in the reverse order to that in which they were typed.program invers_order integer::i integer, dimension(20)::j do i=1,20 read*,j(i) end do do i=20,1,-1 print*,j(i) enddoend program invers_order

Page 18: Examples Introduction to Scientific & Engineering Computing

1.Write a program which produces a table of sin x and cos x for angles x from 0° to 360° in steps of 5°.

program tableofsincosinteger::ido i=0,360,5print*,"For degrees ",i," sin =",sin(i*1.0)," ","cos =",cos(i*1.0)enddoend program tableofsincos

Page 19: Examples Introduction to Scientific & Engineering Computing

1.A chemist makes fifty measurements of the rates of three different reactions like below. Write a program that calculates the average of each reaction itself and all of them using an array. Reac.1 Reac.2 Reac.3221.3 316.2 164.1233.2 120.6 106.3……. …… …….

Page 20: Examples Introduction to Scientific & Engineering Computing

Write a program, which asks the user student number and print the faculty name of the student attending using first two digits of the number as follows;

(01) Civil Eng. (02) Architecture, (03) Mechanical,(04) Electrical and Electronic, (05) Mines, (06) Chemical and Metallurgical, (07) Naval Arch.(08) Science and Letters, (09) Management(10) Conservatory, (11) Aeronautics and Astronautics, (12) Maritime Faculty(13) Textile Technologies and Design,

Page 21: Examples Introduction to Scientific & Engineering Computing

Write a program to calculate energy of an object. Main program will read the following data.

g=9.81h=0.23 m=1.1v=5

And will print input values, kinetic and potential energy,

A function subprogram calculate only potential energyA subroutine subprogram calculate only kinetic energy

Page 22: Examples Introduction to Scientific & Engineering Computing
Page 23: Examples Introduction to Scientific & Engineering Computing

program ex_1 real, parameter :: pi = 3.141693 ! Exponential form of real numers real, parameter :: e = 1.6e-19, eps = 8.86e-12 integer :: z real :: r, phi !read distance from particle print*, "Enter distance from particle =" read*, r !read charge of particle print*, "Enter charge of particle =" read*, z !calculate Coulomb potential phi = z * e / (4.0 * pi * eps * r) !print results with print print*, "Coulomb Potential at a distance",r, "from a particle with a charge of",& z, "= " , phiend program ex_1

Page 24: Examples Introduction to Scientific & Engineering Computing
Page 25: Examples Introduction to Scientific & Engineering Computing

print *, j,". ogrencinin notunu giriniz"read *, notif (not>=0.and.not<=100) then

exitelse

print *, "uygun aralikta not girin 0-100"cycle

end ifend doselect case (not)

case(85:100)!print *, "A"harf="A"

case(70:84)harf="B"

case(60:69)harf="C"

case(45:59)harf="D"

case(0:44)harf="F"

case defaultprint *, "uygun aralikta sayi girmediniz"

end selectprint *, "girilen notun", not, " harf karsiligi=", harfend doend program notlar

program notlarinteger :: not,icharacter(len=1)::harfprint *, "sinif mevcudunu giriniz"read *, ido j=1,ido

Page 26: Examples Introduction to Scientific & Engineering Computing

(Use starting values n = 4.14159265Є = 0.00001a = 2.5 )

Page 27: Examples Introduction to Scientific & Engineering Computing

module motion public :: harmonic real,public :: n, eps, a contains subroutine harmonic(t,pos,vel,acc) real,intent(in)::t real,intent(out)::pos,vel,acc pos=a*sin(n*t+eps) vel=n*a*cos(n*t+eps) acc=-a*n**2*sin(n*t+eps) end subroutine harmonicend module motionprogram eqn use motion real :: t n=4.14159265 eps=0.01 a=2.5print *, "please give the value of t :"read *,tcall harmonic(t,pos,vel,acc)print *, "position is", posprint *, "velocity is", velprint *, "acceleration is", accend program eqn

Page 28: Examples Introduction to Scientific & Engineering Computing

1. You are given the following data file named “input.dat”:0.0245436930.0306796160.0368155390.0429514630.0490873860.0552233090.0613592320.0674951560.0736310790.0797670020.0859029250.0920388490.098174772Write a FORTRAN program that will read this file treating each line as a separate data point, and then evaluate the function f x : x ex sin x for every point. The results should be written to the screen in two columns: one with x and the other one with f x . Also calculate and print out the average value obtained from all function evaluations. The function f xshould be implemented as a FUNCTION.

Page 29: Examples Introduction to Scientific & Engineering Computing