Click here to Skip to main content
13,804,094 members
Rate this:
Please Sign up or sign in to vote.
See more:
scene.width = 600 #Set the size of our viewing portal
scene.height = 600

# Create the plot
PosGraph = graph(width=600, height=250, title='Canister Floating on Water: Experiment and Simulation',
ytitle='position (m)', xtitle='time (sec)')

#expt = series(graph = PosGraph, color=color.cyan)
sim = series(graph = PosGraph,

# Load the data. It expects data in rows, where each set of data is comma separated.
# The time should be in the first column, and position in the second.
# It ignores any other columns, although you could have yours read in velocity or whatever.
#It must have data in the first row. Remove any headers or column names.

#f = read_local_file()

# Create an empty list or array to store the data
t_data = []
x_data = []

# Now go row by row through the file, and add the data from the first column (number 0) to time array
# and the second column (number 1) to position array
#for row in f.text.splitlines():
#cols = row.split(",")
# t_data.append(float(cols[0]))
# x_data.append(float(cols[1]))

# put it on the graph, too. X = time, Y = position
# expt.plot(float(cols[0]), float(cols[1]))

## Now simulate those results. Below here is what you'll need to change for your experiment

g = -9.8
deltat = .05 #the time interval for each loop in seconds; choose any small value
mass = 0.022 # This was measured
#My oscillator was a mass on a horizontal spring. Your oscillator may depend on different factors

D = 1000 # This was measured. You could also use it as a fitting parameter that you vary (kg)

p = D #Rho
f_friction = 1.1 # This is probably hard to measure, so I'll use it as a fitting parameter to vary
# starting with 0 is a good way to see that the oscillation in your simulaiton works
y_init = -0.028 # look at your first data point
y_equil = -0.017 # the point around which you oscillate. A good choice to start with comes from the data
t = 0 #initialize time. Choose the first time point of your experimental data so the graphs can match
v_i = vec(0,1,0) #initialize speed. Look at your first position data points to estimate the initial speed

#First, my block
container = cylinder(pos=vec(0,y_init,0), size = vector(.03, .049, .03), v = v_i)
#I could add the spring, but it would only be for show

while t < 6.8: #tell it how long to run the loop, in seconds (how long does your experimental data go?)
rate(40) #tell it how quickly to run the loop (bigger numbers are faster)
H= container.pos
V= Pi*r^2*H

#Calculate the forces on your oscillator. Mine are spring and friction. Yours may be different
# Fsp = -spring_k * (block.pos - vector(x_equil, 0,0)) # - k *delata_x which is block position minus equilibrium position
Fg = g*mass # opposite direction to velocity
Fb = p*V*g
#Get the net force vector
Fnet = Fb + Fg + Ffric

#Find the acceleration of the craft. Newton's Second Law!
a = Fnet/mass

#allow the acceleration to change the velocity of the craft for a short time interval 'deltat'
# vf = vi + a*t
v = v + a*deltat

#allow the velocity to change the position of the craft for a short time interval 'deltat'
# xf = xi + v*t
container.pos = container.pos + container.v*deltat

#plot the matching data to your experiment

#advance time by the interval deltat
t = t + deltat

#go back to the top of the loop and re-calculate everything at the new position; repeat until the 'while' statement is satisfied

What I have tried:

I have tried to give each variable a number
Updated 8-Dec-18 0:23am
OriginalGriff 8-Dec-18 1:37am
Well, it's not C++. Matlab at a guess.
Edit your question, and correct the tags to match whatever the heck that is, and then tell us where and when the error occurs - just dumping code on us and saying "it don't work" tells us nothing much at all.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

v_i = vec(0,1,0) #initialize speed. Look at your first position data points to estimate the initial speed


v=v_i   # so v is defined as a vector


v = v + a*deltat # you cannot add a single value to a vector

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181218.1 | Last Updated 8 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100