goto keyword in matlab [closed] - vb.net

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this function in vb which I want to write in matlab. Does anyone know how this could be written using for loops instead of the goto keyword?
FOR i = 1 TO 101
X(i) = (i - 1) * .5 / 50:
X(i) = X(i) / 2 / PI / F:
NEXT i
j = 6
870 FOR i = 1 TO 100
IF X(i) <= j * .005 AND X(i + 1) >= j * .005 THEN GOTO 890 ELSE GOTO 1000
890 AZ4(j) = AY4(i) + (AY4(i + 1) - AY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
BZ4(j) = BY4(i) + (BY4(i + 1) - BY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
CZ4(j) = CY4(i) + (CY4(i + 1) - CY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
DZ4(j) = DY4(i) + (DY4(i + 1) - DY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
EZ4(j) = EY4(i) + (EY4(i + 1) - EY4(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
AZT(j) = AYT(i) + (AYT(i + 1) - AYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
BZT(j) = BYT(i) + (BYT(i + 1) - BYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
CZT(j) = CYT(i) + (CYT(i + 1) - CYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
DZT(j) = DYT(i) + (DYT(i + 1) - DYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
EZT(j) = EYT(i) + (EYT(i + 1) - EYT(i)) / (X(i + 1) - X(i)) * (j * .005 - X(i))
i = 101
1000 NEXT i
j = j + 1
IF j * .005 > X(101) THEN GOTO 1040
GOTO 870
1040 FOR i = 1 TO 126
AY4(i) = AZ4(i): BY4(i) = BZ4(i): CY4(i) = CZ4(i): DY4(i) = DZ4(i): EY4(i) = EZ4(i)
AYT(i) = AZT(i): BYT(i) = BZT(i): CYT(i) = CZT(i): DYT(i) = DZT(i): EYT(i) = EZT(i)
NEXT i
FOR i = 1 TO 126
X(i) = i * .005
NEXT i

I will write just the skeleton of the code, the rest of the statements must be completed by you. I will use ii and jj instead of i and j because these have special meaning in MATLAB (complex square root of -1).
for ii = 1:101
% statements
end;
jj = 6;
while true
for ii = 1:100
if (cond1) && (cond2) % replace with actual conditions
% statements from label 890
break;
end;
end;
ii = 101; % useless, but...
jj = jj+1;
if cond3 % replace with actual condition
break;
end;
end;
for ii = 1:126
% statements
end;
for ii = 1:126
% statements
end;

Related

For Statement in assigning random values to integers in VBA

NN1 = Int((500 - 100 + 1) * Rnd + 100)
NN2 = Int((500 - 100 + 1) * Rnd + 100)
NN3 = Int((500 - 100 + 1) * Rnd + 100)
NN4 = Int((500 - 100 + 1) * Rnd + 100)
I assume that we can reduce this into
For i = 1 to 4
"NN" & i = Int((500 - 100 + 1) * Rnd + 100)
Next i
However, I am getting a compilation error. How do I rectify this?
Dim NN() As Integer
For i = 1 To 4
NN(i) = Int((500 - 100 + 1) * Rnd + 100)
Next i
I would suggest using an Array instead of multiple Integers

VBA appears to leave a for loop without cause?

I have a for loop (the last loop in the code below) which fills some arrays with values through some computations.
However, for some reason, once i=5 it jumps back up to the top of the loop (the x+h part) without going through the rest of the loop.
While x < xmax
If x + h < xmax Then 'If the step is going to overshoot the desired xmax
x = x + h 'make h adequately smalller
Else
h = xmax - x
x = xmax
End If
'k(Order #, equation #)
For j = 1 To 6 'First to 6th order
'temp=riddersmethodT(temp) 'Calculate temperature of mixture
FT = 0
rho(0) = 0 'Setting FT and rho_av to 0 to be re-calculated
For i = 1 To 7
rho(0) = rho(0) + rho(i) * Y4(i) 'Calculate average density of mixture
FT = FT + Y4(i)
vol_F = vol_F + Y4(i) * MW(i) / rho(i) 'Calculating the total volumetric flowrate (m^3/s)
Next i
rho(0) = rho(0) / FT
For i = 1 To 8 'Calculating all of the k(1) values for eq 1 to 8
k(j, i) = AllODES(x, Y4, i, j, k, h, temp, diameter, vol_F, rho(0))
Next i
Next j
For i = 1 To 8
Y4Old(i) = Y4(i) 'Saving old y4 values to calc delta0
Y4(i) = Y4(i) + h * (k(1, i) * (37 / 378) + k(3, i) * (250 / 621) + k(4, i) * (125 / 594) + k(6, i) * (512 / 1771))
Y5(i) = Y4(i) + h * (k(1, i) * (2825 / 27648) + k(3, i) * (18575 / 48384) + k(4, i) * (13525 / 55296) + k(5, i) * (277 / 14336) + k(6, i) * (0.25))
delta0(i) = error
delta1(i) = Abs(Y5(i) - Y4(i))
delRatio(i) = Abs(delta0(i) / delta1(i)) 'Ratio of errors; careful of getting zeroes!
Next i
I don't understand how this can be possible seeing as i is not being manipulated within that loop. If you have any insight, please let me know!
My guess is that your final loop over i has a divide by zero somewhere. You could handle errors in your loop using something like the following:
Sub yourSub()
For i = 1 To 8
On Error GoTo ErrorHandler:
Y4Old(i) = Y4(i)
'Saving old y4 values to calc delta0
Y4(i) = Y4(i) + h * (k(1, i) * (37 / 378) + k(3, i) * (250 / 621) + k(4, i) * (125 / 594) + k(6, i) * (512 / 1771))
Y5(i) = Y4(i) + h * (k(1, i) * (2825 / 27648) + k(3, i) * (18575 / 48384) + k(4, i) * (13525 / 55296) + k(5, i) * (277 / 14336) + k(6, i) * (0.25))
delta0(i) = error
delta1(i) = Abs(Y5(i) - Y4(i))
delRatio(i) = Abs(delta0(i) / delta1(i)
Next i
Cleanup:
' do cleanup here
Exit Sub
ErrorHandler:
' handle error here
Resume Cleanup
End Sub
But it would be best to fix your match which is allowing a division by zero in the first place.

VBA calling a variable by name inside a loop

Ok here's the deal: I need to do a comprehensive check in a .csv file (comparing the one in my current sheet to an external one). I decided to divide the list into 10 equal sections (deciles). In each decile I choose a random value belonging to that section and use that row number to compare the two sets of data.
Where things fall apart is inside the FOR function. I am looking for a way to go through each decile (starting from rand0) and have VBA check whether the values of the .csv and the Data sheet in this workbook are equal. If they are not - a function (called get_param) is to be executed.
I dont quite understand how to have VBA go through the FOR function from Dec = 0 to 9 - so in essence from row number rand0 to row number rand9 and perform the inequality check (in the second IF function). The rand & Dec part does not work. I am looking for clues on how to fix this or on a new implementation to do the same thing.
A few more details:
n is the number of rows in the .csv file (equal to a couple of thousand).
np is the number of rows in this file (should be equal to n - if not, execute function). ParamLocation is designated automatically - it should be located in a specific location.
Sub check_changes_param()
Dim Dec As Integer
Call public_dims
Call deciles
Set ParamBook = Workbooks.Open(ParamLocation)
'==========CHECKS IF PARAMETERS.xlsm EXISTS IN THE CORRECT LOCATION==========
If ParamLocation = "" Then
MsgBox "The Parameters.xlsm file does not exist or is in the incorrect location. Please ensure it is located in " & ParamLocation
Else
For Dec = 0 To Dec = 9
If ThisWorkbook.Sheets("Data").Cells(rand & Dec, 11) <> ParamBook.Sheets("Data").Cells(rand & Dec, 11) Or n <> np Then
Call get_param
Exit For
End If
Next Dec
End If
End Sub
Public Sub deciles()
rand0 = Int((n / 10) * 1) * Rnd + 1
rand1 = Int((n / 10) * 2 - (n / 10) * 1 + 1) * Rnd + (n / 10) * 1
rand2 = Int((n / 10) * 3 - (n / 10) * 2 + 1) * Rnd + (n / 10) * 2
rand3 = Int((n / 10) * 4 - (n / 10) * 3 + 1) * Rnd + (n / 10) * 3
rand4 = Int((n / 10) * 5 - (n / 10) * 4 + 1) * Rnd + (n / 10) * 4
rand5 = Int((n / 10) * 6 - (n / 10) * 5 + 1) * Rnd + (n / 10) * 5
rand6 = Int((n / 10) * 7 - (n / 10) * 6 + 1) * Rnd + (n / 10) * 6
rand7 = Int((n / 10) * 8 - (n / 10) * 7 + 1) * Rnd + (n / 10) * 7
rand8 = Int((n / 10) * 9 - (n / 10) * 8 + 1) * Rnd + (n / 10) * 8
rand9 = Int(n - (n / 10) * 9 + 1) * Rnd + (n / 10) * 9
End Sub
Try this instead:
Sub check_changes_param()
Dim Dec As Integer
Call public_dims
Dim deciles As Variant
deciles = decilesArray()
Set ParamBook = Workbooks.Open(ParamLocation)
'==========CHECKS IF PARAMETERS.xlsm EXISTS IN THE CORRECT LOCATION==========
If ParamLocation = "" Then
MsgBox "The Parameters.xlsm file does not exist or is in the incorrect location. Please ensure it is located in " & ParamLocation
Else
For Dec = 0 To UBound(deciles)
If ThisWorkbook.Sheets("Data").Cells(deciles(Dec), 11) <> ParamBook.Sheets("Data").Cells(deciles(Dec), , 11) Or n <> np Then
Call get_param
Exit For
End If
Next Dec
End If
End Sub
Public Function decilesArray() As Variant
randomize()
rand0 = Int((n / 10) * 1) * Rnd + 1
rand1 = Int((n / 10) * 2 - (n / 10) * 1 + 1) * Rnd + (n / 10) * 1
rand2 = Int((n / 10) * 3 - (n / 10) * 2 + 1) * Rnd + (n / 10) * 2
rand3 = Int((n / 10) * 4 - (n / 10) * 3 + 1) * Rnd + (n / 10) * 3
rand4 = Int((n / 10) * 5 - (n / 10) * 4 + 1) * Rnd + (n / 10) * 4
rand5 = Int((n / 10) * 6 - (n / 10) * 5 + 1) * Rnd + (n / 10) * 5
rand6 = Int((n / 10) * 7 - (n / 10) * 6 + 1) * Rnd + (n / 10) * 6
rand7 = Int((n / 10) * 8 - (n / 10) * 7 + 1) * Rnd + (n / 10) * 7
rand8 = Int((n / 10) * 9 - (n / 10) * 8 + 1) * Rnd + (n / 10) * 8
rand9 = Int(n - (n / 10) * 9 + 1) * Rnd + (n / 10) * 9
decilesArray= Array(rand0,rand1,rand2,rand3,rand4,rand5,rand6,rand7,rand8,rand9)
End Sub
I propose this correction,
public dim statements should be defined outside sub or functions.
use an array instead of concatenation to refer to a variable, rand & dec is not a correct VBA syntax to address rand0, ... rand9 variables.
Dim rand(9)
Sub check_changes_param()
Dim Dec As Integer, n
' Call public_dims ' put dim instructions as first instructions of this module
n = 10 'modify 10 to reflect correct value
Call deciles(n)
Set ParamBook = Workbooks.Open(ParamLocation)
'==========CHECKS IF PARAMETERS.xlsm EXISTS IN THE CORRECT LOCATION==========
If ParamLocation = "" Then
MsgBox "The Parameters.xlsm file does not exist or is in the incorrect location. Please ensure it is located in " & ParamLocation
Else
For Dec = 0 To 9
If ThisWorkbook.Sheets("Data").Cells(rand(Dec), 11) <> ParamBook.Sheets("Data").Cells(rand(Dec), 11) Or n <> np Then
Call get_param
Exit For
End If
Next Dec
End If
End Sub
Public Sub deciles(n)
rand(0) = Int((n / 10) * 1) * Rnd + 1
For i = 1 To 8
rand(i) = Int((n / 10) * (i + 1) - (n / 10) * (i + 1) + 1) * Rnd + (n / 10) * i
Next
rand(9) = Int(n - (n / 10) * 9 + 1) * Rnd + (n / 10) * 9
End Sub

Laravel use reference from SQL

I use the SQL command SELECT AS to retrieve some calculated results from my query in Laravel 5.1
How can i later in this query use my new referces to calculate something?
I want to do something like this to get the average rating.
SELECT (rating_food + rating_service + rating_decor)/3 as rating_total
.
$query->leftJoin('reviews', 'restaurant_id', '=', 'restaurants.id');
$query->addSelect([
'restaurants.*',
DB::raw('avg(reviews.rating_food) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_food'),
DB::raw('avg(reviews.rating_service) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_service'),
DB::raw('avg(reviews.rating_decor) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_decor')
]);
(this is taken from a scope.)
I found this solution.
Add it as a FROM query.
$query->from(DB::raw('
(
SELECT
avg(reviews.rating_food) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_food,
avg(reviews.rating_service) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_service,
avg(reviews.rating_decor) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_decor
FROM restaurants
JOIN reviews ON reviews.restaurant_id = restaurants.id
)
as rating, restaurants
'));

Algorithm For Finding Greenwich Mean Sidereal Time Having Problems

I have currently gotten an algorithm to work for finding the Julian Day for my current location, but when using this value to proceed in finding the Greenwich Mean Sidereal Time, I get some very funky numbers. Can anyone run this script and maybe determine where my calculations go wrong? Thanks.
#1/user/bin/python
import math
from time import gmtime, strftime
#Sidereal Time Program
#Julien Date Converter
seconds = (int(strftime("%S")) * .01)
JD1 = ((367 * (int(strftime("%Y")))) - ((7 * ((int(strftime("%Y")))
+ (((int(strftime("%m"))) + 9) / 12))) / 4)
+ ((275 * (int(strftime("%m")))) / 9) + (int(strftime("%d"))) + 1721013.5
+ ((int(strftime("%I")) + (seconds) + 4) / 24) - 0.5 + 0.5 + 1.46)
JD2 = ((367 * (int(strftime("%Y")))) - ((7 * ((int(strftime("%Y")))
+ (((int(strftime("%m"))) + 9) / 12))) / 4)
+ ((275 * (int(strftime("%m")))) / 9) + (int(strftime("%d"))) + 1721013.5
+ ((int(12) + 4) / 24) - 0.5 + 0.5 - 0.192361555)
H = JD1 - JD2
JD = JD2 + (H / 24)
D1 = JD1 - 2451545.0
D2 = JD2 - 2451545.0
T = D1 / 36525
GMST1 = 6.697374558 + (0.06570982441908 * D2) + (1.00273790935 * H) + (0.000026 * (T * T))
GMST2 = 18.697374558 + (24.06570982441908 * D1)
o = 125.04 - (0.052954 * D1)
L = 280.47 + (0.98565 * D1)
e = 23.4393 - (0.0000004 * D1)
x = 2 * L
sym = (-0.000319 * (math.sin (o))) - (0.000024 * (math.sin (x)))
eqeq = (sym * (math.cos (e)))
GAST1 = GMST1 + eqeq
GAST2 = GMST2 +eqeq
print (JD1)
print (T)
print (GAST1)
print (GAST2)
Edit: Here is the formula I am using: http://aa.usno.navy.mil/faq/docs/GAST.php
It appears that this is a package that will do what you want. See, e.g., here.