Name: ____________________ NetID: ______________ Section#: ______ (e.g. AYA, AYB, ...)
Name: ____________________ NetID: ______________ Section#: ______
Name: ____________________ NetID: ______________ Section#: ______
Fill in the blank below to find the roots of the polynomial f(x)
= x3
- 3x2+ 2x .
.
>> f = ______ [ 1 -3 2
0] _______________________;
>result = roots(f)
Write the result you get (the roots).
0.0000
2.0000
1.0000
result =
___________above may be written vertically or horizontally
for full credit and 2.0000 may be written as
2_________________
>> check = polyval( _____ f _______, __________
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 = _____ [ 1, linspace(0,0,98), -1, 0 ]
______or ________ [ 1, zeros(1,98), -1, 0] ________(commas
not necessary)___________
>> roots(p)
Hint: x100 - x = x100 + 0*x99 + 0*x98+ ... + 0*x3 + 0*x2 - 1*x + 0 . Create a vector, p [ 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 x4 + 2x2 + 3 at x= -2 . Don't write the solution, just write the MATLAB command that gives you the solution.)
____________ polyval(
[ 1 ,0 , 2, 0, 3], -2)
_______________________________________________________
Use the Matlab sum
function and : and .^ operators to compute 20-20+18-18+16-16+
...+2-2
total = ____________ sum( (20:-2:2) .^ -(20:-2:2) )
OR sum(
(20:-2:2) .^ (-20:2:-2) )
______________(both pairs of inner parenthesis are
necessary)____________________
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 -0.99 ];
>> floor(x)
ans =
_______________ 5 -6
-1 0
4 -1
______________________________________
>> ceil(x)
ans =
______________ 6 -5
-1 0
5 0
_______________________________________
>> z =
[8 7 5 2 1];
>>
cumsum(z) % note how this differs from the sum function
ans =
______________ 8 15 20 22 23
_______________________________________
>> w =
1:2:7;
>>
cumprod(w) % note how this differs from the prod function
ans =
________________ 1 3
15 105
_____________________________________
What variables are listed in your workspace after you run the clear command?
__________________ none i.e no variables _______________________________________
_________________a, b, c ________________________________________
Are variables that are created
within a script accessible to you in the workspace? Y /
N _____ Y_________
What do you get after running help rolldice the second time?
function c =
rolldice()
rolldice: simulate
the roll of a six sided die.
returns a random
integer between 1 and 6 inclusive.
______________ or the comments in the function are displayed ________________________________________
Part 3: Kinetic Energy of a particle
What value did Matlab return when you typed energy(m,0)?
>> energy( m , 0)
_________________ 0
_____________________________
>> energy( m , [
0 , .5*c ])
ans =
0
1.26655050546144e-14
______________________________________________
Fill in the blanks to create a vector v that has 200 equally spaced values from 0 to .99*c.
>> v = linspace(
_____ 0 ______
, ____ .99*c
___________ , 200);
Write your code for the function energy in the blanks given. You may not need all the lines.
______________ function ke = energy(m,v)
________________________________
_______________ c = 299792458;
_______________________________
_______________ ke = ( 1./sqrt(1 - v.^2 ./
c.^2) - 1) .* m .* c.^2;
_______________________________