Matlab Exercises
Matlab Exercises
MATLAB exercises
1. Start MATLAB. Type x = -1:0.2:1 to create an x-axis vector. Then execute the
following statements and examine the effects.
sqrt(x)
sin(x)
cos(x)
x.^2
x.^3
plot(x, x.^2)
plot(x,sqrt(x))
2. Examine what happens when you execute the following statements:
x = [1 2 3 4]
y = -2:2:4
x.^y
x.*y
x./y
x*y
x*y
x*y
3. Make a matrix by typing M = rand(4)
Find the inverse of M, using the function inv.
Multiply the inverse and M together and watch the result!
4. Make a matrix by typing M = randn(5)
Examine
M(:,3)
M(1:2,4)
M(3,:)
M(3,4)
M(4,3:4)
diag(M)
sum(M)
5. Solve the following system of linear equations using inv, \ and /
3x + 2y + z = 2
5x y z = 6
x+y+z = 0
14. Write a script that solves the equation 1 + x = 0 with Newtons iterative method. If
the equation to solve is f ( x ) = 0 , and the nth estimate of a root is xn, then the next
estimate x n +1 = x n
f ( xn )
.
f ( x n )
Start at an arbitrary point in the complex plane and study where the procedure ends.
Then make a script that covers the whole area abs(real(x))<1 and abs(imag(x))<1 with
starting points. Color the starting points differently according to where the iteration
ends. Then limit the starting points to interesting intervals.
15. A sine function may be expressed as f ( x ) = a cos( x ) + b sin( x ) . Generate a vector x
and then f(x) with some a and b. Now, with x and f(x) given, write a script that
identifies a and b using some least square estimate. A least square estimate minimizes
the error between the given function and the solution.
16. Study some useful MATLAB functions by making simple examples. Study for
instance:
clock
date
now
datestr
datetick
tic and toc
17. Study the text function. Write a simple script where you plot a function and plot the
text maximum is = and then the maximum value. The text should automatically be
placed near the maximum of the function in the plot.