

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: Lab; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2008;
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS100M Section (Lab) Exercise 4
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.
k = input(’Please enter a positive integer smaller than 1000: ’); for j = ______________________ disp(j) end
(−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))
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))
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.
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.
[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.