Introduction to Matlab
Dlnya sabir salih
Some Useful MATLAB commands
• who List known variables
• whos List known variables plus their size
• help Ex: >> help sqrt Help on using sqrt
• clear Clear all variables from work space
• clear x y Clear variables x and y from work space
• clc Clear the command window
Plotting with MATLAB
Plotting with MATLAB
>> % To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')
>> % To put a title on the plot, we would use:
>> title ('Title of my plot')
Plotting with MATLAB
• Vectors may be extracted from matrices. Normally, we wish to plot
one column vs. another. If we have a matrix “mydata” with two
columns, we can obtain the columns as a vectors with the
assignments as follows:
>> first_vector = mydata ( : , 1) ; % First column
>> second_vector = mydata ( : , 2) ; % Second one
>> % and we can plot the data
>> plot ( first_vector , second_vector )
• Suppose we want to plot multiple plots on a single set of axes.
• We might try and do it as follows:
• x = 0:0.01:1;
• y1 = x.^2;
• y2 = x.^3;
• plot(x,y1,’blue’)
• plot(x,y2,’red’)
• But only the red one shows up! What’s the deal? Well, when
you call plot, it will simply make the current figure using the
entries of the latest plot call, overwriting what came before.
To avoid this, we use the hold command,
• x = 0:0.01:1;
• y1 = x.^2;
• y2 = x.^3;
• hold on
• plot(x,y1,’blue’)
• plot(x,y2,’red’)
• hold off
MATLAB Relational Operators
• MATLAB supports six relational operators.
Less Than <
Less Than or Equal <=
Greater Than >
Greater Than or Equal >=
Equal To ==
Not Equal To ~=
MATLAB Logical Operators
• MATLAB supports three logical operators.
not ~ % highest precedence
and & % equal precedence with or
or | % equal precedence with and
Only a single operator in
MATLAB!!
• Create x as a vector of linearly spaced values
between 0 and 2π. Use an increment
of π/100 between the values. Create y as sine
values of x. Create a line plot of the data.
• x = 0:pi/100:2*pi;
• y = sin(x);
• plot(x,y)
• Define x as 100 linearly spaced values
between −2π and 2π. Define y1 and y2 as sine
and cosine values of x. Create a line plot of
both sets of data.
• x = linspace(-2*pi,2*pi);
• y1 = sin(x);
• y2 = cos(x);
• figure
• plot(x,y1,x,y2)