

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS1112 Section Exercise 5
Let the sum of the first n terms be Tn. As n increases one expects the ratio (^) TTnn+1 to approach 1. Write
a script to find the smallest n such that the ratio (^) TTn+1n > .9999. Display n and Tn. Do not use arrays.
Hint: You need to keep track of a current sum and the next sum in the loop.
(a) The following program reads in a real number x and an integer N , and computes the sum ∑N k=
(−1)kx^2 k (2k)! to the first^ N^ terms. (This sum converges to cos(x) as^ N^ → ∞.) x = input(’Please input a real number between 0 and pi/2: ’); N = input(’Please input a positive integer: ’); total = 0;
for j = _______________________ total = total + (-1)^(j/2) * x^j / factorial(j); end fprintf(’The sum of the first %d terms is %12.8f\n’, N, total);
(b) The following does the same thing as in part (b), but this time we are not allowed to use expo- nentiation and the factorial function, and must compute these explicitly.
x = input(’Please input a real number between 0 and pi/2: ’); N = input(’Please input a positive integer: ’); total = 1; % Explicitly assign the first term (when j=0) sign = 1; % The sign of a term, either 1 or - jfact = 1; % Current value of j! xtoj = 1; % Current value of x^j
for j = _________________________
sign = ________________________________;
jfact = ________________________________;
xtoj = ________________________________;
total = ____________________________________________; end fprintf(’The sum of the first %d terms is %12.8f\n’, N, total);