Connecting Tech Pros Worldwide Forums | Help | Site Map

Programming an equation

Newbie
 
Join Date: Sep 2009
Posts: 1
#1: Sep 22 '09
At time t, the velocity v of an object undergoing simple harmonic motion at the end of a spring is given by the formula


v = A * √ k / m * cos ( √ k / m * t )

Here, m is the mass of the object (in g), k is a constant depending on the spring (in g / s2), A is the maximum distance the object moves, and t is the time (in s).
Write a C program which accepts as inputs a time t, mass m, constant k, and distance A, and which computes the velocity of the object according the formula above.
When running your program, use the following inputs:
t: 0.10 s
m: 36.0 g
k: 400 g / s2
A: 5.0 cm

Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,213
#2: Sep 22 '09

re: Programming an equation


The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.

Banfa
Administrator
Familiar Sight
 
Join Date: Jan 2007
Posts: 193
#3: Sep 30 '09

re: Programming an equation


This appears to be a very simple assignment even though it may take some debugging because of units. Here are the bare bones.
Since k is a spring constant you should declare it as a const.
Likewise for pi.
Since sqrt(k/m) appears twice in the formula assign a variable to it (say z).
Now you can simplify the solution by:
Expand|Select|Wrap|Line Numbers
  1. cin>>t>>m>>A; //get the 3 variables, (k already declared).
  2.   v=A*z;
  3.  v*=cos(z*t);//calculate v (rememeber cos (arguement) in radians (deg*pi/180)
  4. cout<<v;
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#4: Sep 30 '09

re: Programming an equation


Following up on whodgson's comment that the cos() function in C requires an argument in radians ...

Your formula requires you to compute the cosine of squareRoot(k/m)*t. This cosine argument is dimensionless. Your problem statement provides no clue whether the computed value of this argument is in degrees or radians.

I think it is ok for you to back to your teacher and ask if the actual dimensions for k are g-(deg/sec)^2 or g-(rad/sec)^2.
Familiar Sight
 
Join Date: Jan 2007
Posts: 193
#5: Oct 1 '09

re: Programming an equation


Ummm.
In this sort of motion (SHM) angular velocity is sort of synonymous with angular frequency ie w^2=k/m or w=sqrt(k/m)
The units of k are N/m or kg/sec^2 so the units of k/m are kg/s^2/kg=s^-2
When the sqrt of this is taken the unit of frequency is in rad/s.
(recall f=w/2*pi = 1/2*pi * sqrt(k/m)
But you are right in a sense because radian is not really a propper unit.
Quite confusing really.
Reply