If you had me in class, follow this blog, follow my twitter account, or just talked to me for more than five minutes you will know I am a BIG fan of MATLAB. I love it! I love the IDE. It’s a wonderful programming language for a physicist who doesn’t care about programming languages. I know there are a number of real coders that don’t like it. For real programming (classes, inheritance, etc) there are definitely better languages out there. But if you just need to build a script to solve a differential equation there ain’t anything better that MATLAB – according to this fairly unsuccessful physicist.
But there is a problem. Matlab costs $$$$$$! The student edition is only $100, which I highly recommend and in fact have made mandatory for my classes. Seriously, throw the text book away and just get MATLAB. HeHe.
But every time I enter in a new semester and I am starting to prepare for class, I remember that not using open source material is exclusionary for those folks that can’t afford the extra resources. So last night I opened up glowscript and tried to make my first lab.
NO BREAK POINTS! It felt like I was coding in the 1980s. AND everything was in 3D, which is usually super hard in coding land. It turns out 2D is harder in glowscript??? The problem is that I pretty dumb and can only think in 2D. There were lots of four letter words and I am sure I broke many rules (such as using global variables) because nothing made sense at first. (which is always the case when starting a new platform.)
HOWEVER, I LIKED IT. It was fun! I love that it is super easy to publish your animated code and get a shareable link. Like BAM! EASY! I was coding last night and refused to go to bed!
See my lame program here. This mimics one of the labs we do in our physics 1 class. WARNING, its very LAME:
https://www.glowscript.org/#/user/mwright/folder/MyPrograms/program/AirTableForceLab
The code for it is below, ugh. I am not proud of this. Maybe I should added some comments too.
GlowScript 3.0 VPython
grav = 9.8
imass1 = vector(-0.2, 0.15, 0)
imass2 = vector(0.43, -0.1, 0)
iline1 = vector(0.07, 0.16, 0)
iline2 = vector(0.43, 0.0, 0)
sline1 = vector(0.6, 0.02, 0.05)
sline2 = vector(0.02, 0.2, 0.05)
disk1 = cylinder (pos=vector(0.35, 0.1, -0.025), axis=vector(0,0,0.05), radius=0.09, color = color.green)
myTable = box (pos=vector(0, 0, 0), size=vector(0.7, 0.2, 0.05), color = color.white)
leg1 = box (pos=vector(-0.3, -0.25, 0), size=vector(0.1, 0.3, 0.05), color = color.white)
leg2 = box (pos=vector(0.3, -0.25, 0), size=vector(0.1, 0.3, 0.05), color = color.white)
line1 = box (pos=iline1, size=sline1, color = color.blue)
line2 = box (pos=iline2, size=sline2, color = color.blue)
mass1 = box (pos=imass1, size=vector(0.1, 0.1, 0.05), color = color.red)
mass2 = box (pos=imass2, size=vector(0.1, 0.1, 0.05), color = color.red)
m1 = 0.5
m2 = 0.5
def S1(s):
global m1
m1 = s.number
print ('mass 1 updated')
def S2(s):
global m2
m2 = s.number
print ('mass 2 updated')
winput(bind=S1, text='0.5')
scene.append_to_caption('Set the value for Mass 1 (on table in kg - hit enter when done)')
scene.append_to_caption('\n')
winput(bind=S2, text='0.5')
scene.append_to_caption('Set the value for Mass 2 (on table in kg - hit enter when done)')
scene.append_to_caption('\n')
scene.append_to_caption('The time is recorded in the window below - refresh page before rerunning')
print ('time = 0.0s')
scene.append_to_caption('\n')
def B(b):
dt = 0.002
acc = (m2) / (m1 + m2) * grav
mass1.a = vector(acc, 0, 0)
mass2.a = vector(0, -1.0*acc, 0)
mass1.v = vector(0,0,0)
mass2.v = vector(0,0,0)
hld1 = mass1.pos.x
hld2 = mass2.pos.y
mytime = 0
while mass2.pos.y > -0.3:
rate(100)
mass1.v = mass1.v + mass1.a * dt
mass2.v = mass2.v + mass2.a * dt
mass1.pos = mass1.pos + mass1.v * dt
mass2.pos = mass2.pos + mass2.v * dt
dx1 = mass1.pos.x - hld1
hld1 = mass1.pos.x
line1.pos = line1.pos + vector(0.7 * dx1,0, 0)
line1.size = line1.size + vector(-dx1,0, 0)
dx2 = mass2.pos.y - hld2
hld2 = mass2.pos.y
line2.pos = line2.pos + vector(0,0.5*dx2, 0)
line2.size = line2.size + vector(0,-1.0*dx2,0, 0)
mytime = mytime + dt
print ('Time = ', mytime, ' s')
button( bind=B, text='Click to Run Me' )
scene.append_to_caption(' ')




Leave a Reply