Projectile motion program in c
First we need to design the interface. The following image shows an example of what the form might look like when the program runs. Use a sub-procedure to input the initial height and initial velocity. Be sure to validate your input and make sure that negative values produce an error message.
Use a function to compute the maximum height of the ball. Use a sub-procedure to determine the height of the ball at every quarter second and display the results in the list box.
The four options to be provided by buttons are as follows: a Determine the maximum height of the ball. Hints: Use a sub-procedure to input the initial height and initial velocity.
The member variable n defines number of vector components and x is an array of real numbers. Each item of x holds one vector component.
The first constructor simply sets the number of vector components and creates the array x. The second constructor is a copy constructor; it not only sets the number of components and creates x array, but also copies array values from the vector a. To get a quick approach to individual vector components, I define an indexer:. A subtraction can be done in the same manner. Now the multiplication of vector a by a real number c from the left:. The multiplication from the right side should also be added.
Having readied our PointN class, we can now try to code an integration procedure. First, we should decide which shape the function calculating the second derivative should have. An interface is an ideal mean to do this:. The interface is called SecondDerivativeN , which stands for "second derivative in n dimensions.
The method takes time t , a vector of positions y and a vector of velocities dy and returns a vector of second derivatives. The integration is coded in NystromN class:. The constructor takes an instance of the SecondDerivativeN interface as its first argument.
Other arguments are: initial time t0 , initial vectors of positions and velocities y0 and dy0 , time interval h and finally, an initial vector of second derivatives d2y0.
This last one is a bit questionable. Why should we provide a value of the second derivative when the method calculating it was passed as the first argument?
Well, this is my favorite way of organizing the integration process. The example of NystromN usage will illustrate this approach. As an example, we use the problem mentioned in the Backgroud section. Let the air density be 1. The FlyingBall2 class takes all necessary parameters from its constructor and stores them in its member variables. The SecondDerivateiveN.
GetValue method implements the SecondDerivativeN interface. It calculates the two-dimensional vector of acceleration using the time t , the x and y coordinates in the y vector, and the v x and v y components of the velocity vector in v.
The method determines the total velocity vTotal first. Then it gets two parameters that define the direction of velocity, cosAplha and cosBeta. Drag magnitude is stored in the drag variable and, with the help of ratios cosAplha and cosBeta , the drag components dragX and dragY are obtained.
At this moment, we have all forces taking action on the projectile. Summing them with respect to their directions, we get the vector of the resulting force. The last step is to divide this vector by the projectile mass and we finally have the acceleration vector. By definition, acceleration is the second derivative of position, so the rest is now up to the integration routine NystromN.
Note that the definition of main does not expect any arguments, but you're passing two arguments in the call from TOF. If you want to keep TOF typed void and have it update t in the argument list, then you must pass a pointer to t :. Also, remember that sin and cos expect their arguments to be expressed in radians , not degrees. Finally, unless you have a really good reason not to, use double instead of float.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 4 years ago. Active 4 years ago. Viewed 2k times. Improve this question. Someone like you Someone like you 1 2 2 bronze badges. You forgot to return the value. I'm baffled by why TOF calls main. What is this line supposee to achieve? Aside from others things: Never call main manually from a function inside your program.
0コメント