Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C computer-programming language using one or more iteration structures to approximate the cosine function, given a radian value x, input using

Write a program in C computer-programming language using one or more iteration structures to approximate the cosine function, given a radian value x, input using scanf() During each iteration of the summation shown above, use printf() to output the value of the iteration index k, the kth approximation of cos(x), and the kth approximation error

errork = cosk (x)cosstdlib (x).

To use the standard library cosine function, you must include the math header file:

Recall the factorial of a nonnegative integer n is defined as n!=n*(n1)*(n2)* ... 10!=1 You can implement the factorial evaluation using a while() construct. The

calculates xy. The fabs() function calculates the absolute value of floating-point number,

Example output:

$ ./Lab03.exe ? 3.14 k: 0, cos(x): 1.000000, err: 1.999999e+00 k: 1, cos(x): -3.929800, err: 2.929802e+00 k: 2, cos(x): 0.120688, err: 1.120687e+00 k: 3, cos(x): -1.210518, err: 2.105196e-01 

function Modify your program so you store the error computed during the previous iteration and stop iterating when the previous error equals the current iteration error. In your comment block header explain how many iterations were invoked for x = , /2, /3, and /4, for a reasonableapproximate value of .

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

The Maclaurin series expansion for cos(x) is the infinite alternating series cos(x)=1-- + 2!' 4! 6! -o (2k)! Write a program using one or more iteration structures to approximate the cosine function, given a radian value x, input using scanf(). We will be discussing iteration in class on Thursday February 6, but in the meantime read sections 3.7 to 3.12 in the textbook. During each iteration of the summation shown above, use printf() to output the value of the iteration index k, the kth approximation of cos(x), and the kth approximation error error, = |cos, (x) - cos stdlib (x) where cosk(x) refers to the kth iteration approximation and CoSstdlib(x) is the value returned by the C standard library cosine function. To use the standard library cosine function, you must include the math header file: #include Recall the factorial of a nonnegative integer n is defined as n!=n(n-1).(n2). ... .1 0!=1 You can implement the factorial evaluation using a while () construct. The pow () function calculates x. double pow (double x, double y); The fabs () function calculates the absolute value of floating-point number, double fabs (double x); Example output: $ ./Labo3.exe ? 3.14 k: 0, cos (x): 1.000000, err: 1.999999e+00 k: 1, cos(x): -3.929800, err: 2.929802e+00 k: 2, cos (x): 0.120688, err: 1.120687e+00 k: 3, cos (x): -1.210518, err: 2.105196e-01 k: 4, cos(x): -0.976140, err: 2.385835e-02 5, cos (x): -1.001817, err: 1.818018e-03 k: 6, cos (x): -0.999899, err: 9.988098e-05 k: 7, cos (x): -1.000003, err: 4.009919e-06 k: 8, cos (x): -0.999998, err: 2.816154e-07 k: 9, cos (x): -0.999999, err: 1.624061e-07 k: 10, cos (x): -0.999999, err: 1.624061e-07 $ ./ Labo3.exe ? 3.14159265359 k: 0, cos (x): 1.000000, err: 2.000000e+00 k: 1, cos (x): -3.934803, err: 2.934803e+00 k: 2, cos (x): 0.123910, err: 1.123910e+00 k: 3, cos (x): -1.211353, err: 2.113529e-01 k: 4, cos (x): -0.976022, err: 2.397776e-02 k: 5, cos (x): -1.001829, err: 1.829147e-03 k: 6, cos (x): -0.999900, err: 1.004338e-04 7, cos(x): -1.000004, err: 4.172325e-06 8, cos (x): -1.000000, err: 1.192093e-07 k: 9, cos(x): -1.000000, err: 3.774758e-15 k: 10, cos(x): -1.000000, err: 3.774758e-15 Modify your program so you store the error computed during the previous iteration and stop iterating when the previous error equals the current iteration error. In your comment block header explain how many iterations were invoked for x = 1, 1/2, T/3, and tr/4, for a reasonable approximate value of IT

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions