You are on page 1of 29

GHULAM KHALIQ Hassan afzaal

Variable names ARE case sensitive Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores.

ans Default variable name for results pi Value of inf Infinity NaN Not a number e.g. 0/0 i and j i = j = square root of minus one: (-1) (imaginary number) e.g. sqrt(-1) ans= 0 + 1.0000i realmin The smallest usable positive real number realmax The largest usable positive real number

Matlab has some special (reserved) words that you may not use, for example,
for end if while function return elseif case otherwise switch continue else try catch global persistent break

Variable Basics
>> 16 + 24 ans = 40
no declarations needed

>> product = 16 * 23.24 product = 371.84 >> product = 16 *555.24; >> product product = 8883.8

mixed data types

semi-colon suppresses output of the calculations result

Intro MATLAB

>> clear clear removes all variables; >> product = 2 * 3^3; clear x y removes only x and >> comp_sum = (2 + 3i) + (2 - 3i); y >> show_i = i^2; complex numbers (i or j) require >> save three_things no special handling >> clear >> load three_things >> who save/load are used to Your variables are: retain/restore workspace comp_sum product show_i variables >> product product = 54 use home to clear screen and put >> show_i cursor at the top of the screen show_i = -1

Intro MATLAB

Click to create a new M-File

Extension .m A text file containing script or function or program to run

If you include ; at the end of each statement, result will not be shown immediately

Scripts do not accept input arguments, nor do

they produce output arguments. Scripts are simply MATLAB commands written into a file. They operate on the existing workspace. Functions accept input arguments and produce output variables. All internal variables are local to the function and commands operate on the function workspace. A file containing a script or function is called an m-file If duplicate functions (names) exist, the first in the search path (from path command) is executed.

Intro MATLAB

function [a b c] = myfun(x, y) Write these two lines to a file b = x * y; a = 100; c = x.^2; myfun.m and save it on MATLABs path

>> myfun(2,3) % called with zero outputs ans = 100 >> u = myfun(2,3) % called with one output u = 100 >> [u v w] = myfun(2,3) % called with all outputs u = 100 v = Any return value which is not stored 6 in an output variable is simply w = discarded 4

Intro MATLAB

Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function is
function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2)

You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name

Examples Write a function : out=squarer (A, ind) Which takes the square of the input matrix if the input indicator is equal to 1 And takes the element by element square of the input matrix if the input indicator is equal to 2
Same Name

Another function which takes an input array and returns the sum and product of its elements as outputs

The function sumprod(.) can be called from command window or an m-file as

>> x = [0:pi/100:pi]; >> y = sin(x); >> plot(x,y), title('Simple Plot')

Intro MATLAB

>> z = cos(x); >> plot(x,y,'g.',x,z,'b-.'), title('More Complicated')


Line color, style, marker type, all within single quotes

Intro MATLAB

MAKING X-Y PLOTS


MATLAB has many functions and commands that can be used to
create various types of plots.

In our class we will only create two dimensional x y plots.

Plot title
1200

EXAMPLE OF A 2-D PLOT


Light Intensity as a Function of Distance
Theory Experiment

Legend

y axis label

1000

Text
800

Tick-mark

INTENSITY (lux)

Comparison between theory and experiment. 600

400

Data symbol

200

10

12

14

x axis label

16 DISTANCE (cm)

18

20

22

24

Tick-mark label

TWO-DIMENSIONAL plot() COMMAND


The basic 2-D plot command is:

plot(x,y)
where x is a vector (one dimensional array), and y is a vector. Both vectors must have the same number of elements. The plot command creates a single curve with the the abscissa (horizontal axis) and the (vertical axis).

values on

y values on the ordinate y

The curve is made from segments of lines that connect the


points that are defined by the elements in the two vectors.

and

coordinates of the

CREATING THE X AND Y VECTORS


If data is given, the information is entered as the elements of the vectors

x and y.

If the values of

y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The spacing (difference) between the elements of x must be such that the plotted curve
will show the details of the function.

PLOT OF GIVEN DATA


Given data: x y 1 2 2 6.5 3 7 5 7 7 5.5 7.5 4 8 6 10 8

A plot can be created by the commands shown below. This can be done in the Command Window, or by writing and then running a script file. >> x=[1 2 3 5 7 7.5 8 10]; >> y=[2 6.5 7 7 5.5 4 6 8]; >> plot(x,y) Once the plot command is executed, the Figure Window opens with the following plot.

PLOT OF GIVEN DATA

LINE SPECIFIERS IN THE plot() COMMAND


Line specifiers can be added in the Specify the style of the line.

plot command to:

Specify the color of the line.


Specify the type of the markers (if markers are desired).

plot(x,y,line specifiers)

LINE SPECIFIERS IN THE plot() COMMAND

plot(x,y,line specifiers)

Line Style Solid dotted dashed dash-dot

Specifier : --.

Line Specifier Marker Specifier Color Type red green blue Cyan magenta yellow y black r g b c m k plus sign circle asterisk point square diamond d + o * . s

LINE SPECIFIERS IN THE plot() COMMAND


The specifiers are typed inside the plot() command as strings. Within the string the specifiers can be typed in any order. The specifiers are optional. This means that none, one, two, or all the three can be included in a command. EXAMPLES:

plot(x,y)
plot(x,y,r) plot(x,y,--y)

A solid blue line connects the points with no mar


A solid red line connects the points with no markers. A yellow dashed line connects the points.

plot(x,y,*)
plot(x,y,g:d)

The points are marked with * (no line between the points.)
A green dotted line connects the points which are marked with diamond markers.

PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND


Year Sales (M) 1988 127 1989 130 1990 136 1991 145 1992 158 1993 178 1994 211

>> year = [1988:1:1994]; >> sales = [127, 130, 136, 145, 158, 178, 211]; >> plot(year,sales,'--r*')

Line Specifiers: dashed red line and asterisk markers.

PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND

Dashed red line and asterisk markers.

You might also like