Вы находитесь на странице: 1из 4

# 1

LAB #1
Brief review of MATLAB instructions known from previous courses MATLAB use:
From prompt window Script file (file.m)

## Vector and matrix assignment:

X=[1,2,3,4]; or X=[1 2 3 4]; line vector Otherwise: X=[xmin:increment:xmax]; X=[1;2;3;4] or X=[1 2 3 4]; column vector A=[1 2; 3 4; 5 6]; 3 2 matrix Special matrices: ones(m,n) (m n) matrix all elements are = 1 zeros(m,n) (m n) matrix all elements are = 0 eye(n) (n n) identity matrix diag(X, m) creates a matrix with the X vector placed on the m-th diagonal and zero elsewhere. The m-th diagonal means the main diagonal if m=0, the first upper diagonal if m=+1, the first lower diagonal if m=-1, ...

## Matrix operation: if A e B are two matrices with the same size:

A+B sum A-B difference A*B scalar product A.*B product (element by element) A./B ratio (element by element) A^2 A*A (scalar product) A.^2 square (element by element) max(A) o min(A) MATLAB builds a vector containing the maximum (or minimum) element of each column. abs(A) absolute value of (A). sum(A,p) creates a row vector whose elements are sum BY COLUMN of the elements of A (if p=1) or a column vector whose elements are the sum BY ROW of the elements of A (if p=2)

## Main graphical commands:

plot(x,y,type_of_line or type_of_point) title('title_of_plot') xlabel('x_axis_label'); ylabel(y_axis_label); subplot(a,b,x) in the same graphical window, an a b matrix of different plots is created; the input for the x-th plot will follow (subplots are counted form top left to right bottom). x = ginput(1); MATLAB (pauses the execution of the script file and) waits for an input (click) from the mouse: x will contain the coordinates of the point where you have clicked. hold on a second line can be plotted in the same graphical window (should follow a first plot statement. BE CAREFUL: the second line will not be graphically distinguished from the first) hold off opposite to hold on
Oct. 11-14, 2011

CM Lab #1

## Main I/O commands:

disp(print_this_sentence_on_the_screen); disp(sprintf(Place here a C-format alphanumeric string)) similar to the previous one, but allows more accurate formatting q = input(give me an input'); MATLAB (pauses the execution of the script file and) waits for an input form the keyboard load filename.dat or load filename.mat

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: ==, <=, >=, ~=, <, >,&, ~, |

## Main pre-defined functions:

sin(x) cos(x) exp(x) log(x) natural logarithm

## Other useful commands:

clear('pippo') MATLAB clears the variable pippo form memory close(xx) MATLAB close the graphical window # xx clear all / close all MATLAB clears all the variables and closes all the graphical windows RECOMMENDED AT THE BEGINNING OF ANY SCRIPT % before comments in the script files size(A), length (B) dimension (rows, columns) of matrix A, number of elements of vector B tic/toc start/stop an internal clock. Useful to time code execution clc MATLAB clears all the lines in the command prompt help ...(name of a predefined MATLAB function) shows the full description of the function: inputs, outputs, how it works find(logical expression) returns the index of the elements in the vector/matrix satisfying the logical expression written in input.

CM Lab #1

## EXERCISES (to be solved using script files)

1) Assign the following matrices: A=[2 5 B=[3 6 8 1 3]; 4];

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]

## 3) Assign the matrix Q: Q=[1 2 3 4 1 9 6 8 4 2 3 4 6 3 7 8 1 6 3 4 5 8 5 5 2 3 4 4 5 6 1 7 3 9 5 6 5 6 7 3 9 1 7 7 4 6 7 8 5 2 8 5 9 8 7 8 9 7 4 6 3 2 6 8 9 1 9 6 4 1 4 8 9 1 2 2 8 2 4 6 6]

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