Sei sulla pagina 1di 2

MIE 124

Homework 1
Due January 28, 2014

Formatting Note: Future homework will simply require that you submit your own m-file.
However, for Homework 1, please submit a Microsoft Word document with your responses. For
some questions, you may be asked to provide a brief ~1-2 sentence comment on your answer. In
all cases, you should copy and paste the Matlab command you entered, and then if relevant the
resulting answer that Matlab prints. For example, if a question asked you to define a variable x
with a value of 3, y with a value of 5, and then determine the sum of x and y, a proper response
would be:

>> x=5;
>> y=3;
>> x+y

ans =

I literally just copied and pasted this from the Matlab command window. You could then briefly
comment: “Matlab performs addition correctly.” Or something equally inane for such a simple
example. The actual questions will be a tad more complicated.

1. Recall your high school trig, and let’s explore how Matlab calculates trig functions.
a. In Matlab, calculate sin(30) and sin(pi/6). What units does Matlab use for the sin
function?
b. Now try sind(30) and sind(pi/6). How does sind differ from sin?
c. Try sin(pi/6+2*pi). Does this make sense?
d. Try asin(0.5). Does this make sense?

2. Defining variables. Try this out:

x=2*3.14;
y=1+x;
x=5;

What is y now? Comment on why it does not equal 6.

3. Formatting and significant digits. Let’s define two variables. x=1/3, and y=0.333333.
a. When you look in the workspace pane or if you type x and y into the command prompt,
what do you see? Do you think x and y are really equal?
b. Now change the format to long, and type x and y into the command prompt. Now do you
think they are equal?
c. Change the format back to short and determine x-y. Comment on your answer.

1
4. Rounding. Matlab allows us to round numbers in different ways. Let’s try it out.
a. Try out round(3.1), round(3.49), round(3.5), and round(3.999). What does round do?
b. Now try the ceil function. What is ceil(3.000000001), ceil(3.9999999999) and
ceil(2.999999999)?
c. Repeat part b but use the floor function. How do ceil and floor work?
d. rem can be a useful function to find remainders. Try our rem(3,1), rem(3,2), and
rem(3,3). Describe how you think rem works.

5. Random numbers. First type rng('shuffle') to get a new seed for the random number generator
in Matlab. For each series of random numbers below, please determine the Matlab command
that would give this series, using only the rand function to generate the random number (not
randi). For example, a random number between 0 and 1 would just be rand(1).
a. A random number between 0 and 5.
b. A random number between 3 and 5.
c. A random integer between 11 and 25.
d. A random integer between -15 and -6.
e. A random odd integer between 0 and 100.

6. Other math functions.


a. What are log(exp(1)) and log(exp(2)). What base is log?
b. What about log10(10,000). What base is log10?
c. Try entering -9^.5 and (-9)^.5. Why do they differ? How does Matlab represent an
imaginary number?
d. Try abs(-9)^.5. What does abs do?

7. We haven’t discussed plotting yet, but let’s try out a few very simple exercises. You can
paste the plots into your word file directly.
a. Try out plot(2,4). What does this do (you may have to squint)? Now try (plot(2,4, '*').
b. Try plot(rand(10,1)). What do you think is happening?
c. You can use help plot to find formatting tips, for example, you can try plot(rand(10,1),'r--
d'). What do you think the ‘r’, ‘--', and ‘d’ do to the format?

Potrebbero piacerti anche