Trigonometric calculations are sometimes correct and sometimes wrong

Trigonometric calculations are sometimes correct and sometimes wrong,When I assign h to 7 or 8, the program gets an error, and when h is greater than 9, the program can run

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
array_realsolardata1 = 
[
	[-189,-218,-246,-273,-301,-327,-354,-380,-405,-430,-455,-479,-502,-525,-547,-568,-589,-609,-628,-647,-665,-682,-698,-714,-728,-742,-755,-779,-790,-799,-817],
	[-824,-830,-836,-841,-845,-849,-851,-853,-854,-855,-854,-853,-851,-848,-845,-841,-836,-831,-824,-818,-810,-802,-793,-784,-774,-763,-752,-741,-728],
	[-716,-703,-689,-675,-661,-647,-632,-616,-601,-585,-568,-552,-535,-518,-501,-484,-466,-449,-431,-413,-395,-377,-358,-340,-322,-304,-285,-267,-249,-231,-213],
	[-196,-178,-161,-144,-127,-110, -93, -77, -61,  46,  30,  16,   1,  13,  27,  41,  54,  66,  79,  91, 102, 113, 124, 134, 143, 153, 161, 169, 177, 184],
	[ 190, 196, 201, 206, 210, 217, 216, 219, 220, 222, 222, 222, 222, 221, 219, 217, 214, 211, 207, 203, 198, 193, 187, 181, 174, 167, 159, 151, 142, 133, 124],
	[ 114, 104,  94,  83,  72,  60,  48,  36,  24,  12,   1,  14,  39,  52, -65, -78, -91,-105,-117,-130,-143,-156,-168,-181,-193,-205,-217,-229,-240,-251],
	[-262,-273,-283,-293,-302,-311,-320,-328,-336,-343,-350,-356,-362,-368,-372,-376,-380,-383,-385,-387,-389,-389,-389,-389,-388,-386,-384,-381,-377,-373,-368],
	[-363,-357,-351,-344,-336,-328,-319,-310,-300,-290,-279,-267,-255,-242,-229,-216,-201,-187,-171,-156,-140,-123,-107, -89, -72,  54,  35,  17,   2,  21,  41],
	[  60,  80, 100, 121, 141, 162, 183, 183, 204, 225, 246, 267, 288, 310, 331, 353, 374, 395, 417, 438, 459, 480, 501, 522, 542, 562, 582, 602, 621, 640],
	[ 659, 678, 696, 696, 713, 731, 748, 764, 780, 796, 796, 811, 825, 839, 853, 866, 878, 890, 901, 912, 921, 931, 940, 948, 955, 961, 967, 972, 976, 980, 982],
    [984, 985, 985, 984, 983, 981, 977, 973, 969, 963, 956, 949, 941, 932, 922, 911, 900, 887, 874, 860, 846, 830, 814, 797, 779, 760, 741, 721, 700, 678],
  [656, 633, 609, 585, 561, 535, 509, 483, 456, 429, 402, 374, 346, 317, 288, 259, 230, 201, 171, 142, 112,  82,  52,  23,   7, -37, -66, -96,-125,-154,-183],
];
MonthOffset = [0,31,59,90,120,151,181,212,243,273,304,334]
PI = 3.1415926
time = Time.new
y = time.year
m = time.month
d = time.day
h =19
m1 = time.min
s = time.sec
if y%4==0&&(y%100!=0||y%400==0)&&m>2
   day=1
else
   day=0
end  #If it's a leap year, add one to the total number of days after February
Nd = MonthOffset[m-1]+d+day   #Calculate the total number of days in a year
totalsecond1 = h*3600+m1*60+s #Calculates the total number of seconds at the current time of the day
meansolardata1 = totalsecond1+(119.58-120)*240 #Calculate mean solar time
realsolartime1 = meansolardata1+array_realsolardata1[m-1][d-1] #Calculate true solar time
w = ((realsolartime1/3600)-12)*15  #solar hour angle
th = 23.45*Math::sin(2*PI*(284+Nd)/365) #Declination Angle
ElevationAngle = Math::asin(Math::sin(31.47*PI/180)*Math::sin(th*PI/180)+Math::cos(31.47*PI/180)*Math::cos(th*PI/180)*Math::cos(w*PI/180))*180/PI #ElevationAngle
AzimuthAngle = Math::asin(Math::cos(th*PI/180)*Math::sin(w*PI/180)/Math::cos(ElevationAngle*PI/180))*180/PI+180 #AzimuthAngle 
d1=2024.mm
d3=4000.mm
if AzimuthAngle<180
   direction=-1
   A1=90-Math::atan((Math::tan(ElevationAngle*PI/180))/(Math::sin((180-AzimuthAngle)*PI/180)))*180/PI  #The flat uniaxial theory tracks the Angle
   B_flag = Math::sin((90-A1)*PI/180)*d3/d1
   if B_flag<1
      B = Math::asin(sin((90-A1)*PI/180)*d3/d1)*180/PI+A1-90 #Inverse tracking Angle B
      if B<=0
         B = 0
      end
   else
      B=90
   end
else
   direction = 1
   A1=-90-Math::atan((Math::tan(ElevationAngle*PI/180))/(Math::sin((180-AzimuthAngle)*PI/180)))*180/PI #The flat uniaxial theory tracks the Angle
   B_flag = Math::sin((90+A1)*PI/180)*d3/d1
   if B_flag<1
      B = -Math::asin(Math::sin((90+A1)*PI/180)*d3/d1)*180/PI+A1+90 #Inverse tracking Angle
      if B>=0
         B = 0
      end
   else
      B=90
   end
   B=-1*B
   A=-1*A1
end
p B
p A1
m0=B.abs
m1=45
m2=A1.abs
if m0>m1
  mb=m0
  m0=m1
  m1=mb
end
if m0>m2
  mb=m0
  m0=m2
  m2=mb
end
target_angle=m0*direction

… and you can clearly see the error and its location in the Ruby Console

Error: #<NoMethodError: undefined method `sin' for main:Object>
<main>:47:in `<main>'
SketchUp:1:in `eval'

So there is an syntax error here:

.

      B = Math::asin(sin((90-A1)*PI/180)*d3/d1)*180/PI+A1-90 #Inverse tracking Angle B
                      ^
.

Correctly:

.

      B = Math::asin(Math::sin((90-A1)*PI/180)*d3/d1)*180/PI+A1-90 #Inverse tracking Angle B
                      ^
.



Beside this you maybe realized a warning message in Ruby Console like

<main>:61: warning: already initialized constant B

This is because you are using constant assignment. A constant is a type of variable which always starts with a capital letter. Constants are used for values that aren’t supposed to change, but Ruby doesn’t prevent you from changing them, but you will see this warning message.
You can find more for example here.https://www.rubyguides.com/2017/07/ruby-constants/

I recommend using local variables starting with non-uppercase letter if you want to change the value.



There is a “better” PI already in Ruby Math` module

Math::PI
>>3.141592653589793
6 Likes

Thanks