Lab Exercise 4 on Introduction to Computing Using MATLAB | CS 1112, Lab Reports of Computer Science

Material Type: Lab; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2008;

Typology: Lab Reports

Pre 2010

Uploaded on 08/31/2009

koofers-user-15j
koofers-user-15j 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS100M Section (Lab) Exercise 4
1for-loop review
For each of the following sub-problems, complete the program so that it produces the desired result.
Do this on paper first and then check your solution using Matlab. You should not modify the given
code in any way—only fill in the blanks that are provided. In every case you will need to use for-loops
with an “increment” that is not one.
1. The following program reads an integer k, and outputs all the multiples of kup to 1000.
k = input(’Please enter a positive integer smaller than 1000: ’);
for j = ______________________
disp(j)
end
2. The following program reads in a real number xand an integer N, and computes the sum
PN
k=0
(1)kx2k
(2k)! to the first Nterms. (This sum converges to cos(x)asN→∞.)
x = input(’Please input a real number between 0 and pi/2: ’);
N = input(’Please input a positive integer: ’);
sum = 0;
for j = _______________________
sum = sum + (-1)^(j/2) * x^j / factorial(j);
end
disp(sprintf(’The sum of the first %d terms is %12.8f\n’, N, sum))
3. The following does the same thing as in part (2) above, but this time we are not allowed to use
exponentiation 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: ’);
sum = 1; % Explicitly assign the first term (when j=0)
sign = 1; % The sign of a term, either 1 or -1
jfact = 1; % Current value of j!
xtoj = 1; % Current value of x^j
for j = _________________________
sign = ________________________________;
jfact = ________________________________;
xtoj = ________________________________;
sum = ____________________________________________;
end
disp(sprintf(’The sum of the first %d terms is %12.8f\n’, N, sum))
1
pf2

Partial preview of the text

Download Lab Exercise 4 on Introduction to Computing Using MATLAB | CS 1112 and more Lab Reports Computer Science in PDF only on Docsity!

CS100M Section (Lab) Exercise 4

1 for-loop review

For each of the following sub-problems, complete the program so that it produces the desired result. Do this on paper first and then check your solution using Matlab. You should not modify the given code in any way—only fill in the blanks that are provided. In every case you will need to use for-loops with an “increment” that is not one.

  1. The following program reads an integer k, and outputs all the multiples of k up to 1000.

k = input(’Please enter a positive integer smaller than 1000: ’); for j = ______________________ disp(j) end

  1. The following program reads in a real number x and an integer N , and computes the sum ∑N k=

(−1)k^ x^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: ’); sum = 0; for j = _______________________ sum = sum + (-1)^(j/2) * x^j / factorial(j); end disp(sprintf(’The sum of the first %d terms is %12.8f\n’, N, sum))

  1. The following does the same thing as in part (2) above, but this time we are not allowed to use exponentiation 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: ’); sum = 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 = ________________________________; sum = ____________________________________________; end disp(sprintf(’The sum of the first %d terms is %12.8f\n’, N, sum))

2 Fibonacci numbers

The Fibonacci numbers are defined as Fn = Fn− 1 + Fn− 2 with F 1 = 1 and F 2 = 1. Notice that to calculate any Fn, you only need to know the two previous Fibonacci numbers—you do not need to keep track of the entire sequence at any time. Write a Matlab script to print the numbers Fn, Fn + 1 , Fn + 2,.. ., Fn+1 − 1 , Fn+1. For example, if n = 6, then your script prints 8, 9, 10, 11, 12, 13 since F 6 = 8 and F 7 = 13. Your script begins with the following statements:

n= input(’Input n: ’); value1= 1; value2= 1;

Use scalar variables only—a scalar is a variable that stores a single value at one time. Review FVL §3.2 if you need help with computing the Fibonacci numbers.

3 ASCII “drawing” in the Command Window

Write a script to “draw” the following figure in the Command Window using fprintf statements. Prompt the user to input an integer (n) for the number of asterisks on each side of the square. For example, if n is 5, then the following figure will be printed. You may assume that n > 3.






4 Step pyramid

[From last week] Complete the script stepPyramidSkeleton.m to draw a step pyramid. The base rectangle is L-by-H where H ≤ L. Each step has the same height H. The next rectangle up is 2/3 the length of the rectangle below, and so forth. The top step must have a length no less than H.

You will need function DrawRect—download it from the Lecture Materials page and put it in your working directory (the directory from which you will run your script). Use a while-loop.

Please delete your files from the computer before leaving the lab!