Question

In: Electrical Engineering

1) Write a program in C++ sin(x) can be approximately calculated using the following formula, where...

1) Write a program in C++

sin(x) can be approximately calculated using the following formula, where n! is factorial(n) – for example 3!=3*2*1 = 6 (the function in previous problem).

The more terms we use in the series, the higher will be accuracy of the calculations. By using infinite terms in the series we will have the exact value.

Hint 1: For n! simply use (copy and paste) the factorialFunc(n)from previous problem.

Hint 2: This problems is similar to the “pi series” problem in the homework problems. A simple for loop is needed. Much easier that it looks.

Write a program that gets x and calculates sin(x) using 10, 100, 1000 and 10,000 terms.

Then run the program and calculate and display the result for x=pi/6.

Solutions

Expert Solution

From the given problem statement, we can conclude that sin(x) is being computed using taylors series.

The C++ code is given below:

#include <iostream>

using namespace std;

double factorial(int x) //calculates the factorial
{
    double fact = 1;
    for(; x >= 1 ; x--)
    {
        fact = x * fact;
    }
    return fact;
}

double power(double x,double n) //calculates the power of x
{
    double output = 1;
    while(n>0)
    {
         output =( x*output);
         n--;
    }
    return output;
}

float sin(double angle,int numberofterms) //value of sine by Taylors series
{
   double a,b,c;
   double result = 0;
   for(int y=0 ; y!=numberofterms ; y++)
   {
      a= power(-1,y);           //sign of the term
      b= power(angle,(2*y)+1);       //numerator
      c= factorial((2*y)+1);       //denominator
      result = result+ (a*b)/c;       //1 - x^3/3! + x^5/5! - x^7/7!+...

   }
   return result;
}

double angle,output1,output2,output3,output4;

int main()
{
    cout<<"enter the value of angle in radians\t";
    cin>>angle;


    output1 = sin(angle,9);
    output2 = sin(angle,99);
    output3 = sin(angle,999);
    output4 = sin(angle,9999);

    cout<< "\nsine of the given angle for 10 iterations is\t"<< output1;
    cout<< "\nsine of the given angle for 100 iterations is\t"<< output2;
    cout<< "\nsine of the given angle for 1000 iterations is\t"<< output3;
    cout<< "\nsine of the given angle for 10000 iterations is\t"<< output4;
    cout<< "\n";
    return 0;
}

The snap shot of the code is provided below:

The output for x=pi/6 is shown below. Please note that the result converges for 10 iterations, so we get the same value for other other iterations too.


Related Solutions

PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance...
PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance = speed * time For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any program based on the title assigned. 2. The program must fulfill ALL the requirements below. The requirements listed below are the MINIMUM requirement. Your program may extend beyond the requirements if needed. a) Create at least one (1) base class. b) Create at least two (2) derived classes that inherit from the base class created in 2(a). c) Create at least one (1) object...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Write a python program to calculate the value of sin(?) using its Taylor series expansion: sin(?)...
Write a python program to calculate the value of sin(?) using its Taylor series expansion: sin(?) = ? − ? 3 3! + ? 5 5! − ? 7 7! + ? 9 9! … The program should consist of the following functions: a) Develop and test a calcSin() function that receives as an argument, of type float, the value of the variable and returns the result as a float also. Include 20 terms maximum from the series, or until...
Write a program (fortran 90) that calls a subroutine to approximate the derivative of y=sin(x)+2x^2 using...
Write a program (fortran 90) that calls a subroutine to approximate the derivative of y=sin(x)+2x^2 using a one-sided difference approach fx = (fi-fi-1)/deltaX and a centered difference approach fx = (fi+1-fi-1)/deltaX. The value of the function f and its derivative fx should be evaluated at x=3.75. Your code should print both values tot he screen when it runs.
Let F (x, y) = y sin x i – cos x j, where C is...
Let F (x, y) = y sin x i – cos x j, where C is the line segment from (π/2,0) to (π, 1). Then C F•dr is A 1 B 2 C 5/2 D 3 E 7/2
in c++ pleaseStatistics are often calculated with varying amounts of inputdata. Write a program...
in c++Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics.Ex: When the input is 15 20 0 5 -1, the output is:10 20You can assume that at least one non-negative integer is input.
sin(tan-1 x), where |x| < 1, is equal to: (a) x/√(1 – x2) (b) 1/√(1 – x2) (c) 1/√(1 + x2) (d) x/√(1 + x2)
sin(tan-1 x), where |x| < 1, is equal to:(a) x/√(1 – x²)(b) 1/√(1 – x²)(c) 1/√(1 + x²)(d) x/√(1 + x²)
According to Zeller's Formula, the day of the week can be calculated by: f=k+(13m−1)/5+D+D/4+C/4−2C where: •k...
According to Zeller's Formula, the day of the week can be calculated by: f=k+(13m−1)/5+D+D/4+C/4−2C where: •k is the day of the month. •m is the month number designated in a special way: March is 1, April is 2, . . . , December is10; January is 11, and February is 12. If x is the usual month number, i.e. for January x is 1, for February x is 2, and so on; then m can be computed with this formula:m=...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT