%% Gravitational Acceleration
% In this lab you will design your own experiment to measure gravitational
% acceleration using a pendulum.
%
% * [ <GAcc.pdf GAcc.pdf> ] pdf version. 
% * [ <GAcc.php GAcc.html> ] html version. 
% 

%% Materials and Equipment
% For this lab you will need:
%
% # A pendulum. As part of the lab, you will design and obtain the
% parts for a pendulum to measure gravitational acceleration.
% # Electronic photo-gate from <https://gibbs.ccny.cuny.edu/teaching/s2021/labs/VibratingCantilever/SPVib.php#3
% Vibrating Cantilever Laboratory.>
% # Computer with free USB port for arduino.
% # MATLAB. (Technically not required, but would be more difficult without.)

%% Part I Design
% This lab will be different from the others. Part of the lab will be designing
% a pendulum from household items to measure gravitational accelerations.  From
% the theory below you will develop design criterion based on error analysis and
% corrections from non-ideal behavior. Here is an example from the design of the
% Vibrating Cantilever Laboratory.  A slender cantilever vibrates with a
% fundamental frequency given by:
%
% $$
% f_0=\frac{1}{2\pi}\left(\frac{\beta_0}{L}\right)^2\sqrt{\frac{EI}{\rho}}
% $$
%
% where $E$ is the elastic modulus, $\rho$ is the density, $L$ is the length,
%
% $$I=\frac{\pi D^4}{64}$$ 
%
% is the area moment of inertia for the beam, $D$ is the diameter, and $\beta_0$
% is a constant. This can be rearranged:
%
% $$
% K=\frac{\beta_0}{2\pi}\sqrt{\frac{EI}{\rho}}=f_0L^2.
% $$
%
% Then the fractional error in K, 
%
% $$
% \frac{\delta K}{K}=\sqrt{\left(\frac{\delta f_0}{f_0}\right)^2+
%                        \left(\frac{2\delta L}{L}\right)^2}.
% $$
% 
% Determining the exact fractional error in a frequency measurement using the
% FFT is difficult, but it is almost always as good as 2/N, where N is the
% number of points in the time series. For the photogate we have N=768 so
% $\delta f/f< 2.6\times 10^{-3}$.  So the error will be dominated by $\delta
% L/L$ until $\delta L/L<2.6/2\times 10^{-3}=1.3\times 10^{-3}$.  If we
% have a ruler with and uncertainty of +/- 0.5 mm then we need
% $L>=0.5mm/1.3\times 10^{-3}=384mm$ or about 15 inches. From numeric simulation
% we can estimating the fractional error in frequency for a 50 count amplitude
% signal with negligible clock error and 768 samples to be $\delta f/f=1\times
% 10^{-6}$. Thus the design goal from this analysis is that we should make the
% cantilever as long as possible.
%
% However there are other considerations.  For example, the equation is for a
% slender rod. So the diameter must be small with respect to length. So we want
% D/L as small as possible.  But if D/L is too small then the rod will bend
% under its own weight. The deflection of a cylindrical cantilever under its own
% weight is given by: 
%
% $$ \frac{\delta x}{D}=\left(\frac{2\rho g D}{E}\right)
%                       \left(\frac{L}{D}\right)^4
% $$
% 
% The deflection must also be small $\delta x/D \approx 1$ for the equation to
% hold so L/D cannot be too large.  The design criterion from these two
% equations and the slender assumption is that we want L/D to be as large as
% possible and 
%
% $$ L/D \approx \left(\frac{E}{2\rho g D}\right)^{\frac{1}{4}}. $$
% 
% Then the final criterion can be stated as we want 
%
% $$\frac{E}{2\rho g D}$$
%
% as large as possible.  There are other criterion like the density should be
% uniform, the cross-sectional area is constant, the material can not deform
% (like copper wire). However, this should give you and idea of how to think
% about the criterion for the pendulum you need from the theory section below.
% Since you will be using the same frequency measurement system you can assume
% that the fractional error in the frequency is $10^{-6}$. Make a list of your
% criterion to include in the report and to use in part II.

%% Theory of Pendulum
%
% [0] <https://en.wikipedia.org/wiki/Pendulum#Simple_gravity_pendulum _*Simple
% Pendulum:*_> The simple pendulum consists of a _massless_[1] _infinitely stiff
% rod_[5] of length L and a _point mass_[1] m at the end in a uniform
% gravitational field with acceleration g. The rod is fixed at one end with a
% _frictionless pivot_[4] and the mass is at the other end and both are confined
% to move in a _2D plane_[3] in a _vaccuum_[4]. The major approximations are in
% _italics_ and numbered[#]. The frequency for _small amplitude_[2] osculations
% of a simple pendulum is:
%
% $$
% f=\frac{1}{2\pi}\sqrt{\frac{g}{L}}.
% $$
%
% [1] <https://en.wikipedia.org/wiki/Pendulum#Compound_pendulum _*Physical
% Pendulum:*_> The physical pendulum relaxes the constraints [1] on the mass
% distribution. The main difference is that the equivalent length is given by
% $L_{eq}=I/mR$, where I is the <https://en.wikipedia.org/wiki/Moment_of_inertia
% moment of inertial> about the pivot and R is the distance from the pivot to
% the center of mass.  So for a point mass and massless rod $R=L$, $I=mR^2$ and
% $L_{eq}=mL/mL^2=L$.  This type of pendulum is also called compound which
% emphasizes that the <https://en.wikipedia.org/wiki/Parallel_axis_theorem
% parallel axis theorem> can be used to build up the moment of inertial from the
% parts.  A common example is the massive rod with a spherical mass at the end.
% You can use the parallel axis theorem to find I and the center of mass. Then
% the frequecy for _small amplitude_[2] oscillations of a physical pendlum is:
% 
% $$
% f=\frac{1}{2\pi}\sqrt{\frac{g}{L_{eq}}}=
%   \frac{1}{2\pi}\sqrt{\frac{mgR}{I}}. 
% $$
%
% [2] <https://en.wikipedia.org/wiki/Pendulum#Compound_pendulum _*Large
% Oscillation Pendulum:*_> If we relax the constraint of small oscillations then
% the frequency is effected. To get the full solution we need to use elliptic
% integrals.  There are also approximations that are quite accurate.  See
% Richard B. Kidd and Stuart L. Fogg , _A simple formula for the large-angle
% pendulum period_ , The Physics Teacher 40 , 81-83 (2002) [
% <https://doi.org/10.1119/1.1457310 doi> ] [ <Pendulo2.pdf pdf> ]. Note that
% the following approximate formulas will work for a large initial angle
% $\theta_0$ in a physical (or simple) pendulum:
%
% $$
% f\approx\frac{1}{2\pi}\sqrt{\frac{g\cos(\theta_0/2)}{L_{eq}}}\approx
%         \frac{1}{2\pi}\sqrt{\frac{g}{L_{eq}}}
%          \left(1+\frac{\theta_0^2}{16}
%                 +\frac{11\theta_0^4}{3072}
%                 +\frac{173\theta_0^6}{737280}\right)^{-1}
% $$
%
% [3] <https://en.wikipedia.org/wiki/Spherical_pendulum _*Spherical Pendulum:*_>
% If we relax the confinement of the pendulum to a 2D plane for example in a
% pendulum on a fixed length string then there is another angle $\phi$ for the
% pendulum to swing in.  The period of the motion in $\phi$ is determined from
% angular momentum conservation (see.
% <https://en.wikipedia.org/wiki/Conical_pendulum Conical Pendulum> for the
% extreme cass of stationary $\theta$. The previous formulas all work for this
% case as long as the angular momentum is zero.  In practice, due to damping the
% angular moment will tend to zero. So if you start with a small rotation then
% it will damp away leaving pure in plane motion.
%
% [4] _*Frictional Pendulum:*_ Friction is present in all real pendulums.  There
% are two main sources: frictional torque at the pivot and air resistance from
% the cross-sectional area of the pendulum in the direction of motion.  A nice
% analysis of air drag is found here: Mohazzabi, P. and Shankar, S. (2017)
% _Damping of a Simple Pendulum Due to Drag on Its String_. Journal of Applied
% Mathematics and Physics, 5, 122-130 [ <https://doi.org/10.4236/jamp.2017.51013
% doi> ] [ <https://www.scirp.org/journal/paperinformation.aspx?paperid=73856
% link> ] [ <JAMP_2017012515591136.pdf pdf> ].  The main point is that you need
% to reduce friction sufficiently so that your pendulum will swing for many
% oscillations. Ideally it would swing 20-30 times without much loss as our
% previous assumption that the fractional frequency error is $10^{-6}$ is based
% on 25 consistent oscillations.  Generally friction lengthens the period and
% therefore decreases the frequency.
%
% [5] <https://en.wikipedia.org/wiki/Elastic_pendulum _*Elastic Pendulum:*_> If
% the rod or string of a pendulum is elastic like a spring or rubber band then
% the motion can be very complicated and chaotic.  If we had done a 6th lab we
% would have explored the motion of a weight on a rubber band being driven to
% choas.  To measure gravity we need the pendulum to resist stretching.  So a
% rubber band is not appropriate. However, for small oscillations the effect of
% stretching is usually negligible. For small oscillations the equations are
% linear so to a good approximation we can just add the solutions to each
% problem.  The frequency of a mass on a spring with spring constant K is
% $f_s=1/2\pi\sqrt{K/m}$. As long as $f_s>>f_p$ then you should be ok.
%

%% Part II Experiment
%
% From the theory above in Part I you have made a list of criterion.  For the
% Cantilever experiment one of the main criteria was that we wanted the largest
%
% $$Q=\frac{E}{2\rho g D}$$ 
%
% we could find.  For part II look around your house or work and try to find the
% best materials that will match your criterion.  If needed you could buy a few
% things, but do not spend more than $10.  For the cantilever experiment, I
% found that the Q for spaghetti was larger than most of the common materials in
% my house that came in a cylindrical form. Two other item had higher Q, bamboo
% skewers and steel skewers for shish kabobs. However the bamboo skewers have a
% lot of damping; they would only produce 3-4 oscillations. The steel skewers
% actually performed better than spaghetti, but the frequencies are very high,
% which is good for errors but not as visually clear what is happening. They are
% very sharp and difficult to cut.  So for the measurement of density using
% water displacement they were not useable. They are also much more expensive
% and do not come in different diameters.  Another interesting thing that I
% found was that whole wheat spaghetti has much more damping than regular.  This
% may be because it is not as homogeneous.
% 
% 1) Make a list (to include in the report) of several possible pendulum designs
% and evaluate them base on your empiric criterion from part I and on any other
% criterion you find useful to choose a specific design.  Simple is always a
% good design criterion. It is better to start simple and add complexity as you
% go. As an example my entire pendulum consists of only 4 parts. Make a written
% description of your design before you build it for inclusion in the report.
% If you want some feedback you could send the description to me (as long as it
% is not the last day).
%
% 2) Build and test your design. A good design will oscillate for 100s of
% periods before stopping.  It will have something thin a the very bottom to
% easily pass through the photogate. You may need to adjust your design as you
% go.  This is part of the experimental process.  Write a description of your
% final design and explain any major changes from you draft design from
% earlier.
%
% 3) Measure $L_{eq}$: Measure the equivalent length $L_{eq}$ for your pendulum.
% Take a picture of you pendulum to include in the report and describe how you
% determined $L_{eq}$ using the picture or diagrams as needed. Record and report
% the value and uncertainty in the standard form. From $L_{eq}$ and the
% frequency derive and expression for gravitational acceleration g including any
% corrections as need from the theory section. Record and report how the
% fractional uncertainty in g depends on the fractional uncertainties in f and
% L.
%
% 4) Measure the frequency of the oscillation by letting the pendulum swing
% through the photogate from the
% <https://gibbs.ccny.cuny.edu/teaching/s2021/labs/VibratingCantilever/SPVib.php#3
% Vibrating Cantilever Laboratory.>  The technique is very similar.  The main
% difference is that the oscillation frequency will be lower and will last
% significantly longer.  To account for these changes you will not really need
% to make 10 measurements to find the best one.  Start the pendulum swinging and
% then use <PhotoGateOscilloscope.m PhotoGateOscilloscope.m> to measure the
% light in the photogate.  This is a newer version of PhotoGateOscilloscope.m
% which allows much longer acquisition times needed for this experiment. *Note:*
% delete any older versions before saving. The ino file for the arduino
% <PhotoGateOscilloscope/PhotoGateOscilloscope.ino PhotoGateOscilloscope.ino> is
% the same.  A typical command would look like this:
%
%  [V,T,N]=PhotoGateOscilloscope('com5',1,80000);
%
% Notice that the second argument is 1. That means only one acquisition of 768
% points will be taken. The time between each of the 768 is $80000\mu s$ from
% the third argument.  This total acquisition will take 768*.08s = 61s or about
% a minute.  You will need to adjust the timing to get at least 25 oscillations
% in one acquisition. You can estimate the period of the oscillation from the
% known value of gravity.  Remember that if the oscillations are larger than the
% size of the photoresistor there will be an apparent frequency doubling as the
% pendulum swings through from right to left and then again from left to right.
% If the amplitude is small enough then the pendulum will stay inside the
% photogate the whole time and the frequency will not be doubled.  More
% typically there will be some power in both frequencies f and 2f.  To measure
% the frequency you can use <intFFT.m intFFT.m> from the cantilever lab.  A
% typical command would look like this:
%
%  [FV,f,f0]=intFFT(V,T,1000);
%  plot(f,FV);
%
% From the plot you can zoom in to measure the peak frequency.  With an
% interpolation of 1000 as above you will get 4 or 5 significant figures. If you
% need more you can increase to 10000 but it will take significantly longer and
% you will have to find the peak by searching through the values of FV.  If you
% find that the peak is at index ii, then f(ii) is the corresponding frequency.
% The follow commands can automate the process if the peak you want is the
% largest:
% 
%  [~,ii]=max(FV(1:N/2-1)); % find the location of the max of FV in the 1st half
%  f(ii)                    % f(ii) is the corresponding freq (ignore negative)
%
% This method is not fool proof the frequency you want may not be the largest.
% If not you could use max on a different section of FV.
% 
% 5) From the frequency and $L_{eq}$ determine g.  If it is within 10% you have
% a working gravity meter.  If the error is greater than 1% see if there are any
% small modifications that you could do to improve.  Once you are satisfied make
% 10 measurement of the frequency at the smallest amplitude where you can still
% get 25 good oscillations.  Record and report the mean and std of the 10
% measurements then calculate your best value of g in standard form with error
% estimate. Also record and report the deviation from the accepted value of
% 9.802 m/s/s (NYC).  Is the deviation consistent with the error estimate?
%

%% Part III Proposal
%
% Write a 1 paragraph proposal on how you could make the experiment more
% accurate if you could spend up to 100 dollars.  How would you get the most
% accuracy with the fewest dollars.  Include an estimate of the improved
% accuracy.  You can assume that the frequency measurement has a fractional
% accuracy of $10^{-8}$ which is relatively easy to get by collecting longer
% data sets.  Since we are not measuring at full speed we can take very long
% data sets if needed.

%% Files
% * <GAcc.zip>  All files in one zip file.
% * <GAcc.m>    (This File).
% * <GAcc.pdf>  (pdf version).
% * <PhotoGateOscilloscope.m> Photo-gate acquisition function.
% * <intFFT.m> Interpolated FFT.
% 