All pastes #26804 Raw Edit

Something

public text v1 · immutable
#26804 ·published 2005-10-27 02:25 UTC
rendered paste body
    Scan the angle in degrees x_deg (=60). Express this angle in
    radians by using x=PI*x_deg/180, and calculate Y=cos(x) by
    using the math.h library of functions.

    Your objective in this quiz is to compare the so calculated
    value of Y=cos(x) with the approximate value y obtained by
    using 1, 2, 3, and 4 leading terms of the Taylor series:

        cos(x) = 1-x^2/2! + x^4/4! - x^6/6! + ....

    Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 .

    Use a do/while loop in conjunction with a switch statement
    switch(). Enter an integer 1 for one term approximation,
    2 for two terms, etc., by using scanf() function. Use
    0 to exit the program.

    Let your opening case calculate the fourth term -x^6/6!, let the
    subsequent case calculate the x^4/4! term, etc. (In this way,
    you wan't need to use a break statement within your switch).

    Evaluate the corresponding y , print the result in one line,
    and the relative error (y-Y)/Y in another line.

    Execute your program for all four cases (with corresponding
    printouts).




    Scan the angle in degrees x_deg (=60). Express this angle in
    radians by using x=PI*x_deg/180, and calculate Y=cos(x) by
    using the math.h library of functions.

    Your objective in this quiz is to compare the so calculated
    value of Y=cos(x) with the approximate value y obtained by
    using 1, 2, 3, and 4 leading terms of the Taylor series:

        cos(x) = 1-x^2/2! + x^4/4! - x^6/6! + ....

    Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 .

    Use a do/while loop in conjunction with a switch statement
    switch(). Enter an integer 1 for one term approximation,
    2 for two terms, etc., by using scanf() function. Use
    0 to exit the program.

    Let your opening case calculate the fourth term -x^6/6!, let the
    subsequent case calculate the x^4/4! term, etc. (In this way,
    you wan't need to use a break statement within your switch).

    Evaluate the corresponding y , print the result in one line,
    and the relative error (y-Y)/Y in another line.

    Execute your program for all four cases (with corresponding
    printouts).


............................................

Your output should look like this:

Enter x_deg: 60.
True value of cos(x) = 0.500000
Enter an integer 1-4; 0 to Exit
4
4 terms approximation
Approximate cos(x) = 0.499965
Relative error = -0.007 percent
Enter an integer 1-4; 0 to Exit
3
3 terms approximation
Approximate cos(x) = 0.501796
Relative error = 0.359 percent
Enter an integer 1-4; 0 to Exit
2
2 terms approximation
Approximate cos(x) = 0.451689
Relative error = -9.662 percent
Enter an integer 1-4; 0 to Exit
1
1 terms approximation
Approximate cos(x) = 1.000000
Relative error = 100.000 percent
Enter an integer 1-4; 0 to Exit
0