Problem Statement Introduction: For this machine problem, you will be writing a
ID: 3623292 • Letter: P
Question
Problem StatementIntroduction: For this machine problem, you will be writing a simple C program to approximate the function sin(x). The user of your program should be prompted to enter a number x, and your program should approximate and print out the value of sin(x).
Algorithm: We will use the Taylor series approximation for the value of sin(x):
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + … x^n/n!
where n is the degree of the Taylor polynomial and x is an angle in radians. The higher the degree, the closer the approximation is to the actual value of sin(x). Note that the error of the approximation is no larger than
|x|^(n+2)/(n + 2)!
This fact is important for Checkpoint 2.
Checkpoint 2
For the second checkpoint, you will modify and extend your code for checkpoint 1. You will need to modify the code to read in a second number, r, from the user. This should be read immediately after x is read. This second number represents the desired maximum error. You then should modify your code to use a loop to calculate the approximation. You will need to add enough terms so that the error of your approximation is no larger than r. Finally, you will also need to print out
the value of n used to compute the approximation.
I cannot construct a loop for the formula sin_(x) and the approximation.
this should not use math.h. so should be done with loop.
please tell me the code in C lang.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float sum,term,xd,x;
int i;
//accept value of angle in degrees
printf("Enter x in degree:");
scanf("%f",&xd);
//convert degree to radians
x=(xd*3.141552654)/180.0;
sum=0;
term=x;
//calculate sum using sine series
for(i=2;fabs(term)>0.000001;i++)
{
sum+=term;
term=-term*x*x/((2*i-1)*(2*i-2));
}
//final sum is the required sine value
printf("Sin (%f)=%f",xd,sum);
getch();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.