-
For Loop issues
VB is returning an issue with my "Next S". What I am trying to do have my function "Psi" return an expression of X dependent on which cellular compartment is the target (T) and which is the Source (S).
For example... if we are in T=1 and S=1, I want Psi(1,1,X,Rc,Rn)= f(x)
Can I have everything written in the same routine? Or should I have a sub routine and each expression as a function?
Thanks for the help!!
Function Psi(T, S, X, Rc, Rn)
Rc = CellRadius
Rn = NuclearRadius
For T = 1 To CellDim '(Target)
For S = 1 To CellDim '(Source)
'For Nucleus to Nucleus
If 0 <= X <= 2 * Rn Then
Psi(T, S, X, Rc, Rn) = 1 - (3 / 4) * (X / Rn) + (1 / 16) * (X / Rn) ^ 3 'T=1,S=1
nmin = 0
nmax = 2 * Rn
Else
Psi(T, S, X, Rc, Rn) = 0 'T=1,S=1
nmin = 0
nmax = 0
End If
Next S
'For Cytoplasm to Nucleus
If (Rc <= 3 * Rn) And (0 <= X < Rc - Rn) Then
Psi(T, S, X, Rc, Rn) = (3 / (4 * X * (Rc ^ 3 - Rn ^ 3))) * X ^ 2 * ((Rn ^ 2) - (1 / 12) * X ^ 2) 'T=1,S=2
nmin = 0
nmax = Rc - Rn
ElseIf (Rc <= 3 * Rn) And ((Rc - Rn) <= X < (2 * Rn)) Then
Psi(T, S, X, Rc, Rn) = (3 / (4 * X * (Rc ^ 3 - Rn ^ 3))) * ((1 / 2) * (Rc ^ 2 - Rn ^ 2) * (Rn ^ 2 - X ^ 2) + (2 * X / 3) * (Rc ^ 3 - Rn ^ 3) - (1 / 4) * (Rc ^ 4 - Rn ^ 4))
nmin = Rc - Rn
nmax = 2 * Rn
ElseIf (Rc >= 3 * Rn) Then
Psi(T, S, X, Rc, Rn) = (Rn ^ 3) / (Rc ^ 3 - Rn ^ 3)
nmin = 0
nmax = Rc
ElseIf (Rc <= 3 * Rn) And ((2 * Rn) <= X <= (Rc + Rn)) Then
Psi(T, S, X, Rc, Rn) = ((3 / (4 * X * (Rc ^ 3 - Rn ^ 3))) / 12) * (X ^ 4 - (3 * (Rc ^ 4 + Rn ^ 4)) + 6 * (Rc ^ 2 * Rn ^ 2 - (X ^ 2 * Rn ^ 2) - (X ^ 2 * Rc ^ 2)) + 8 * X * (Rc ^ 3 + Rn ^ 3))
nmin = 2 * Rn
nmax = Rc + Rn
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
Next S
'For CellSurface to Nucleus
If (Rc - Rn) <= X <= (Rc + Rn) Then
Psi(T, S, X, Rc, Rn) = ((2 * X * Rc) - (Rc ^ 2) - X ^ 2 + (Rn ^ 2)) / (4 * X * Rc) 'T=1,S=3
nmin = Rc - Rn
nmax = Rc + Rn
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
Next S
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cell to Nucleus T=1, S=4
nmin = 0
nmax = 0
End If
Next T 'Target=Cytoplasm
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cytoplasm or Cell Surface
nmin = 0
nmax = 0
Next T 'Target=Cell Surface
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cell Surface
nmin = 0
nmax = 0
Next T 'Now on Target=Cell
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Nucleus to Cell
nmin = 0
nmax = 0
Next S
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cytoplasm to Cell
nmin = 0
nmax = 0
Next S
If 0 <= X <= (2 * Rc) Then
Psi(T, S, X, Rc, Rn) = (1 / 2) + Rc / (4 * X)
nmin = 0
nmax = 2 * Rc
End If
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
Next S
If 0 <= X <= 2 * Rc Then
Psi(T, S, X, Rc, Rn) = 1 - (3 / 4) * (X / Rc) + (1 / 16) * (X / Rc) ^ 3
nmin = 0
nmax = 2 * Rc
End If
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
End Function
-
Re: For Loop issues
It would help if you would wrap your code with code tags.
What are some example input values for these variables: T, S, X, Rc, Rn?
And the expected results.
-
Re: For Loop issues
Annie
It would also help if you used indents.
I have done this below on the first few lines to give you the idea.
Code:
Function Psi(T, S, X, Rc, Rn)
Rc = CellRadius
Rn = NuclearRadius
For T = 1 To CellDim '(Target)
For S = 1 To CellDim '(Source)
'For Nucleus to Nucleus
If 0 <= X <= 2 * Rn Then
Psi(T, S, X, Rc, Rn) = 1 - (3 / 4) * (X / Rn) + (1 / 16) * (X / Rn) ^ 3 'T=1,S=1
nmin = 0
nmax = 2 * Rn
Else
Psi(T, S, X, Rc, Rn) = 0 'T=1,S=1
nmin = 0
nmax = 0
End If
Next S
'For Cytoplasm
Spoo
-
Re: For Loop issues
And it would also help to know what you mean by : VB is returning an issue with my "Next S". ... that could be a number of things... although, I think we can rule out your fingers falling off as a cause since VB wouldn't have anyting to do with that.
-tg
-
Re: For Loop issues
Here is the code reposted with proper indents
Code:
For T = 1 To CellDim '(Target)
For S = 1 To CellDim '(Source)
'For Nucleus to Nucleus
If 0 <= X <= 2 * Rn Then
Psi(T, S, X, Rc, Rn) = 1 - (3 / 4) * (X / Rn) + (1 / 16) * (X / Rn) ^ 3 'T=1,S=1
nmin = 0
nmax = 2 * Rn
Else
Psi(T, S, X, Rc, Rn) = 0 'T=1,S=1
nmin = 0
nmax = 0
End If
Next S ' This one would be ok
'For Cytoplasm to Nucleus
If (Rc <= 3 * Rn) And (0 <= X < Rc - Rn) Then
Psi(T, S, X, Rc, Rn) = (3 / (4 * X * (Rc ^ 3 - Rn ^ 3))) * X ^ 2 * ((Rn ^ 2) - (1 / 12) * X ^ 2) 'T=1,S=2
nmin = 0
nmax = Rc - Rn
ElseIf (Rc <= 3 * Rn) And ((Rc - Rn) <= X < (2 * Rn)) Then
Psi(T, S, X, Rc, Rn) = (3 / (4 * X * (Rc ^ 3 - Rn ^ 3))) * ((1 / 2) * (Rc ^ 2 - Rn ^ 2) * (Rn ^ 2 - X ^ 2) + (2 * X / 3) * (Rc ^ 3 - Rn ^ 3) - (1 / 4) * (Rc ^ 4 - Rn ^ 4))
nmin = Rc - Rn
nmax = 2 * Rn
ElseIf (Rc >= 3 * Rn) Then
Psi(T, S, X, Rc, Rn) = (Rn ^ 3) / (Rc ^ 3 - Rn ^ 3)
nmin = 0
nmax = Rc
ElseIf (Rc <= 3 * Rn) And ((2 * Rn) <= X <= (Rc + Rn)) Then
Psi(T, S, X, Rc, Rn) = ((3 / (4 * X * (Rc ^ 3 - Rn ^ 3))) / 12) * (X ^ 4 - (3 * (Rc ^ 4 + Rn ^ 4)) + 6 * (Rc ^ 2 * Rn ^ 2 - (X ^ 2 * Rn ^ 2) - (X ^ 2 * Rc ^ 2)) + 8 * X * (Rc ^ 3 + Rn ^ 3))
nmin = 2 * Rn
nmax = Rc + Rn
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
Next S this would need to be Next T
'For CellSurface to Nucleus
If (Rc - Rn) <= X <= (Rc + Rn) Then
Psi(T, S, X, Rc, Rn) = ((2 * X * Rc) - (Rc ^ 2) - X ^ 2 + (Rn ^ 2)) / (4 * X * Rc) 'T=1,S=3
nmin = Rc - Rn
nmax = Rc + Rn
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
Next S ' Invalid
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cell to Nucleus T=1, S=4
nmin = 0
nmax = 0
End If
Next T 'Target=Cytoplasm 'Invalid
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cytoplasm or Cell Surface
nmin = 0
nmax = 0
Next T 'Target=Cell Surface 'Invalid
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cell Surface
nmin = 0
nmax = 0
Next T 'Now on Target=Cell 'Invalid
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Nucleus to Cell
nmin = 0
nmax = 0
Next S 'Invalid
Psi(T, S, X, Rc, Rn) = 0 'Nothing for Cytoplasm to Cell
nmin = 0
nmax = 0
Next S 'Invalid
If 0 <= X <= (2 * Rc) Then
Psi(T, S, X, Rc, Rn) = (1 / 2) + Rc / (4 * X)
nmin = 0
nmax = 2 * Rc
End If
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
Next S 'Invalid
If 0 <= X <= 2 * Rc Then
Psi(T, S, X, Rc, Rn) = 1 - (3 / 4) * (X / Rc) + (1 / 16) * (X / Rc) ^ 3
nmin = 0
nmax = 2 * Rc
End If
Else
Psi(T, S, X, Rc, Rn) = 0
nmin = 0
nmax = 0
End If
There are lots of issues there. I have marked several of them, there are also a few extra Else and End IFs there that should not be
The proper way to created a nested for next loop is
Code:
For S= LowNumber to HighNumber
For T = LowNumber to HighNumber
' do some stuff
Next T
Next S
You can not add a bunch more next statements there must be 1 next statement for 1 for statement and they must be in the proper order.
-
Re: For Loop issues
Not sure that
Code:
If 0 <= x <= 2 * Rn Then
is doing what you think it is. I can't see how it's being evaluated but it seems to always return True irrespective of the values of x and Rn.
I assume you're testing whether x lies between 0 and 2 * Rn inclusive, in which case you have to be very specific
Code:
If 0 <= x And x <= 2 * Rn Then
Same applies for statements such as
Code:
If (Rc <= 3 * Rn) And (0 <= X < Rc - Rn) Then
which could be
Code:
If (Rc <= 3 * Rn) And (0 <= X And X < Rc - Rn) Then
-
Re: For Loop issues
On re-reading your first post, whilst not understanding the Science involved, I think you are attempting to return a value which is a Function of X, Rc and Rn for given values of S and T. Perhaps, Psi(T, S) = f(X, RC, RN) i.e. for each combination of T and S there is exactly one result.
That being the case I'd expect a calling routine to establish the values of X, RC and RN and call the function for various combinations of T and S
e.g.
In the Calling Routine
Code:
Dim lngS As Long
Dim lngT As Long
Dim sngX As Single
Dim sngResult As Single
Dim sngRC As Single
Dim sngRN As Single
sngRC = CellRadius
sngRN = NuclearRadius
For lngT = 1 To CellDim
For lngS = 1 To CellDim
sngX = <some value>
sngResult = Psi(lngT, lngS, sngX, sngRC, sngRN)
'
' Do whatever you want to do with the Result here
' e.g. sngResult for Psi(1, 1, X, Rc, Rn) would be
' 0 or
' whatever (1 - (3 / 4) * (X / Rn) + (1 / 16) * (X / Rn) ^ 3) evaluates to
' Depending on the values of X, RC and RN
'
Next lngS
Next lngT
The Function Psi might look like:
Code:
Option Explicit
Private Function Psi(lngT As Long, lngS As Long, sngX As Single, sngRC As Single, sngRN As Single) As Single
'
' Here you'd return the appropriate value for a given T and S combination depending upon the values of RC, RN and X
' For example
'
Select Case lngT
Case 1
Select Case lngS
Case 1
'
' T = 1, S = 1
'
Select Case True
Case 0 <= sngX And sngX <= 2 * sngRN
Psi = 1 - (3 / 4) * (sngX / sngRN) + (1 / 16) * (sngX / sngRN) ^ 3
nmin = 0
nmax = 2 * sngRN
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
Case 2
'
' T = 1, S = 2
'
Select Case True
Case sngRC <= 3 * sngRN And (0 <= sngX And sngX < sngRC - sngRN)
Psi = (3 / (4 * sngX * (sngRC ^ 3 - sngRN ^ 3))) * sngX ^ 2 * ((sngRN ^ 2) - (1 / 12) * sngX ^ 2)
nmin = 0
nmax = sngRC - sngRN
Case sngRC <= 3 * sngRN And ((sngRC - sngRN) <= sngX And sngX < sngRC - sngRN)
Psi = (3 / (4 * sngX * (sngRC ^ 3 - sngRN ^ 3))) * ((1 / 2) * (sngRC ^ 2 - sngRN ^ 2) * (sngRN ^ 2 - sngX ^ 2) + (2 * sngX / 3) * (sngRC ^ 3 - sngRN ^ 3) - (1 / 4) * (sngRC ^ 4 - snfRn ^ 4))
nmin = sngRC - sngRN
nmax = 2 * sngRN
Case (sngRC >= 3 * sngRN)
Psi = (sngRN ^ 3) / (sngRC ^ 3 - sngRN ^ 3)
nmin = 0
nmax = sngRC
Case (sngRC <= 3 * sngRN) And ((2 * sngRN) <= sngX And sngX <= (sngRC + sngRN))
Psi = ((3 / (4 * sngX * (sngRC ^ 3 - sngRN ^ 3))) / 12) * (sngX ^ 4 - (3 * (sngRC ^ 4 + sngRN ^ 4)) + 6 * (sngRC ^ 2 * sngRN ^ 2 - (sngX ^ 2 * sngRN ^ 2) - (sngX ^ 2 * sngRC ^ 2)) + 8 * sngX * (sngRC ^ 3 + sngRN ^ 3))
Max = sngRC + sngRN
nmin = 2 * sngRN
nmax = sngRC + sngRN
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
'
' Insert other Case statements for values of S
' for which results need to be calculated when T = 1
' e.g.
Case 3
'
' T = 1, S = 3
'
End Select ' This marks the end of results for all values of S where T = 1
'
' Insrt here other Case statements for values of T
'
Case 2
Select Case lngS
Case 1
'
' Code for result when T = 2, S = 1
'
Case 2
'
' Code for result when T = 2, S = 2
'
End Select ' End of results for all values of S where T = 2
End Select ' this marks the end of all results
End Function
Whether you elect to use Select Case/ End Select or If/ElseIf/Else/End If is a matter of style but I personally think it's more readable with Select Case. (That's not an invitation for anyone to start a war BTW :):) )
Or, of course, I might have completely misinterpreted the original requirement...........
(E&OE)
-
Re: For Loop issues
is it possible to upload the complete project ?
I find it difficult figuring out what CellDim is set at and other things.
cheers.
-
Re: For Loop issues
Tried to upload the entire file but it said it was an "invalid file".
I made the changes suggested by Doogle with the cases. The only reservation I have is that this X is not a set value. It represents a step-value from zero to the energy of the electron that I am using. Does this code define it as a set value. One of the following steps I will take is to multiply (via function "combo") this Psi function we've been talking about by another function, which I call "SP" (calculates the stopping power). When compiling I get an error "By Ref Argument type mismatch on the "lngT" in the Function Combo of the Psi(lngT)
Thank you for all your help and patience!!
Function SP(ERange, X)
'Calculates the Stopping Power for the electrons at the range of the electron minus the integral step(X).
SP = 3.333 * ((ERange - X) + 0.007) ^ -0.435 + 0.0055 * (ERange - X) ^ 0.33
End Function
'This function works correctly, I was able to test it for set values of the electron range (ERange) and X (step).
Function Combo(lngT, lngS, lngX, sngRC, sngRN, ERange)
'Dim X As Long
lngX = 0
Combo(lngT, lngS, lngX, sngRC, sngRN, ERange) = SP(ERange, lngX) * Psi(lngT, lngS, lngX, sngRC, sngRN) * 1 / Ee
End Function
Function Psi(lngT As Long, lngS As Long, lngX As Single, sngRC As Single, sngRN As Single) As Single
'
' Here you'd return the appropriate value for a given T and S combination depending upon the values of RC, RN and X
' For example
'
sngRC = CellRadius
sngRN = NuclearRadius
Select Case lngT
Case 1
Select Case lngS
Case 1
'
' T = 1, S = 1
'
Select Case True
Case 0 <= lngX And lngX <= 2 * sngRN
Psi = 1 - (3 / 4) * (lngX / sngRN) + (1 / 16) * (lngX / sngRN) ^ 3
nmin = 0
nmax = 2 * sngRN
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
Case 2
'
' T = 1, S = 2
'
Select Case True
Case sngRC <= 3 * sngRN And (0 <= lngX And lngX < sngRC - sngRN)
Psi = (3 / (4 * lngX * (sngRC ^ 3 - sngRN ^ 3))) * lngX ^ 2 * ((sngRN ^ 2) - (1 / 12) * lngX ^ 2)
nmin = 0
nmax = sngRC - sngRN
Case sngRC <= 3 * sngRN And ((sngRC - sngRN) <= lngX And lngX < sngRC - sngRN)
Psi = (3 / (4 * lngX * (sngRC ^ 3 - sngRN ^ 3))) * ((1 / 2) * (sngRC ^ 2 - sngRN ^ 2) * (sngRN ^ 2 - lngX ^ 2) + (2 * lngX / 3) * (sngRC ^ 3 - sngRN ^ 3) - (1 / 4) * (sngRC ^ 4 - snfRn ^ 4))
nmin = sngRC - sngRN
nmax = 2 * sngRN
Case (sngRC >= 3 * sngRN)
Psi = (sngRN ^ 3) / (sngRC ^ 3 - sngRN ^ 3)
nmin = 0
nmax = sngRC
Case (sngRC <= 3 * sngRN) And ((2 * sngRN) <= lngX And lngX <= (sngRC + sngRN))
Psi = ((3 / (4 * lngX * (sngRC ^ 3 - sngRN ^ 3))) / 12) * (lngX ^ 4 - (3 * (sngRC ^ 4 + sngRN ^ 4)) + 6 * (sngRC ^ 2 * sngRN ^ 2 - (lngX ^ 2 * sngRN ^ 2) - (lngX ^ 2 * sngRC ^ 2)) + 8 * lngX * (sngRC ^ 3 + sngRN ^ 3))
Max = sngRC + sngRN
nmin = 2 * sngRN
nmax = sngRC + sngRN
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
Case 3
'
' T = 1, S = 3
'
Select Case True
Case sngRC - sngRN <= lngX And lngX <= sngRC + sngRN
Psi = ((2 * lngX * sngRC) - (sngRC ^ 2) - lngX ^ 2 + (sngRN ^ 2)) / (4 * lngX * sngRC)
nmin = sngRC - sngRN
nmax = sngRC + sngRN
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
Case 4
'
' T=1, S=4
'
Psi = 0
nmin = 0
nmax = 0
End Select ' This marks the end of results for all values of S where T = 1
'
' Insrt here other Case statements for values of T
'
Case 2
Select Case lngS
Case 1
'
' Code for result when T = 2, S = 1
'
Psi = 0
nmin = 0
nmax = 0
Case 2
'
' Code for result when T = 2, S = 2
'
Psi = 0
nmin = 0
nmax = 0
Case 3
'
' T = 2, S = 3
'
Psi = 0
nmin = 0
nmax = 0
Case 4
'
' T=2, S=4
'
Psi = 0
nmin = 0
nmax = 0
End Select ' End of results for all values of S where T = 2
Case 3
Select Case lngS
Case 1
'
' Code for result when T = 3, S = 1
'
Psi = 0
nmin = 0
nmax = 0
Case 2
'
' Code for result when T = 3, S = 2
'
Psi = 0
nmin = 0
nmax = 0
Case 3
'
' T = 3, S = 3
'
Psi = 0
nmin = 0
nmax = 0
Case 4
'
' T=3, S=4
'
Psi = 0
nmin = 0
nmax = 0
End Select
Case 4
Select Case lngS
Case 1
'
' Code for result when T = 4, S = 1
'
Psi = 0
nmin = 0
nmax = 0
Case 2
'
' Code for result when T = 4, S = 2
'
Psi = 0
nmin = 0
nmax = 0
Case 3
'
' T = 4, S = 3
'
Select Case True
Case 0 < lngX And lngX = 2 * sngRC
Psi = (1 / 2) - lngX / (4 * lngX)
nmin = 0
nmax = 2 * sngRC
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
Case 4
'
' T=4, S=4
'
Select Case True
Case 0 < lngX And lngX = 2 * sngRC
Psi = 1 - (3 / 4) * (lngX / sngRC) + (1 / 16) * (lngX / sngRC) ^ 3
nmin = 0
nmax = 2 * sngRC
Case Else
Psi = 0
nmin = 0
nmax = 0
End Select
End Select
End Select ' this marks the end of all results
End Function
-
Re: For Loop issues
If you need X to vary then you could establish an outer loop
Code:
For sngX = <some starting value> To <some ending value> Step <some increment>
For lngT = 1 To CellDim
For lngS = 1 To CellDim
sngResult = Psi(lngT, lngS, sngX, sngRC, sngRN)
'
' Do whatever you want to do with the Result here
' e.g. sngResult for Psi(1, 1, X, Rc, Rn) would be
' 0 or
' whatever (1 - (3 / 4) * (X / Rn) + (1 / 16) * (X / Rn) ^ 3) evaluates to
' Depending on the values of X, RC and RN
'
Next lngS
Next lngT
Next sngX
-
Re: For Loop issues
Doogle, thank you so much!
So I didn't put limits on X before because those limits will depend on the ERange (range of the electron), and I will have to integrate (using the trapezoidal rule). I had written as follows:
Function CalculatePhi(T As Double, S As Double) As Double
Dim X As Long, Phi() As Double, i As Double, Combo() As Double
ReDim Phi(S, T), Combo(T, S, X, Rc, Rn)
X = 0
Phi(T, S) = 0
For i = 0 To nmax
X = i * 1 / 1000 'step size is 1 nm(everything else is in micrometers)
Phi(T, S) = Phi(T, S) + ((X + 1 / 100) - X) * (Combo(i + 1 / 1000) - Combo(i)) / 2
Next i
End Function
-
Re: For Loop issues
The reason you're seeing the compile errors is because you're not defining your variables and they are all defaulting to Variants.
Case in point, Function SP expects two arguments; since you haven't defined their type they will default to Variants. If you call that function from Function Combo, passing lngX (which is defined as a Long type) it will report a Type Mismatch (i.e. Type Variant <> Type Long)
I think I'd be surprised if:
Code:
Function Combo(lngT, lngS, lngX, sngRC, sngRN, ERange)
'Dim X As Long
lngX = 0
Combo(lngT, lngS, lngX, sngRC, sngRN, ERange) = SP(ERange, lngX) * Psi(lngT, lngS, lngX, sngRC, sngRN) * 1 / Ee
End Function
works. I think I'd have expected an 'Out of Stack Space' error when the Function is called.
Code:
Function Combo(lngT, lngS, lngX, sngRC, sngRN, ERange)
'Dim X As Long
lngX = 0
Combo = SP(ERange, lngX) * Psi(lngT, lngS, lngX, sngRC, sngRN) * 1 / Ee
End Function
would be OK.
EDIT: Just realised that you have an Array named Combo so your original code is ok. However, I would have thought that the compiler would have got bothered about a Function having the same name as an Array. Perhaps it's cleverer than I thought.
-
Re: For Loop issues
BTW you should use code tags to enclose your code here. It retains the original formatting and is much easier to read i.e.
[code]
Your code goes here
[/code]
You should be able to edit your previous post to insert the tags and then copy and paste the original code back in.
(Quite a few people wont look at code if it's not been formatted corectly so you may miss out on some helpful suggestions)
EDIT: To attach a file or files it's best to .zip them and attach the .zip file. (With the emphasis on .zip as opposed to .rar as not everyone has the ability to open .rar files - me included)
-
1 Attachment(s)
Re: For Loop issues
Attachment 101291
Is the complete code. All that I am working on currently is in "Module 1". If you want to run through the entire code:
1. put in some initial activity, "cellular level" and either mouse or human data
2. press "nucleus" and "saved data" and "Dose Point", "Activity", "Done"
3. Input for the cell radius/cell nucleus
4. Input a radionuclide e.g. "Gallium"
Otherwise an outline of the part of the code I am trying to develop is calculate and "S-value" When "Dose Point" is selected. The steps to include this are:
1. Psi Function
Inputs: Rc (micrometers), Rn (micrometers), X(step) (micrometers)
Outputs: Cellular compartment (T, S) (e.g. nucleus, cytoplasm), Psi function = F(Rc,Rn,X), nmin, nmax
2. SP function
Inputs: ERange (electron range in micrometers), X(step) (micrometers)
Outputs: SP(X)
3. "Combo" = (Psi*SP where SP is evaluated at the ERange-X
Inputs: Range, Step, Rc,Rn, Electron energy (range)
Outputs: cellular compartment, the product value
4. "Calculate Phi" integrates the previous function from nmin to nmax
Inputs: nmin, nmax (integration limits), step (X), and cell compartment
Output: value of this integration "Phi"(T,S) for each compartment
5. Calculate the S-Value
Inputs: Electron Energy, "Phi(T,S)", mass of the compartment
Output: S-Value(T, S)
Thanks! And I will put the code into the suggested brackets in the future.
-
Re: For Loop issues
Annie
To reinforce Doogle's suggestion regarding use of code tags, let me
suggest two ways to do it.
1. Manually typing in the tags (as Doogle did in post #12
2. Select and wrap. To do this,
... 1. enter your code as you did in post #11
... 2. press the Go Advanced button at the lower right of the Reply window.
... 3. select the code ,, ie, drag your mouse from the beginning to end of your code
... 4. then press the # icon on the menu bar .. it is on row 2, 3rd from the right. It adds the wrappers
Alternatively, you can press the # icon first, then, paste your code between them.
Code:
Function CalculatePhi(T As Double, S As Double) As Double
Dim X As Long, Phi() As Double, i As Double, Combo() As Double
ReDim Phi(S, T), Combo(T, S, X, Rc, Rn)
X = 0
Phi(T, S) = 0
For i = 0 To nmax
X = i * 1 / 1000 'step size is 1 nm(everything else is in micrometers)
Phi(T, S) = Phi(T, S) + ((X + 1 / 100) - X) * (Combo(i + 1 / 1000) - Combo(i)) / 2
Next i
End Function
You might also consider using indents
Code:
Function CalculatePhi(T As Double, S As Double) As Double
Dim X As Long, Phi() As Double, i As Double, Combo() As Double
ReDim Phi(S, T), Combo(T, S, X, Rc, Rn)
X = 0
Phi(T, S) = 0
For i = 0 To nmax
X = i * 1 / 1000 'step size is 1 nm(everything else is in micrometers)
Phi(T, S) = Phi(T, S) + ((X + 1 / 100) - X) * (Combo(i + 1 / 1000) - Combo(i)) / 2
Next i
End Function
Spoo
-
Re: For Loop issues
i cannot find Module 1. correct files ?
@radioactiveannie
Do you have any input and expected output values we can use for each of the routines just to make sure we are using the correct variable types. cheers.
I just seen your function :
Function Psi(T, S, X, Rc, Rn)
A function returns a value, we have not in this case. do we require a return value ?
We also need the variable types for each variable in the function eg :
if we are just processing and no value is needed after the processing we change the name Function to a sub
Code:
Function Psi(T as double, S as double, X as double, Rc as double, Rn as double) ' error a function must return a value.
Function Psi(T as double, S as double, X as double, Rc as double, Rn as double) as double ' correct - a function has a return value
Psi = any calculation ' Psi is the same name as the functions name with a return value as a double
End Function
Sub Psi(T as double, S as double, X as double, Rc as double, Rn as double) ' OK we not returning a value for other processing.
debug.print anything here ' we are just doing stuff or printing - no output required
End Sub
' example function
Code:
Function ThisIsMyAddFunct( FirstVariable as integer, SecondVariable as integer) as Integer
' Set the return Value which in this case is an Integer
ThisIsMyAddFunct = FirstVariable + SecondVariable
EndFunction
' example subroutine
SubThisIsMyAddSub( FirstVariable as integer, SecondVariable as integer)
' Do the work and just print it out
Debug.print FirstVariable + SecondVariable
End Sub
' example 2 that uses a function
Sub ThisIsMyLazyAddSub( FirstVar as integer, SecondVar as integer)
dim Answer as integer
Answer = ThisIsMyAddFunct( FirstVar , SecondVar )
' Let ThisIsMyAddFunct function do the work I will just print it out
Debug.print Answer
End Sub