Question

In: Computer Science

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 the value of a term is less than 1e-8.

b) Write a main program to invoke the function and calculate and print the values of sin ( ? 4 ), sin (− ? 2 ), and cos2 ( ? 3 ).

Solutions

Expert Solution

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

import numpy as np
def fact(n):
if(n<=1):
return 1;
return n*fact(n-1);
def calcSin(x):
s=0;
for i in range(20):
t=(-1)**i*(x**(2*i+1))/fact(2*i+1);
s=s+t;
if(abs(t)<1e-8):
break;
return s;
def main():
print("sin(pi/4)=",calcSin(np.pi/4.0));
print("sin(-pi/2)=",calcSin(-np.pi/2.0));
print("cos(pi/3)^2=",1-calcSin(np.pi/3.0)*calcSin(np.pi/3.0));
main();

Kindly revert for any queries

Thanks.


Related Solutions

Find Taylor series expansion for sin^2(z)
Find Taylor series expansion for sin^2(z)
Write the first three non-zero terms of the Taylor series of sin x near x0 =...
Write the first three non-zero terms of the Taylor series of sin x near x0 = 3π/2. Sketch a plot of the curves for sin x and your approximation.
Using steps.py in this repository for a starter file, write a Python program to calculate average...
Using steps.py in this repository for a starter file, write a Python program to calculate average number of steps per month for a year. The input file (which you will read from the command line, see the sample program on how to read command line arguments in this repository) contains the number of steps (integer) a person took each day for 1 year, starting January 1. Each line of input contains a single number. Assume this is NOT a leap...
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
Find the zeroes and their order for the following. (Use Taylor series or derivatives) sin(z)
Find the zeroes and their order for the following. (Use Taylor series or derivatives) sin(z)
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A*...
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is a puzzle invented and popularized by Noyes Palmer Chapman in the 1870s. It is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order, using as few moves as possible. You are permitted to slide blocks...
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...
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT