CSCI 1320 - Assignment #3



This assignment will be your first where you write a significant amount of code to solve some small problem. With what we have talked about so far, your ability to solve problems is still fairly limited, but it is growing. Last assignment you used just the I/O functions (and could do it with just the O part). This time we will add conditional execution as well as your new found skills with loops to help you solve a simple problem on a computer. I'm giving you three options to choose from depending on your particular preferences. You should choose one and make sure that the comments in the code you send to me make it very clear which one you have chosen. Even if you don't htink you like math or physics you should read the description of those problems just to see if they interest you. To turn in this assignment just send the code in an e-mail to mlewis@cs.trinity.edu. You can do this with the command "mail mlewis@cs.trinity.edu < assn3.c" if assn3.c is the name of your source file.

Standard Problem:

If you choose this option, you will write a program to calculate grade for a student taking this class. The one difference is that for now you don't have to drop the lowest grade. So your program should read in the 10 assignment grades, the 6 quiz grades, 2 test grades, and the class participation grade. It should print out the averages for each of those as well as the total average in the class given the formula on the syllabus. After you have their number average you should print the letter equivalent for it (just A, B, C, D, F will work, no +/- required.) This is a problem where being able to redirect input will probably be very helpful. (For extra credit make it drop the lowest quiz grade and allow for multiple students to be processed in a row.)

Math Type:

One of the useful things that you learn in calculus is that functions can be approximated. Your calculus text will mention both the Maclaurin series approximation and the Taylor series approximation. They are basically the same other than Maclaurin series are always taken about x=0 and this is what we will be working with here. The definition of the Maclaurin series is f(x)~sum_i(x^i*(d/dx)^i(f(0))/i!). So this is the sum from i=0 up to some n (or infinity if you want to be really accurate). In the sum we have x raised to the i power (not an xor here) times the ith derivative of f(x) evalutated at 0 divided by i factorial. Obviously, this is a real pain to use on functions where taking the derivative isn't easy. However, for some functions where the derivatives are straightforward, performing this approximation is very easy. Examples of that would be e^x, sin(x), and cos(x). I want you to write a program that does a Maclaurin approximation of cos(x). That's not really that hard because the derivative is -sin(x), which has a derivative of -cos(x) which goes to sin(x) then back to cos(x). Also note that you are always evaluating at x=0 so all the terms for sin go to zero.

The first few terms in this series are:

1-0-x^2/2!+0+x^4/4!-0-x^6/6!+0+x^8/8!+...

For the assignment, I want you to ask the user for the x to use, as well as an accuracy. Use the math library function cos to get the real value of cosine at that value of x. Then iterate until the difference between the series value and what that function gives you is less than the input accuracy. In the loop, print out the value of the series once every hundred iterations. After the loop print out the real value, the value you got from the series, and how many terms you had to sum in it. (For extra credit, make your program use a Taylor series instead. This means inputing another value x0 which the series is expanded around.)

Physics Type:

Computers are used extensively for simulating physical systems, especially when the system is hard/impossible to build in a lab. For this assignment I'm interested in having you write a very simple simulation of the gravitational Kepler problem. You will also explore the accuracy of what you are doing a little bit. Imagine you have a body in orbit around a star. We'll assume that the star is much larger than the other body so it stays at the origin, (0,0), of our coordinate system. The other body starts at some position (x,y) with a velocity (vx,vy). A simple "integrator" for a system like this can be constructed by discretizing Newton's laws (a fancy way fo saying that we avoid calculus and do things in a way that is more computer friendly). Newton's second law tells us F1=m1*a1 and fro gravity F=-G*m1*m2/(r*r). We're going to simplify this a fair bit for our toy system and just say that a=-1/(r*r). We can break this into components and get ax=-x/(r*r*r) and ay=-y/(r*r*r). Now, the trick on the computer is to say that instead of moving smoothly, the particle jumps over certain timesteps, dt. So after one timestep the new position is x=x+dt*vx and y=y+dt*vy. Similarly, vx=vx+dt*ax and vy=vy+dt*ay. Doing this in a loop "integrates" the motion of the body. (Use the sqrt function in math.h to calculate r.)

This integrator is very simple, but far from perfect. If you start with your body at (1,0) with a velocity of (0,1) it should be in a nice circular orbit staying that distance forever. By measuring how that distance changes, you can get a measure of how accurate, or inaccurate, the integrator is. You can play with other positions and velocities to see what happens. as well.

If you choose this part, you will write a program that takes the initial x, y, vx, and vy as well a a timestep, dt, as inputs.It should then advance the system for a total time of 10.0 (so if dt=0.1 that requires 100 iterations). At the end of it you should measure the distance from the origin and print a message giving that and the original distance. Then check to see if the change is less than 1%. If it is, say that the integration was accurate enough, otherwise say it isn't. In a comment in your code you should tell me how small you had to make your timestep for this to be reached given the coordinate 1 0 0 1. (I should note that this measure of accuracy is only good for circular orbits. We aren't going to do enough physics to go beyond that, but if you happen to want to, the real objective is to conserve total energy. You can get extra credit by comparing inital and final total energies of the system.)