Question

Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to its Tavlor series expansion: (-1)r2n (2n)! x2 x4x6x8x10 cos(x)-Σ 1 This series produces the exact value of cos(x) for any real number x, but contains an infinite number of terms. Obviously, a computer program can compute only a finite number of terms. Thus, you will have to truncate the infinite series in (1). Your program should be able to do so in two different ways. Fixed number of terms: Implement the function cosוב ( double x, int N) that accepts as pa- rameters a real number x and an integer N (you can assume that N will be always positive). The function cos N should return the sum of the first N terms in (1), as a double. Fixed precision: Implement the function cos_delta (double x, double delta) that ac- cepts as parameters a real number x and another real number δ (you can assume that δ will be always positive). The function cos delta should return, as a double, the sum of the first NN terms in (1), where N is the smallest positive integer such that

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here's the program written as per the given specifications. Please do rate the answer if you found this helpful.

Code:

#include <stdio.h>

int factorial(int n)

{

if(n == 0) return 1;

else return n * factorial(n-1);

}

double power(double x, int n)

{

if(n == 0) return 1;

else if(n%2 == 0) return power(x, n/2) * power(x, n/2);

else return x * power(x, n/2) * power(x, n/2);

}

double cos_n(double x, int n)

{

double approx = 0;

// // for n times.

for(int i=0; i<n; i++)

{

approx += power(-1, i) * power(x, 2*i)/factorial(2*i);

}

return approx;

}

double cos_delta(double x, double delta)

{

double approx = 0, last_approx = 0, current_term = 0;

// variable for maintaing absolute difference between last_approx and approx.

double absDiff = 0;

int i=0;

do

{

last_approx = approx; // update last approx.

current_term = power(-1, i) * power(x, 2*i)/factorial(2*i); // calculate current term.

approx += current_term; // update current aprrox.

i++;

absDiff = (last_approx-approx > 0) ? last_approx-approx : approx-last_approx; // find the abs difference.

}while(absDiff > delta); // loop till difference is greater than delta.

return approx;

}

int main(void)

{

FILE *inFile = fopen("cos_input.dat", "r");

FILE *outFile = fopen("cos_output.dat", "w");

if(!inFile || !outFile)

{

printf("Error opening files!");

return 1;

}

int testcases = 0;

fscanf(inFile, "%d", &testcases);

int functionType = 1;

double x;

double delta;

int numTerms;

double cosApprox = 0;

for(int i=0;i <testcases; i++)

{

fscanf(inFile, "%d", &functionType);

if(functionType == 1)

{

fscanf(inFile, "%lf %d", &x, &numTerms);

cosApprox = cos_n(x, numTerms);

}

else

{

fscanf(inFile, "%lf %lf", &x, &delta);

cosApprox = cos_delta(x, delta);

}

// writing to file the output.

fprintf(outFile, "\nCase %d: cos(%.3lf) = %.12lf", (i+1), x, cosApprox);

}

return 0;

}

Sample Input file:

Files cos input.dat O saved 2 1-1.00 6 3 2 1 0.00001 4 1 1.5 2 main.C cos_input.dat 22θ.eb cos_output.dat 6 2 2 1.1

Sample output file:

Add a comment
Know the answer?
Add Answer to:
Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a.) Write a C++ program that calculates the value of the series sin x and cos...

    a.) Write a C++ program that calculates the value of the series sin x and cos x, sin 2x or cos 2x where the user enters the value of x (in degrees) and n the number of terms in the series. For example, if n= 5, the program should calculate the sum of 5 terms in the series for a given values of x. The program should use switch statements to determine the choice sin x AND cos x, sin...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos...

    Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos_series that takes that takes as its inputs, x and N and has output given by the sum in the N-term Maclaurin Series approximation for Cos(x). Hint: try a “for loop” and set “format long” in your code. You may use the MATLAB built-in function factorial() B. Check your code by finding the 2-terms, 3-terms, 4-terms, 5-terms and 6-terms Maclaurin Series approximations every 30 degrees...

  • The following function computes by summing the Taylor series expansion to n terms. Write a program...

    The following function computes by summing the Taylor series expansion to n terms. Write a program to print a table of using both this function and the exp() function from the math library, for x = 0 to 1 in steps of 0.1. The program should ask the user what value of n to use. (PLEASE WRITE IN PYTHON) def taylor(x, n): sum = 1 term = 1 for i in range(1, n): term = term * x / i...

  • The cosine function is analytically defined as follows: m 1. x² + x6 (-1)" x2m COS...

    The cosine function is analytically defined as follows: m 1. x² + x6 (-1)" x2m COS X = (-1)" x2n (2n)! 2!*4!- 6 + ... + (2m)! Write a complete C program that has three functions: main, cosine, and factorial. The cosine function receives a real number x and returns a real number representing the cosine of x. The cosine function also receives another integer m that determines the number of terms that will be used in computing the cosine...

  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

  • Write a program in Python that approximates the value of π by summing the terms of...

    Write a program in Python that approximates the value of π by summing the terms of this series: 4/1-4/3 + 4/5- 4/7 + 4/9- 4/11 + ... The program should prompt the user for n, the number of terms to sum, and then output the sum of the first n terms of this series. Have your program subtract the approximation from the value of math. pi to see how accurate it is.

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...

    Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above. firstPrime: is 2 secondPrime: the first prime number after 2 thirdPrime: the third prime number …. nth prime: the nth prime number Your program must be organized as follows: int main() { //prompt the user to enter n //read n from the...

  • d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes...

    d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes as parame- ters any three integers and returns the largest of the three integers. Your output should have the same format and should work for any integers a user enters Desired output: Enter three integers: 6 15 8 The largest integer is: 15 7.3 Recursive Functions Write a program that uses a function sum(int) that takes as an argument a positive integer n and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT