Professional Documents
Culture Documents
LAB #1
Brief review of MATLAB instructions known from previous courses MATLAB use:
From prompt window Script file (file.m)
CM Lab #1
Loops:
for counter = m : i : n commands end while expression commands end if expression commands elseif expression commands end in expression logical operators can be used: ==, <=, >=, ~=, <, >,&, ~, |
CM Lab #1
a) Whats the result of: A'? b) Whats the result of: A*B? And of A*B'? c) Whats the result of: A.*B? d) Whats the result of: A.^B? e) Whats the result of: C=[A' B' A' B'] ? f) Whats the result of: C(3,2)? g) Whats the result of: D=[A; B; A; B]? h) Whats the result of: D(2,3)? i) Assign the matrix W=[D D;C C] and the vector w=[1 3 5 7]. Whats the value of q=W(w,2)? And z=W(w,:)? l) Assign the matrix: X =[ 1 2 ... 16 4 5 ... 19 0.1 0.2 ... 1.6 0.1 0.3 ... 3.1] (Try to use an efficient MATLAB notation).
2) Using the special matrices ones, zeros and eye, assign the matrix: A=[0 0 1 1 1 2 2 2 0 0 1 1 1 4 4 4 1 2 3 0 0 0 2 0 2 1 2 0 0 0 0 2 3 2 1 0 0 0 0 0 1 0 0 3 3 6 7 0 0 1 0 3 3 6 0 7 0 0 1 3 3 6 0 0]
And then: a) Compute the sum of the terms in the odd columns. b) Compute the sum of the terms in the main diagonal. c) Compute the sum of the outermost frame elements, which are not 4. d) Compute the sum of all the matrix terms, excluding those equal to 9, close to 7.
CM Lab #1 Oct. 11-14, 2011
4) Execute the following MATLAB commands: clear all; tic; for k=1:6e4; a(k) = k; end; toc; clear all; tic; a = zeros(1, 6e4); tic; for k=1:6e4; a(k) = k; end; toc; clear all; tic; a = [1:6e4]; toc; Try to explain the difference in the execution times. 5) We want to plot the function y=sin(x) with 0x50, using the MATLAB pre-defined function sin(x). a) Assign a vector x containing all the points from 0 e 50, with increment 0.01, and compute the y vector, either with a for loop and with an array-smart command. b) Plot y as a function of x. c) Use the commands xlabel, ylabel, gtext, title to complete the plot; then save your work in a postscript file using the instruction: print -depsc filename.
6) Try to assign, evaluate and plot the function 1/(1+x^2) in the range [-2:2] using the following instructions: A. fun='1./(1+x.^2)'; y=eval('fun'); fplot(fun,[-2,2]) or fplot('1./(1+x.^2)',[-2,2])
B. x=linspace(-2,2); fun=inline('1./(1+x.^2)'); y=fun(x); plot(x,y) or x= linspace(-2,2); fun=inline('1./(1+x.^2)'); y=feval(fun,x); plot(x,y) C. D. x= linspace(-2,2); fun=@(x)[1./(1+x.^2)] (anonymous function); y=fun(x); plot(x,y) by building a suitable function in a .m file.
7) Compute the solution of the equation 1 - x^2 = exp(x) using one of the instructions listed above, and the instruction fzero. Try to apply fzero in the following ways: fzero(fun,attemptvalue), fzero('function', attemptvalue), fzero(@anonymous function, attemptvalue).
CM Lab #1