Sie sind auf Seite 1von 9

MATLAB

By
Examples
Abhishek Kumar
Gupta
MATLAB by Exampl es P a ge | 1


Lecture 1

Basi cs of MATLAB
MATLAB
MATLAB is a technical software.
It is a programming language.
It also provides interactive tools for various engineering.
MATLAB Environment
Desktop:
The normal layout is known as Desktop.

Command History
Stores the commands you type. View a log of or search for the statements you entered in
the Command Window, copy them, execute them, and more.

Command Window
Run MATLAB language statements. Works as cmd. Used to run your files

Current Directory Browser
View files, perform file operations such as open, find files and file content, and manage
and tune your files. In MATLAB, all the files should be in the current directory.

Editor
Create, edit, debug, and analyze M-files (files containing MATLAB language
statements).

Figures
Create, modify, view, and print figures generated with MATLAB.

Help Browser
View and search the documentation. A very important tool for your help.

Start Button
Run tools and access documentation for all your MathWorks products, and create and use
shortcuts for MATLAB.
MATLAB by Exampl es P a ge | 2


Variable Editor
View array contents in a table format and edit the values.

Workspace Browser
View and make changes to the contents of the workspace.
Mfiles:
Maltab code file is known as Mfiles. Their extension is .m. To open a file, type in the command prompt
edit filename.m
Alternatively you can also write your code in command line. But for large programs its better to
use Mfiles. Mfiles are of two types:

Script files
Script files are like main function of a C++ code. The starting code should be written in a Script
file. To Run a script file just type its name in the command window.
Function File
From Script files you can call functions file. Function files are like C++ functions. You can call a
function by typing
X=getMaximum([12 3 2]);
P code
Pcode is the protected m file. You can give it to some one without allowing him to see code. To
make p code type in the command window
pcode myfunc.m;
Getting started
Lets get started with a simple multiplication program.
A Simple multiplication program
Type the following in the command window or in a m file.
a=4;
b=5;
c=a*b;
display(c);
Remember if we remove ; from the end of the line, it will not generate an error but it will make
MATLAB to put output in the command window. So use of display command can be omitted by
writing the same code in this manner
a=4;
b=5;
c=a*b
MATLAB by Exampl es P a ge | 3

Calling a MATLAB function
MATLAB has a long list of functions. Lets try some of them.
Try the following by typing in a mfile(or in command window);
a=1.14;
b=sin(a);
c=sqrt(b);
Remember MATLAB is a intuitive language. Use your intuition to call functions.
Searching for a function
There are two types of help available.
Suppose you want to find is there any function for inverse sin. Type the following
lookfor inverse;
result is
acotd - Inverse cotangent, result in degrees.
acoth - Inverse hyperbolic cotangent.
acsc - Inverse cosecant, result in radian.
acscd - Inverse cosecant, result in degrees.
acsch - Inverse hyperbolic cosecant.
asec - Inverse secant, result in radians.
asecd - Inverse secant, result in degrees.
asech - Inverse hyperbolic secant.
asin - Inverse sine, result in radians.
>>
Terminate the output by pressing Ctrl+C as soon as you see asin.

Now if we want to see the syntax,
help asin;
to open the full help type helpwin;

Useful basic function for getting started
Input
A=input(please enter your marks);
Name=input(please enter your name,s);
Output
display(A);
A
Saving your variables
save A filename.mat;
save filename;

How to write your function
MATLAB by Exampl es P a ge | 4

Suppose if we want to make a function which give x
6
for any x, we have to make a function file.
Open a new mfile and type the following in it
function y=myfunc(x)
y=x^6;
end
Can I run windows commands on matlab
Try following
!mspaint
Matrices
MATLAB: MATrix LABoratory
Always remember in MATLAB, everything is a MATRIX. The variable you typed a=5; is also a
matrix(ofcourse 1*1). The name you entered is a matric of characters.

Matrices: Why so important
Try to think everything as a matrix.
Is image a matrix.. yes
Can any polynomial be representated as matrix.. yes as a vector.
A transfer function, polynomials, images, data, communication system, modulation.. yes
everything
Getting started
Lets make a row vector
A=[1 2 3 4 2 1 9 10];
Now a column vector
B=[1;2;3;4;2;1;9;10];
Its simple to create a 2 cross 5 matric
C=[1 2 3 41 1; 34 3 1 2 7];
To make quick arrays like a matrix with all 1 and size 4 cross 5;
D=ones(4,5);
Remember to use always help tool. Try
help zeros
try the following
help eye;
I=eye(4);
R=rand(4,5);
Colon Operator
Entering the whole data tired try the following
a=[1:10];
MATLAB by Exampl es P a ge | 5

b=[1:2:100];
: means to in MALTAB. To generate a span from n1 to n2 type the following
varname=[n1:step:n2];
The colon operator is vey powerful operator, we will explore its powers later.
Subscripting
To access the second row and fourth column member of Matrix A type
S=A(2,4);
Now try following
S=A(1,[2 4 5]);
You can use matrix inside a matrix to extract multiples rows and column. Result is a matrix with
1
st
row and 2, 4 and 5
th
columns of matrix A;

Try to extract 1 and 2 to 5 columns.
S=A(1,[2:5]);
Now what if we want to get all columns and second row
S=A(2,:);
: is also used as all
Try it
B=A(:,:);
You can also use subscripting to assign elements to matrixes
A(1,3)=4;
Or more high ones
A(1,4:5)=zeros(1,2);
Looks like some magics.. well the magic continues till we reach vectorization in third lecture.
Operations
Well the common addition etc operators act like matrix operations. Following commands
C=A*B;
Will give the matrix multiplication of C and B;
Try the following
C=A/B;
C=A+B;
Error will occur if the matrix are not compatible with the operations.
Well unlike C , you dont have to make a for loop to calculate matrix multiplications.
But does MATLAB does the same by itself.
Try the following
X=sum(A);
Y=diag(A);
MATLAB by Exampl es P a ge | 6

Z=max(A);
S=sort(A);
Try to write a data vector with marks of six students in a class and find out its average.
X=input(enter six values in vector);
[average is]
Z=sum(X)/length(X)s
Dot Operations
The real fun comes when we use Dot operations. Try the following
A=[1:10];
B=[4:14];
C=A.*B;
.* is a special operator which multiplies each element of A to corresponding element to B. try out
these
D=A./B;
E=A.^B;
F=A.^2;
Same question again,
Well unlike C , you dont have to make a for loop to calculate matrix multiplications.
But does MATLAB does the same by itself.

The answer is no. Maltab is designed to compute this operation faster than normal loop method.
Dont try to do it in loop method.
Remember to Avoid for loops.
Advances with MATRIX
Functions
All of the matlab functions assume that inputs are matrixs. Most of them work as dot operations.
Try it
X=[1:.01:1];
Y=sin(X);
Answer is a matrix with same size as X with sin of each element of X;
Your own functions are also valid for input matrixes.

One more thing, you can have multiple output from a function file. Try this
Function [A B]=myfun2(X,Y)
A=X.^2+Y.^2;
B=X.^2-Y^2;
Save it with name myfun2.m and call it as
[S1 S2]=myfun2([1:10],[2 5 4 3 0 67 2 1 4 9]);
Consider a data set with three variables:
MATLAB by Exampl es P a ge | 7

Heart rate
Weight
Hours of exercise per week

For five observations, the resulting array might look like
D = [ 72 134 3.2
81 201 3.5
69 156 7.1
82 148 2.4
75 170 1.2 ]
The first row contains the heart rate, weight, and exercise hours for patient 1, the second row
contains the data for patient 2, and so on. Now you can apply many MATLAB data analysis
functions to this data set. For example, to obtain the mean and standard deviation of each
column, use
mu = mean(D), sigma = std(D)
mu =
75.8 161.8 3.48
sigma =
5.6303 25.499 2.2107
Assignment:
1.
Create a vector of the even whole numbers between 31 and 75.
2.
Let x = [3 2 6 8]' and y = [4 1 3 5]' (NB. x and y should be column vectors).
a. Add the sum of the elements in x to y
b. Raise each element of x to the power specified by the corresponding element in y.
c. Divide each element of y by the corresponding element in x
d. Multiply each element in x by the corresponding element in y, calling the result "z".
e. Add up the elements in z and assign the result to a variable called "w".
f. Compute x'*y - w and interpret the result
3.
Create a vector x with the elements,


Add up the elements of the version of this vector that has 100 elements.
4.
Write down the MATLAB expression(s) that will
MATLAB by Exampl es P a ge | 8

A. Compute the length of the hypotenuse of a right triangle given the lengths of the
sides (try to do this for a vector of side-length values).
B. Compute the length of the third side of a triangle given the lengths of the other
two sides, given the cosine rule

Where t is the included angle between the given sides.
5.
Create an array called F
A. Compute the mean of each column and assign the results to the elements of a
vector called avg.
B. Compute the standard deviation of each column and assign the results to the
elements of a vector called s.

Das könnte Ihnen auch gefallen