Problem 1 The following infinite series can be used to approximate e 6 1 Write a
ID: 1766736 • Letter: P
Question
Problem 1 The following infinite series can be used to approximate e 6 1 Write a program to approximate the value of cos x as follows: Using a loop you will add one more term to the approximation each iteration of the loop until your approximation is correct out to 5 significant figures. You may start off with your approximation equal to 1. Because we are starting off with the approximation equal to 1 this is our initial old value. Requirements: x must be entered using the input command Output should be as follows: The approximate value of cos (entered value) is f. ### It took # iterations to converge! 1. 2.Explanation / Answer
X = input("Enter vulue of X=")
cos_x = 0; % intialization of approximate value
i =0:2:10000;
for k = 1:1:length(i) % this for loop for iteration
cos_x = cos_x +( ((-1)^(k+1))*(X^i(k)) )/( factorial(i(k)) );
cos_x
if ( abs((cos(X)-cos_x)) <= 10^-6) % this condition for accuracy
break;
end
end
printf("Approximate value of cos(%f) is %f ", X,cos_x)
printf("It took %d iteration to converge ", k)
Output:-
Enter vulue of X=pi/3
X = 1.0472
cos_x = 1
cos_x = 0.45169
cos_x = 0.50180
cos_x = 0.49996
cos_x = 0.50000
Approximate value of cos(1.047198) is 0.500000
It took 5 iteration to converge
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.