Basic Matlab Exercises
Basic Matlab Exercises
2 / 2 * 3
6 - 2 / 5 + 7 ^ 2 - 1
10 / 2 \ 5 - 3 + 2 * 4
3 ^ 2 / 4
3 ^ 2 ^ 2
2 + round(6 / 9 + 3 * 2) / 2 - 3
2 + floor(6 / 9 + 3 * 2) / 2 - 3
2 + ceil(6 / 9 + 3 * 2) / 2 - 3
2, 4, 6, 8, ...
10, 8, 6, 4, 2, 0, -2, -4
1, 1/2, 1/3, 1/4, 1/5, ...
0, 1/2, 2/3, 3/4, 4/5, ...
b.
c.
d.
e.
f.
et(1 + cos(3t))
cos2(t) + sin2(t)
tan-1(1) (this is the inverse tangent function)
cot(t)
sec2(t) + cot(t) - 1
x(3)
x(1:7)
x(1:end)
x(1:end-1)
x(6:-2:1)
x([1 6 2 1 1])
sum(x)
A'
A(:,[1 4])
A([2 3],[3 1])
reshape(A,2,6)
A(:)
flipud(A)
fliplr(A)
[A A(end,:)]
A(1:3,:)
[A ; A(1:2,:)]
sum(A)
l. sum(A')
m. sum(A,2)
k. [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]
12. Given the array A from problem 4, above, provide the command that will
a.
b.
c.
d.
e.
x > y
y < x
x == y
x <= y
y >= x
x | y
x & y
x & (~y)
(x > y) | (y < x)
(x > y) & (y < x)
if x < 6
if 6 <= x < 20
if 20 <= x <= 35
You can check your answer by plotting y vs. x with symbols. The curve
should be a triangular shape, always above zero and with a maximum of 16.
It
Write brief scripts to evaluate the following functions. If you start each script with a
request for input (using input), you'll be able to test that your code provides the
correct results.
5. h(T) = T - 10
= 0.45 T + 900
Test cases:
6. f(x) = -1
= 0
= 1
a. T = 5, h = -5
b. T = 110, h = 949.5
if x < 0
if x = 0
if x > 0
t(y) =
=
=
=
200
200 + 0.1 (y - 10,000)
1,200 + 0.15 (y - 20,000)
5,700 + 0.25 (y - 50,000)
Test cases:
a. y = 5,000
b. y = 17,000
b. y = 25,000
c. y = 75,000
1. Given the vector x = [1 8 3
will
t
t
t
t
9
=
=
=
=
0
when
when
when
when
y
y
y
y
is
is
is
is
below 10,000
between 10,000 and 20,000
between 20,000 and 50,000
above 50,000
200
900
1,950
11,950
1], create a short set of commands that
aij = xiyj
bij = xi/yj
ci = xiyi, then add up the elements of c.
dij = xi/(2 + xi + yj)
eij = reciprocal of the lesser of xi and yj
4. Write a script that will use the random-number generator rand to determine
the following:
a. The number of random numbers it takes to add up to 20 (or more).
b. The number of random numbers it takes before a number between 0.8 and
0.85 occurs.
c. The number of random numbers it takes before the mean of those numbers
is within 0.01 of 0.5 (the mean of this random-number generator).
It will be worthwhile to run your script several times because you are
dealing
with random numbers. Can you predict any of the results that are
described above?
5. Write a script that asks for a temperature (in degrees Fahrenheit) and
computes the equivalent temperature in degrees Celcius. The script should
keep running until no number is provided to convert. [NB. the function
isempty will be useful here.]
1. Compute the value of pi using the following series
How accurate is
Fn / Fn-1
It is claimed that this ratio approaches the value of the golden mean
( (1 + sqrt(5))/2 ). What do your results show?
3. The Legendre polynomials (Pn(x)) are defined by the following recurrance
relation
=
=
=
=
=
a
(a + b)/2
sqrt(b*y)
t - x*(y - a)^2
2*x
In
the
following conversion table:
Roman
I
V
X
L
C
D
M
Decimal
1
5
10
50
100
500
1000
b. The "new" style where the order of the symbols does matter. For
example,
IX is 9 (10 - 1), XC is 90 (100 - 10). The conversion table given
above
still holds and you may assume for this case that the only instances of
"order" you will encounter are
IV (4), IX (9), XL (40), XC (90), CD (400) and CM (900)
The function input will be useful here.
The format
Compute statistics,
a. One that uses two for-loops to explicitly carry out the calculations,
element by element. An "inner" loop should accumulate the product and
an
"outer" loop should more through the elements of the vector p.
b. One that uses the built-in function prod to replace the inner loop.
In each case, you can check your results with the built-in function,
cumprod.
12. Follow the directions for Exercise 11 but create a function that computes
the
cumulative sum of the elements of a vector. The elements of the
cumulative sum
vector are defined by
sj = x1 + x2 + ... + xj
for j = 1: length of the vector x.
The built-in functions sum and cumsum should be used instead of prod and
cumprod,
respectively.
13. Create a function that generates random arrays of integers beween a and
b,
inclusive. Use the definition line
function A = randint(a,b,M,N)
where a and b define the (inclusive) range and M and N define the size
of the output array (rows and columns, respectively).
a. Test your function with the following piece of code:
x = randint(10,17,100000,1);
hist(x,10:17)
The resulting histogram should be nearly flat from 10 to 17.
particular
attention to the endpoints of the distribution.
b. Test your function with the following pieces of code:
x = randint(-30,5,100000,1);
hist(x,-30:5)
x = randint(-45,-35,100000,1);
hist(x,-45:-35)
x = randint(7,-2,100000,1);
hist(x,-2:7)
Pay
a. on rectangular paper
b. on semilog paper (logarithm on the y-axis)
c. on log-log paper
Be sure to use an appropriate mesh of x values to get a smooth set
of curves.
10. Make a good plot (i.e., a non-choppy plot) of the function
f(x) = sin(1/x)
for 0.01 < x < 0.1.
11. In polar coordinates (r,t), the equation of an ellipse with one of its
foci at the origin is
r(t) = a(1 - e2)/(1 - (e)cos(t))
where a is the size of the semi-major axis (along the x-axis) and
e is the eccentricity. Plot ellipses using this formula, ensuring
that the curves are smooth by selecting an appropriate number of points
in the angular (t) coordinate. Use the command axis equal to set
the proper axis ratio to see the ellipses.
12. Plot the expression (determined in modelling the growth of the US
population)
What