MATLAB: Do I get different results from the same math in two languages

iterationvba

I want to translate a script in VBA to Matlab.
Here's the original script in VBA:
Function tau(h, b, override)
nMax = 60
mMax = 60
If (override = 0) Then
h = 2 * h
tau = 0
For n = -nMax To nMax
For m = -mMax To mMax
If (n = 0) And (m = 0) Then
Else
tau = tau + (-1) ^ (n + m) * 1 / (n * n + (m * h / b) * (m * h / b)) ^ (1.5)
End If
Next
Next
tau = tau * 0.5 * (h / b / 3.14159265358979) ^ 1.5
Else
tau = override
End If
End Function
This script returns tau as -0.242648
My Matlab equivalent returns 0.085214. Here's the script:
function tau = FindTau(h,b,nMax,mMax)
h = 2*h;
tau = 0;
for n = -nMax:nMax
for m = -mMax:mMax
if n ~= 0 && m ~= 0
tau = tau + (-1) ^ (n + m) * 1 / (n * n + (m * h / b) * (m * h / b)) ^ (1.5);
end
end
end
tau = tau * 0.5 * (h / b / 3.14159265358979) ^ 1.5;
end
I don't understand what I'm doing wrong. For nMax = mMax = 60, h = 3.048, and b = 5.4864, I am getting two different results with the same math from two different languages. Any help would be greatly appreciated.
Thanks, Kat

Best Answer

One difference is that in your BVA code you have this conditional:
If (n = 0) And (m = 0) Then
and in your MATLAB code you have the opposite conditional:
if n ~= 0 && m ~= 0
So that your BVA code executes the code in the block if both ‘n’ and ‘m’ equal zero, and your MATLAB code executes the code in the block if both ‘n’ and ‘m’ are not equal to zero.
I have no recent experience with VBA, but I suspect that you are testing for entirely different conditions in each code.