Demo entry 6787533

Earldom

   

Submitted by Earl on Apr 12, 2019 at 15:48
Language: Fortran. Code size: 3.2 kB.

1 	!PY_PTK_Trapezoid_Rule_Fortran95.f95
2 	program trap
3 	implicit none 
4 	double precision:: a,b,DeltaX,DeltaX2,AllTraps,MidTraps,c,x,Formula,cc
5 	integer:: counter,n,ResultCondition,TwoBe

6 	print*, 'input first and last term then number of trapezoid'
7 	MidTraps=0  
8 	AllTraps=0
9 	read*,a,b,n
10 	if (b-a==0) then
11 	print*, "Integral Value = 0"
12 	stop 
13 	end if
14 	if(n==0) then
15 	print*,"Invalid numbers of trapezoids"
16 	stop
17 	end if
18 	if(b-a.GT.0)then
19 	TwoBe=1
20 	else 
21 	TwoBe=-1
22 	end if 

23 	print*,'Do you want to see all the sum values? Insert 0 for No and 1 for Yes'
24 	print*,'(Not having to print decrease compute time)'
25 	read*,ResultCondition
26 	DeltaX=(b-a)/n
27 	DeltaX2=DeltaX/2
28 	counter=1

29 	open(unit=1,file='Report.txt') 
30 	write(1,*)'Initial Integral    ',a 
31 	write(1,*)'End Integral    ',b
32 	write(1,*)'Number of Terms',n

33 	!!! To change you formula please change at the "Formula=YourFormula"
34 	!!!Using x as a variable 
35 	!!! And please apply the same to MATLABFormula with one ' before YourFormula and ' after 
36 	!!! using Z=YourFormula using captial X as the variable and end with ; 

37 	write(1,*)'The Fourmula is sin(x)+1'
38 	write(1,*)"The value of Delta x is: ",DeltaX
39 	write(1,*)"Delta x divided by two is",DeltaX2

40 	do x=a,b,DeltaX
41 	Formula=sin(X)+1	
42 	if(TwoBe==-1)then
43 	Formula=Formula*(-1)
44 	end if  

45 	!First IF condition
46 	if(x==a .or. x==b) then
47 	MidTraps=Formula
48 	if(ResultCondition==1) then 
49 	print*,'=============================================================='
50 	print*
51 	print*,'This is the value number: ',counter 
52 	print*,'This is the value when x equal: ',x    
53 	end if
54 	end if 

55 	!Second IF condition
56 	if ((x<b .and. x>a) .or. (x>b .and. x<a)) then     
57 	MidTraps=2*(Formula)
58 	if(TwoBe==-1)then
59 	MidTraps=Midtraps*(-1)
60 	end if  
61 	if(ResultCondition==1) then
62 	print*,'=============================================================='
63 	print*,'This is the value number: ',counter 
64 	print*,'This is the value when x equal: ',x    
65 	end if   
66 	end if

67 	write(1,*)'This is the value for the trapezoid term number', counter, ' is', Midtraps

68 	!Riemann Sum adding up
69 	AllTraps=AllTraps+MidTraps         

70 	!print the value of the current trapezoid
71 	if(ResultCondition==1) then
72 	print*,'                         ',MidTraps
73 	end if
74 	!increase the counter by 1 for the report.txt
75 	counter=counter+1
76 	end do    

77 	!MATLAB code section

78 	!Final Calculation for Trapezoid Rule
79 	AllTraps=AllTraps*DeltaX2   
80 	!Print the End result to the console command
81 	print*
82 	print*,' The result of definite integral from your given range and function'
83 	print*,AllTraps

84 	if(ResultCondition==1) then
85 	print*,"The value of Delta x is: ", DeltaX
86 	print*,"Delta x divided by two is",DeltaX2
87 	end if 

88 	!write the End result to the console command to report.txt
89 	write(1,*)" This is the value for the term sum up: ",Alltraps

90 	!THE END
91 	end program trap

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).