# MATLAB for engineers, a quick review:

You can ignore this post as, I have never asked, or heard from my gangs who got interview at Cisco hardware team, that any questions related to MATLAB were asked there. I personally had a tutoring sessions with my students at McGIll, and I decided to review MATLAB here for you, as you are an engineer and having knowledge of MATLAB is a MUST for you.

Note: What in many other programming languages can only be done with loops, Matlab can often do with vectors

Desktop calculations:1-At the end of each command line putting

;will not echo the input2- Arithmetic:

+ – * / \(left and right divisions, note that MATLAB has two operation for division)^: Then 6/3 is 2 and 3\6 is 2, too.3- Last line editing: by pressing

Up/Down buttons; you can recall the last line4- A few built-in functions:

sin(x), cos(x), tan(x), sqrt(x), exp(x), log(x), log10(x), acos(x), asin(x), atan(x), atan2(x,y)5- Naming constants and variables

: MATLAB is Case-sensitive. Therefore, Pi, Pi, PI; pi are different 7- Format: 5 digits display is default, but 15 digits display can be achieved by command:format long, for scientific nation command isformat short e.6- Cleaning the screen:

clc7- NaN : Not a Number : I got reject from a company by not knowing it. These values result from operations which have undefined numerical results

One-dimensional arrays

1-Define a row and column matrix: space and comma are equal [1 2 3] = [1,2,3]

2-Indexing for a

matrix(x,y)3- Size of a matrix:

size(Matrix),Length of vector:length(Matrix)4- Clearing variables:

clear5- Add/subtract to matrix

A+B.A-B, array multiplicationA.*B, array divisionA./B. Array powerA.^n6- Defining equally distance elements,

A=[1:1:10];7-Simple plots:

plot(x,y,’color’), grid on/off, xlabel(‘’), ylabel(‘’), figure, hold on/off, hist(Y,X)Matrices

1- Defining a matrix:

A=[1 2 3;4 5 6]2- Juxtaposition and sub matrix C

= [A B], C=[A;B], C=A(:,1), A(:,1), A(1:2,1:2’]3- Elementary operations with matrices

A+B, A-B, A.*B and A*B, A./B4-

ones(m,n), zeros(m,n)Complex numbers

1-imaginary unit is i or j but j is preferred professionally, since, ‘i’ is as current unit in electrical engineering , note that there is no need to write * before i,j

2- real part:

real(Z),imaginary part: imag(Z),magnitudeabs(Z), angle byangle(Z)3- Arrays of complex numbers

System of linear equations:

AX=B1- Determinant:

det(A)2- Crammer’s rule:

X=[det(D1);det(D2);det(D3)]/det(A), D1=A, D1(:,1)=B, D2=A, D2(:,2)=B, …..3- Matrix inversion

inv(A)4-Solving systems of linear equations in MATLAB

Polynomials

1-Polynomial representation: by its coefficients

2- Getting roots:

roots(coeff)3- Multiplication is achieved by convolving

conv(c1,c2)4-Division is achieved by deconvolving

deconv(c1,c2)Programming in MATLAB

1- Programming a function:

function y=Alan(x1, x2)2- Repetitive control structure- FOR LOOPS: e.g.

for K=1:1:10P(1,k) =sqrt(k);End3- Conditional control structure:

If condition

Expression

Elseif condition>>>>>> look at the here elseif are one work but in C++ it was else if

Expression

Expression

Else

Expression

end4- Repetitive control structure-

WHILE LOOPExternal files and programs:

1-Saving data:

save filename.mat variable / save(filename, variables)2- Loading data

X=load(filename.)Differential and integral

1- Calculating differential:

diff(x)/interval: consider that length of diff(x) is less that length of x2- Calculating Integral

: sum(x)*intervalWorking with stringsString in MATLAB are string of characters

- Alway use single quote to define a string
- Adding two strings of x,y back to back is possible by
strcat() , using + sign is a mistake- Several ways to print a string out to workspace: Formatting strings in MATLAB:
Type the name of the variablew/o a trailing semicolon

disp()is almost the same as above, except it does not print out the variable namefprintf()is for formatting text and printing out to a file or other device, such as the workspace. It is a very powerful command for formatting strings, combining them, and printing them outsprintf()is for formatting text in order to create new string variable- Note that : using cell array for many elements are better that making matrix of the names = [‘Jonas’,’Fred’,’John’] meansnames = JonasFredJohn butnames = {‘Jonas’,’Fred’,’John’} means names = ‘Jonas’ ‘Fred’ ‘John’

- Cell arrays can mix and match data types
- Structures can be used to organize and group information

Working with files

fopen()-> fid = fopen(filename, permission) ; fid: number returned by fopen which you will use to refer to this file, permission: ‘r’,’w’, ‘a’fclose()- Other ways to write to files:
ìcsvwrite(), ìdlmwrite()>> myFileID = fopen(‘testfile.txt’,’w’)

myFileID = 3

>> x = 100;

>> fprintf(myFileID,’X isequal to %d\n’,x);

>> fclose(myFileID);

**Best**

**Ali**

**post no 12**

**Introduction to MATLAB**by D.F. Griffiths