Physics 222 Assignment 2

Due January 29

Directions

Please turn in this assignment (and all future assignments for this course) by emailing them to me . Please use the subject of "Physics 222 HW2" for these assignments (if you use the link above, the subject will be added automatically). Please attach the any source code to programs that you write for this assignment. If you write more than one program, attach each separately. When answering the questions themselves feel free to answer directly in the body of your message (or attach your answers). I may take off points for assignments that are not sent to me in the correct form.

  1. Calculating e

    Write a function that uses the expression:

     e = lim (1 + 1/n)n  
        n → ∞ 
    to find approximations for the mathematical constant e. Your function should take as input a long double precision number, and return an approximate value for e as a long double precision number. Be careful about the types that you use for numbers in this program. Your main program should ask for the user to input a number that you will pass to your function for it to use as n in the formula above for calculating e. Your program should compare your approximation for e to the value that you get by calculating:e1. You can do so using the exp function. Note to use this function, you must have an
    #include <cmath>
    line near the top of your program. To use this function include a line something like:
    cout << exp(1.0L)  << '\n';
    This will give exp a long double version of 1.0, which will give you a fairly precise value for e. Also, calculate and print out the percent error in your calculation of e. Note that you also need to use the pow(x,y) function which calculates xy.

    Test your program with the following input: 1., 10., 100., 1000., 10.6, 10.9 and 10.12. Send your results in with your assignment. Comment on your results.

    Use the cout.precision(n) command to force cout to show n digits. Use this function before the places where you are going to print out results.

  2. Temperature in a freezer

    (Problem adapted from Essential C++ by Hanley.)

    Write a program that estimates the temperature in a freezer (in degrees Celsius) given the elapsed time in hours since a power failure. Your program should ask the user for input of a time since power loss as hours and minutes as integers and then convert the time in decimal hours.

    Assume that this temperature (T) is given by the formula:

    T= 4 t2/(t+2) - 20 .

    Your program should have a function that will take in a decimal time in hours and return the temperature of the in freezer at that time since power was lost. Take care about the types of numeric constants that you use in your calculations.

    1. Try your program with the following input: 2 minutes, 1 hour 10 minutes, 10 hours 5 minutes, and 98 hours 55 minutes. Send your results in with your assignment. Comment on your results.
    2. Your program should also create a plot of the temperature versus time. It should calculate the temperature at each hour from 0 to 5. Use the plplot library to create your plot.

    Using PLPlot

    Before trying to use plplot, take a look at the example of a simple program using plplot. You may want to download that program and compile it using the instructions in its header. Note that there are several lines from that program that you will need to copy. You will need the include line:
    #include <plstream.h> 
    You will also need to define all of the numeric variables that you use with the plot as types PLFLT and PLINT, in place of float and int. You will also need corresponding lines like the rest of the example. To compile your program, you should add:
     `pkg-config --cflags --libs plplotd-c++`
    to the end of your usual compiling command. PLPlot allows you to choose from many different forms of output. While developing your program you should probably choose <1> xwin or <3> gcw. For your final postscript plot output you should choose <11> ps or hit the save file button from the gcw window. Note that I had difficulty getting PLPlot to prompt for the output file type after calling cin, so you may want to have the plotting portion of your program before yoput use cin.
  3. Answer the following Review Questions from your textbook:

    3.7, 3.8, and 4.6.