CS101 - Lab Activity 3
Name:
____________________
NetID: ______________ Section#: ______
Name:
____________________
NetID: ______________ Section#: ______
Name:
____________________
NetID: ______________ Section#: ______
Each question is worth 1/4 point.
Objectives
In this lab, you will:
- Understand the difference between MATLAB functions and scripts.
- Learn to use function parameters and return values.
- Design and code the ex function for a hand calculator.
References
- Lecture 2.
- MATLAB Help: Using MATLAB
Programming
and
Data Types
M-File Programming.
- MATLAB Help: Using MATLAB
Mathematics
Matrices and Linear Algebra.
- Matlab by Pratap Chapter 4.2, 4.3 ,5.3 ,6.1.
Part 1 : Matlab Functions
- What variables do you see before running the script?
__________none
____________________________________________________________________________
- What variables do you see after running the script?
__________a b c____________________________________________________________________________
- Are variables that are created within a script accessible to you
in the
workspace?
Y
- Can workspace variables be overwritten by a script?
Y
- What is returned when you call the 'rolldice'
function?
________(any integer from 1 to 6)___________________________________
- What is the value of b after calling the 'rolldice'
function?
________1234
___________________________________
- What did you get when you executed r = dieroll ?
________(Error...)_________________________________________
- What do you get after running 'help rolldice'
the second time?
_____(some comments/help message for the function)______________________________
- What do you get after running 'rolldice(5)'
?
_____(any 5 integers from 1 to 6)______________________________________________
- What happens if you forget the argument to 'rolldice'?
_____(Error...)_________________________________________________________________________________
Part 2 : Kinetic Energy of a particle
Do not open the Matlab editor and write code for your
functions yet.
Just answer the following questions and then go back to the
instructions step 4) Write the code
- What value did Matlab return when you typed ke(m,0)?
____________ 0____
- What value did Matlab return when you typed ke(m, [0
, .5*c])?
__0__ 1.266550505461440e-014_or approx._
- What value did Matlab return when you typed ke(m,c)?
______ Inf
___________
- Fill in the blanks with the same values you typed above.
v = linspace( ____0_______ ,
_____.99*c__or __.99.*c______ , 200);
-
Write your code
for the function ke in the blanks given.
You may not need all the lines.
___________function ke = energy(m,v) _______no deduction
for using other names just so long as the function works correctly____
__________c = 299792458; _______________________________
__________ke = (1./sqrt(1 - v.^2./c.^2) - 1).*m.*c.^2;_________
Part 3 : Writing
your own function
- Write the function definition line (i.e. the first line)
for the function 'max_dist'. Use the
variables x, y and z. The inputs will be x and y, while z is the return
value. Don't write the entire function.
______function z = max_dist(x,y)__________________________________________________________
- For two points x = [ x1 x2
] and y = [ y1 y2 ], then x+y in
Matlab gives the result [ x1 + y1
, x2
+ y2 ].write a Matlab expression
which creates a vector w = [ x12
+ y12 x22
+ y22 ]. (Use only x and y in your
expression,
and don't use subscripting.)
_____w = x.^2 +y.^2;____or_______w = x.*x +y.*y;__________________________________________
Note that the expression above should work
for any number of points, as long as vectors
x and y have the same length. Don't
include this Matlab code
inside your function.
- The built-in 'sqrt' function computes the square-root, for
example if w
= [ 4 16 ] then sqrt(w) would return the vector
[ 2 4]. Write a Matlab expression which creates the vector
u =
using only the variables x and y.
_______u= sqrt(x.^2+y.^2);______or__________u= sqrt(x.*x+y.*y);__________________________
Don't include this Matlab code
inside your function.
- The built-in 'max' function computes the maximum value, for
example
if u = [2 4] then max(u) would return the value 4. Write a Matlab
expression
which
creates the vector z = max(
)
using only the variables x and y.
______z= max(sqrt(x.^2+y.^2));_or
____z = max(u);_____or
__variations______________________
Include this
statement in your function.
- TA signature
__________________________________________________________________
Part 4 : Absolute and Relative Error
- temp = ______________cumprod(1:100);______________no
deductions for missing ;_______
- temp2 = _____________fliplr(temp);_____________________________________________
- y = _________________polyval(p,x);________________________________________________________
-
TRUE
FALSE