Question

In: Computer Science

Write C code to approximate Sin(x) and Cos(x) function using Taylor’s series. The angle x is...

Write C code to approximate Sin(x) and Cos(x) function using Taylor’s series. The angle x is expressed in radians. Use the first FIVE terms of Taylor’s series to approximate Sine/Cosine function.

a. Compare your Taylor’s series approximation of Sine/Cosine function with the math.h implementation of sin and cos function.

b. Determine tan(x) from Taylor’s series approximation of Sine/Cosine functions, for x = 0, PI/4, PI/2, PI. Use PI = 3.14159.

Solutions

Expert Solution

#include<stdio.h>
#include<math.h>
double fact(int n)
{
   if(n==0)
   return 1;
   else
   return (fact(n-1))*n;
}
int main()
{
   int f,i,s,c;
   float val=1,x=0;
   double t=0.0,sin=0.0,cos=1.0;
   float pi=3.14159;
   float l=0,m=(pi*pi)/720,n=(pi*pi)/360;

   printf("\n\tEnter the function\n");
   printf("1. sin \n");
   printf("2. cos \n");
   printf("3. tan \n");
   scanf("%d",&f);
  
   int angle;
   switch(f)
   {
       case 1:
       s=-1;
       printf("Enter the value of x : ");
   scanf("%f",&x);
   t=(x*3.14)/180;
       for(i=1;i<=5;i++)
       {
       s=s*-1;
       sin=sin+((pow(t,i)/fact(i))*s);
       }
       printf("\nsin(%f) = %.3f\n",x,sin);
       break;
       case 2:
       printf("Enter the value of x : ");
   scanf("%f",&x);
   t=(x*3.14)/180;
       s=1;
       for(i=2;i<=5;i+=2)
       {
       s=s*-1;
       cos=cos+((pow(t,i)/fact(i))*s);
       }
       printf("\ncos(%f) = %.3f\n",x,cos);
       break;
       case 3:
      
       printf("\nEnter any of these angles: 1- 0, 2- pi/4, 3- pi/2\n");
       scanf("%d",&angle);
       if(angle==1){
       s=-1;
       for(i=1;i<=5;i++)
       {
       s=s*-1;
       sin=sin+((pow(l,i)/fact(i))*s);
       }
       int s1=1;
       for(i=2;i<=5;i+=2)
       {
       s1=s1*-1;
       cos=cos+((pow(l,i)/fact(i))*s1);
       }
       float tan=sin/cos;
       printf("\ntan(%f) = %.3f\n",l,tan);
       }
       else if(angle==2){
       s=-1;
       for(i=1;i<=5;i++)
       {
       s=s*-1;
       sin=sin+((pow(m,i)/fact(i))*s);
       }
       int s1=1;
       for(i=2;i<=5;i+=2)
       {
       s1=s1*-1;
       cos=cos+((pow(m,i)/fact(i))*s1);
       }
       float tan=sin/cos;
       printf("\ntan(%f) = %.3f\n",m,tan);
       }   
  
       else if(angle==3){
       s=-1;
       for(i=1;i<=5;i++)
       {
       s=s*-1;
       sin=sin+((pow(n,i)/fact(i))*s);
       }
       int s1=1;
       for(i=2;i<=5;i+=2)
       {
       s1=s1*-1;
       cos=cos+((pow(n,i)/fact(i))*s1);
       }
       float tan=sin/cos;
       printf("\ntan(%f) = %.3f\n",m,tan);
       }
      

       break;
       default:printf("\n Invalid entry.\n");
   }
}


Related Solutions

Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
Write a code to approximate the derivative of a function f(x) using forward finite difference quotient...
Write a code to approximate the derivative of a function f(x) using forward finite difference quotient f( x + h ) - f( x ) f'(x) ≈ ------------------- (for small h). h For the function f(x) = sin(x), at x=1 , compute the FD quotients for h = 1/2k, k=5,...,N, with N=30 and compare with the exact derivative cos(x). Output k , h , error. Where SHOULD the error tend as h → 0 ? 1. Look at the numbers....
write the MATLAB code for: Let ? = sin2 (?) and ? = sin(?) cos(?). Let...
write the MATLAB code for: Let ? = sin2 (?) and ? = sin(?) cos(?). Let x vary from 0 to 10? in increments of 0.1?. Plot two figures part a & b. This panel has rows, one figure per row. a. Plot x versus y. This plot is on the top of panel and the next figure (part b) is at bottom of panel. i. Give a meaningful title to this figure. ii. x-axis is time with unit of...
For the given function determine the following: f (x) = (sin x + cos x) 2...
For the given function determine the following: f (x) = (sin x + cos x) 2 ; [−π,π] a) Find the intervals where f(x) is increasing, and decreasing b) Find the intervals where f(x) is concave up, and concave down c) Find the x-coordinate of all inflection points
a. (5 Marks) 1 1 cos(x)cos(y) = -cos(x-y) + -cos(x + y) 1 l sin(x)sin(y) =...
a. 1 1 cos(x)cos(y) = -cos(x-y) + -cos(x + y) 1 l sin(x)sin(y) = -cos(x-y)--cos(x+ y) 1 l sin(x)cos(y) =—sin(x-y) +-sin(x + y) A DSB-FC (double sideband-full carrier) signal s(t) is given by, s(t) = n cos(2rr/cf)+ cos(2«-/mt)cos(2«-fct) What is the numeric value for the AM index of modulation, m, fors(f) ?
Recall that the following Taylor series is used to approximate Cosine: cos(x) = ∑ (−1) nx...
Recall that the following Taylor series is used to approximate Cosine: cos(x) = ∑ (−1) nx 2n (2n)! ∞ n=0 You have been tasked with developing an m-file that allows other engineers to quickly determine the minimum n needed to reduce the truncation error below an error threshold. The truncation error is the absolute error between the approximation and MATLAB’s cos() function. You may want to Google the Taylor series to better your understanding of it. Your code should perform...
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program,...
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program, and use a while loop, to calculate cos(150) (the input is in degrees) by adding terms of the series and stopping when the absolute value of the term that was added last is smaller than 0.0001. Make sure to make the required degree <-> radian conversions. Use fprintf to print the cos(150) (up to 2 decimal places) and the number of terms used to...
f (x) = -0.248226*cos (2 x) - 0.0184829*cos ((2+2)x) - 0.0594608*cos(x)*sin(x) + 0.123626*sin ((2+2)x). The intervall...
f (x) = -0.248226*cos (2 x) - 0.0184829*cos ((2+2)x) - 0.0594608*cos(x)*sin(x) + 0.123626*sin ((2+2)x). The intervall is ]0, 3/2[ What is the local maximum and local minimum? Answer with 5 decimals
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT