Anda di halaman 1dari 4

ICAs for Matlab Review

ENGR 112, Spring 2017


1. (loops) Write a Matlab function called my_sum.m that adds up the values in an array, using a loop.
You cannot use the built-in sum function. You may use other built-in Matlab functions. You may
assume that the array input to the function contains only real numbers (no error checking is
required in your function.)
Example output for this function is given below:
>> x = [9 7 4 8]

x =

9 7 4 8

>> z = my_sum(x)

z =

28

>> x = [1:10]

x =

1 2 3 4 5 6 7 8 9 10

>> z = my_sum(x)

z =

55
2. (loops, conditionals) The figure below shows a cylindrical tank of height H, and radius r with a
spherical cap on each end (also with radius r).

h
r

If the height of the liquid is h, what is the volume of the liquid in the tank? Write a Matlab script
which will allow the user to enter different values of h, return the volume, and only stop when the
user enters a negative value for h.
(Massive hint: there are three possible cases for h
If h is less than r, we need the volume of a partially filled sphere, given by
1
= 2 (3 )
3
If h is greater than r (the lower hemisphere is filled), but less than H-r (the liquid has not
reached the upper hemisphere), the volume is given by
2
= 3 + 2 ( )
3
(Why is this the right equation? Can you explain it?)
If h is greater than H-r, we need the volume of the cylinder between the two end caps,
plus the volume of a filled sphere minus the volume of the empty space in the upper
hemisphere:
4 1
= 2 ( 2) + 3 ( )2 (3 + )
3 3
Your code should use a conditional structure to choose which equation to use, based on the
user input. It should also deal with out-of-range values (i.e. values of h which are greater than
H), and do error checking to make sure the input makes sense (i.e. the input needs to be a real
number.)
3. (loops, conditionals) Edmond Halley (of comet fame) invented a fast algorithm for computing the
square root of a number. Given a number 1, Halleys algorithm calculates in the following
manner:
[1] Start with an initial guess for the square root, given as 1 (hint: an initial guess of 1 almost
always works)
1
[2] Calculate the intermediate step = 2 , where n is the iteration number


[3] Calculate the next iteration approximation +1 = 8
(15 (10 3 ))
[4] Repeat from [2] until the convergence criterion |+1 | (where is some small
number) is met.

Write a Matlab function called Halley_sqrt.m which takes two inputs (A and ) and returns the
approximation of the square root of A. Compare your results with the built-in Matlab function
sqrt.m. Can you determine which is faster for = 0.0001 your function or the built-in function?
4. (strings, loops) An anagram is a word or phrase formed by rearranging the letters of another word or
phrase. Examples include:
listen silent
cinema ice man
a telescope to see place
Spaces obviously dont count when determining if two sets of words are anagrams. Case doesnt
matter.
Write a function called anagram.m that compares two strings and returns 1 (logical true) if they
are anagrams, and 0 (logical false) if they are not. You can use any built-in functions you like (and
no, there isnt a built-in anagram function.)
Examples:
>> r = anagram('dormitory','dirty room')

r =

>> r = anagram('yes','no')

r =

>> r = anagram('dog','cat')

r =

>> r = anagram('conversation','conservation')

r =

Anda mungkin juga menyukai