Name: ____________________ NetID: ______________ Section#: ______
In this lab, you will:
Understand the difference between MATLAB functions and scripts.
Learn to use function parameters and return values.
Write a function to compute relativistic kinetic energy.
Write a function to find the maximum distance to the origin
Write a function that returns more than one value.
Use the conditional Matlab if statement
Lecture 2.
MATLAB Help: Using MATLAB
Programming and Data Types
M-File Programming.
MATLAB Help: Using MATLAB
Mathematics
Matrices and Linear Algebra.
MATLAB by Gilat: Sections 6.1, 6.2, 6.7.
Complete the MATLAB command to create a row vector of
10,000 random integers with either the value 0 or 1 (like flipping a
coin heads-1, tails-0). You must use the rand function.
x = floor ( .5 + ___________________________________ );
Write a single MATLAB command to compute the arithmetic average of 10,000 random integers generated by the 'rand' function in question #1 above. Use the MATLAB mean function and the vector x to compute the arithmetic average. Write the MATLAB command and not the answer (approx. .5) that your command returns. Use the MATLAB help facility if you have a question about using the mean function. If your mean isn't between .48 and .52 then probably your answer to #1 is probably incorrect.
_________________________________________________
Fill in the three blanks below to complete a MATLAB command
to create a column vector of 500 random real numbers with a
value between -2*pi and 2*pi.
y = ______________ +
_____________ * rand(_________________________________) ;
(You
can test your code by typing min(y) and max(y). These values should
lie close to -2pi and 2pi respectively.)
To find the roots of the polynomial 2x3 + 15x2 + 6x - 1, we type the following into the command window,
Write the
result.
____________________________________________________________________________
Write the single MATLAB command to find all the roots of -x + x100 . (Don't write the roots, just write the MATLAB command that gives the roots.)
>> p =
__________________________________________________
>>
roots(p)
Hint: x100 - x = x100 + 0*x99 + 0*x98+ ... + 0*x3 + 0*x2 - 1*x + 0. Create a vector [ 1 (followed by 98 0's) -1 0]. Use linspace or zeros function to create the 98 0's.
Use the polyval function to compute the value of x3 + 2x2 + 3 at x=3 . Don't write the solution, just write the MATLAB command that gives the solution.)
_________________________________________________________
Fill in the blank with the result that MATLAB gives when you type
the following commands.
>>
x = [ 5.9 5.1 -1 0 4.99 -3.99 ];
>>
floor(x)
ans
=
_____________________________________________________
>>
ceil(x)
ans
=
_____________________________________________________
>>
z = [1 3 5 7 9];
>>
cumsum(z) % note how this differs from the sum function
ans
=
_____________________________________________________
>>
w = 1:2:9;
>>
cumprod(w) % note how this differs from the prod function
ans
=
_____________________________________________________
Part 2: Scripts and Functions
______________________________________________________________________________________
______________________________________________________________________________________
Y / N
Y / N
___________________________________________
___________________________________________
___________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
______________________________________________________________________________________
______________________________________________________________________________________
Part 3: Kinetic Energy of a particle
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
_____________________________________________________________________________________
______________________________________________________________________________________
______________________________________________________________________________________
______________________________________________________________________________________
Part 4: Writing your own function
_________________________________________________________________________________
z =
_____________________________________________________________________________________________________________
_____________________________________________________________________________________________________________
19. Write the result MATLAB returns when you type the
following:
>> t = [ 3, 7, 1, 9];
>> max(t)
>>
min(t)
ans =
____________________________________________________________
20. Write the result MATLAB returns when you type the
following:
>>
m = 6;
>>
mod(m, 2)
>> n = 7;
>>
mod(n,2)
ans
=
_________________________________________________________
21. What values do you get for numbers, num, maxNum, minNum = __________________________ ?
22. What values do you
get for numbers, num, maxNum, minNum =
__________________________ ?