VBA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

A

: TANG MINH QUANG : 11T1121Y : 4 30 : 5 7


C H I B A U N I V E R S I T Y N A N O S C I E N C E D E P A R T M E N T

1.
Microsoft Office Excel VBA

2.
a 2 L y

1() 1() 2 S1 S2 2 P1 () P2 ()

3.
Comment Eg. 'Calculate wave number k


2.5

1.5

0.5

0 -6 -4 -2 0 2 4 6

CODE: Sub PROGRAM01() Const a As Double = 0.1, l As Double = 200, lambda As Double = 0.0005 Const nsource As Integer = 2 Dim ysource(nsource - 1) As Double Const ymax As Double = 5, dy As Double = 0.1 Dim y As Double Dim ny As Integer Dim iy As Integer Dim Intensity As Double ' JE^

Dim i As Integer, j As Integer Dim k As Double, p1 As Double, p2 As Double ' Set the source position ysource(0) = -a / 2 ysource(1) = a / 2 'Calculate wave number k k = 2 * WorksheetFunction.Pi / lambda ny = Int((2 * ymax) / dy) + 1 For iy = 1 To ny y = -ymax + (iy - 1) * dy Intensity = 0 For i = 0 To nsource - 1 p1 = k * Sqr(l ^ 2 + (y - ysource(i)) ^ 2) For j = 0 To nsource - 1 p2 = k * Sqr(l ^ 2 + (y - ysource(j)) ^ 2) 'Using Tave function to calculate intensity Intensity = Intensity + TAVE(p1, p2) Next Next ' Write data value back to cells Cells(4 + iy, 1) = y Cells(4 + iy, 2) = Intensity

Next End Sub Function Tave Function TAVE(p1 As Double, p2 As Double) As Double TAVE = Cos(p1 - p2) / 2 End Function ////////////////////////////////// 2

(N=05)
14 12 10 8 6 4 2 0 -20 -15 -10 -5 0 5 10 15 20

(N=10)
60 50 40 30 20 10 0 -20 -15 -10 -5 0 5 10 15 20

(N=20)
250

200

150

100

50

Code: Sub PROGRAM02() Declare variables Const a As Double = 0.01, l As Double = 200, lambda As Double = 0.0005 Const nsource As Integer = 5 Const nsource2 As Integer = 10 Const nsource3 As Integer = 15 Dim ysource(nsource - 1) As Double Dim ysource2(nsource2 - 1) As Double Dim ysource3(nsource3 - 1) As Double Const ymax As Double = 15, dy As Double = 0.1 Dim y As Double Dim ny As Integer Dim iy As Integer Dim Intensity As Double Dim i As Integer, j As Integer Dim k As Double, p1 As Double, p2 As Double

-20

-15

-10

-5

10

15

20

'Set value for slip position For i = 0 To nsource - 1 ysource(i) = i * a - (nsource - 1) / 2 * a Next For i = 0 To nsource2 - 1 ysource2(i) = i * a - (nsource2 - 1) / 2 * a Next For i = 0 To nsource3 - 1 ysource3(i) = i * a - (nsource3 - 1) / 2 * a Next k = 2 * WorksheetFunction.Pi / lambda ny = Int((2 * ymax) / dy) + 1 For iy = 1 To ny ' yW y = -ymax + (iy - 1) * dy Intensity = 0 For i = 0 To nsource - 1 p1 = k * Sqr(l ^ 2 + (y - ysource(i)) ^ 2) For j = 0 To nsource - 1 p2 = k * Sqr(l ^ 2 + (y - ysource(j)) ^ 2) Intensity = Intensity + TAVE(p1, p2) Next Next Cells(4 + iy, 4) = y Cells(4 + iy, 5) = Intensity 'Repeat the calculation with different number of light sources (Copy code from ******** aboved) 'nsource=10

Intensity = 0 For i = 0 To nsource2 - 1 p1 = k * Sqr(l ^ 2 + (y - ysource2(i)) ^ 2) For j = 0 To nsource2 - 1 p2 = k * Sqr(l ^ 2 + (y - ysource2(j)) ^ 2) Intensity = Intensity + TAVE(p1, p2) Next Next

' ZyWxo Cells(4 + iy, 6) = Intensity 'nsource=15 Intensity = 0 For i = 0 To nsource3 - 1 p1 = k * Sqr(l ^ 2 + (y - ysource3(i)) ^ 2) For j = 0 To nsource3 - 1 p2 = k * Sqr(l ^ 2 + (y - ysource3(j)) ^ 2) Intensity = Intensity + TAVE(p1, p2) Next Next Cells(4 + iy, 7) = Intensity Next End Sub Function TAVE(p1 As Double, p2 As Double) As Double TAVE = Cos(p1 - p2) / 2 End Function ////////////////////////////////// 3

3
1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 -6 -4 -2 0 2 4 6

Code: Sub PROGRAM03() Declare variables Const a As Double = 0.1, l As Double = 200, lambda As Double = 0.0005, w As Double = 0.2 Nsource is big source/split , 100 small sources in side Const nsource As Integer = 2, m As Integer = 100 Dim ysource(2 * m - 1) As Double Const ymax As Double = 5, dy As Double = 0.1 '## vZp[^ ## Dim y As Double Dim ny As Integer Dim iy As Integer

Dim Intensity As Double Dim i As Integer, j As Integer, g As Integer Dim k As Double, p1 As Double, p2 As Double Set up position for the light source ( divided into upper and lower part) For i = 0 To m - 1 ysource(i) = (a + w) / 2 - w * i / 99 Next For i = m To 2 * m - 1 ysource(i) = (-1) * (a - w) / 2 - w * i / 99 Next 'Calculate wave number k k = 2 * WorksheetFunction.Pi / lambda ny = Int((2 * ymax) / dy) + 1 For iy = 1 To ny y = -ymax + (iy - 1) * dy Intensity = 0 For i = 0 To nsource - 1 p1 = k * Sqr(l ^ 2 + (y - ysource(i)) ^ 2) For j = 0 To nsource - 1 p2 = k * Sqr(l ^ 2 + (y - ysource(j)) ^ 2) 'Using Tave function to calculate intensity Intensity = Intensity + TAVE(p1, p2) Next

Next ' Write data value back to cells Cells(4 + iy, 1) = y Cells(4 + iy, 2) = Intensity Next End Sub Function Tave Function TAVE(p1 As Double, p2 As Double) As Double TAVE = Cos(p1 - p2) / 2 End Function

4.
(1) 2 (Young)

2 2 S1S2 P l x 2 P () l2 l1

5.


1) FrontPage - http://www.sci.keio.ac.jp/gp/

You might also like