Académique Documents
Professionnel Documents
Culture Documents
Basics MATLAB
What we will learn in this
session
The basic MATLAB interface.
Basic commands.
Declaring & manipulating variables.
Plotting graphs.
.
Basic MATLAB Interface
Command window: Type your
instructions here and press
ENTER to execute them.
Example: Declare a column matrix with
values 1,2 and 3.
Command history: a list of instructions
executed by MATLAB is shown here.
Workspace: shows a list of
variables created by MATLAB.
As you can see, the value of aaa
is shown.
Another way to create a variable
Is to press this button.
MATLAB will prompt you to enter
the variable name.
As you can see, the variable
name has been changed to bbb.
2) Or by double
clicking on bbb.
xxx = 1:100
yyy = rand(1,100)
plot(xxx,yyy)
Example
Example: Draw sin(x)
clear, close all
clc
x = 0:pi/36:10*pi
y = sin(x)
plot(x,y,m')
Example
Example: Plotting the lines using
line parameters
clear, close all
clc
xxx = 1:100
yyy = rand(1,100)
plot(xxx,yyy)
figure, plot(xxx,yyy,'g:') % the command figure is
figure, plot(xxx,yyy,'r--')% used to create a new
figure, plot(xxx,yyy,':mo')% figure each time a plot
% is made.
Example: Drawing two plots in the
same figure
close all
clc
xxx = 1:100
yyy = rand(1,100)
aaa = 1:100
bbb = rand(1,100)
plot(xxx,yyy,'r')
hold on
plot(aaa,bbb,'-.gv')
hold off
figure
plot(aaa,bbb,'-.gv')
Example: Drawing Bar
Graphs
clear, close all
clc
x = 0:pi/36:2*pi
y = cos(x)
bar(x,y,'b')
Example
Example: Drawing a Stair-Step
Plot
clear, close all
clc
x = -10:0.5:10
y = x.^2 + 2.*x + 2
stairs(x,y,'b')
Basic 2D Plotting
The simplest kind of plot is a cartesian plot of (x,y) pairs
defined by symbols or connected with lines
>> x=0:0.05:10*pi;
>> y=exp(-.1.*x).*sin(x);
>> plot(x,y)
>> xlabel('X axis description') Title for plot goes here
1
>> ylabel('Y axis description')
Legend for graph
>> title('Title for plot goes here')
>> legend('Legend for graph')
>> grid on 0.5
NOTE #2:
line(x,y) is similar to plot(x,y) -1
but does not have additional options 0 5 10 15 20 25 30 35
X axis description
Plotting Multiple Curves
>> X = 0.0:pi/100:2*pi;
>> Y1 = cos(X);
>> Y2 = 3*cos(X);
>> Y3 = cos(2*X);
>> Y4 = sin(X);
We could plot these using: 3
>> plot(X,Y1) 1
>> hold on
>> plot(X,Y2) 0
>> plot(X,Y3)
-1
>> plot(X,Y4)
-2
-3
0 1 2 3 4 5 6 7
Plotting Multiple Curves (contd)
Or we could do: 3
>> plot(X,Y1,X,Y2,X,Y3,X,Y4) 2
Or we could do this: 0
-1
>> Z = [Y1;Y2;Y3;Y4];
>> plot(X,Z) -2
-3
more markers. 0
distinctly?
-3
0 1 2 3 4 5 6 7
3-D Plotting
3-D Plotting
Thank You