Skip to content

MATLAB for engineers, a quick review:

December 3, 2012

matlab 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 input

2- 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 line

4- 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 is format short e.

6- Cleaning the screen: clc

7- 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: clear

5- Add/subtract to matrix A+B. A-B, array multiplication A.*B, array division A./B. Array power A.^n

6- 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./B

4-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), magnitude abs(Z), angle by angle(Z)

3- Arrays of complex numbers

System of linear equations: AX=B

1- 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:10 P(1,k) =sqrt(k); End

3- 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

end

4- Repetitive control structure- WHILE LOOP

External 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 x

2- Calculating Integral: sum(x)*interval Working with strings

String in MATLAB are string of characters

  1. Alway use single quote to define a string
  2. Adding two strings of x,y back to back is possible by strcat() , using + sign is a mistake
  3. Several ways to print a string out to workspace: Formatting strings in MATLAB:
  4. Type the name of the variable w/o a trailing semicolon
    1. disp() is almost the same as above, except it does not print out the variable name
    2. fprintf() 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 out
    3. sprintf() is for formatting text in order to create new string variable
  5. 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’
Cells and structures;
  1. Cell arrays can mix and match data types
  2. Structures can be used to organize and group information

Working with files

  1. fopen() ->  fid = fopen(filename, permission) ; fid: number returned by fopen which you will use to refer to this file, permission: ‘r’,’w’, ‘a’
  2. fclose() 
  3. 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
ps: I found this video channel for you. unfortunately, it has no voice. pps: More tutorials: Introduction to MATLAB by D.F. Griffiths
Advertisements

From → Cisco

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: