SUNY Geneseo Department of Mathematics
Math 230 01
Fall 2014
Prof. Doug Baldwin
Here are the answers I had in mind to the questions on hour exam 2. Of course these aren’t the only possible answers, and I will no doubt give full credit to solutions that don’t match these exactly, but you can use these as models for good answers to compare to yours, review as you study for the final, etc.
(Write a for
loop that fills vector W with the results of
calling function noVectors
on each element of vector V.)
The loop steps through all the indices into V and W, applying
noVectors
to the ith element of V
and placing the result in the ith element of W.
I assume that V has already been initialized, and that W is
an empty vector.
for i = 1 : length(V)
W(i) = novectors( V(i) );
end
(Give a Matlab function that computes the “igloo” function.)
The basic algorithm is simply a 3-way if
that figures out which
case of the function definition applies. It is embedded in a function definition
that takes x as its parameter and returns I(x).
function [ y ] = igloo( x )
if -2 <= x && x <= sqrt(3)
y = sqrt( 4 - x^2 );
elseif sqrt(3) < x && x < 2.5
y = 1;
else
y = 0;
end
end
(Write a Matlab function that calculates how many times you can take the square root of x before getting a value less than 2.)
Use a while
loop to repeatedly take the square root of x
until x is less than 2. Count the number of times the loop repeats, and
return that count as the function’s result.
function [ count ] = rootCount( x )
count = 0;
while x >= 2
x = sqrt( x );
count = count + 1;
end
end
(What will the bisection method converge towards if given the function f(x) = -1 for x<1; f(x) = 1 for x>1?)
Bisection will converge towards 1, because any midpoint greater than 1 that it calculates will make it lower the upper bound on its search region, and any midpoint less than 1 will make it raise the lower bound.
(If bisection ever hits exactly 1 as the midpoint the behavior depends on exactly how f is implemented. The question was posed in a way that ensures that this will never happen—1 will always be either 1/3 or 2/3 of the way through every interval.)