You are on page 1of 32

ChE 208

Lecture # 6
MATLAB Basics

Dr. Nahid Sanzida


nahidsanzida@che.buet.ac.bd
What we will learn in this session

„ Basic commands.
„ Declaring & manipulating variables.
„ Plotting graphs.
Introducing Variables

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.

To assign a value to bbb, you can do it in


two ways: 1) Using the command window.
When you click on bbb, the variable
editor window appears. You can type
in new values into bbb by filling in the
cells.
An example is shown here.
Try and do it yourself.
To display variables at the console,
you can type the variable name,
or you can type disp(variable_name).
If you want to see help,
you can type help at the
command window.
Or you can press
F1 to display the
help window.
Click on
Open Help
Browser
to search for a
specific function.
Example: search for
function mean
Basic Commands
Operators (Element by Element)

.* element-by-element multiplication
./ element-by-element division
.^element-by-element power
“%” is the neglect sign for Matlab (equaivalent of “//” in C).
Anything after it on the same line is neglected by Matlab
compiler.
Sometimes slowing down the pause %wait until any key
execution is done deliberately pause(3) %wait 3 seconds
for observation purposes. You
can use the command “pause”
for this purpose
Strings
„ MATLAB also can accept and manipulate
string variables.
„ A string is defined by enclosing it in single
quotes.
„ Example: aString = ‘Hello World!’

Example: Initializing a String


Converting a String to Lower/ Upper case

„ To convert a string to
lowercase or uppercase, 1
use the following commands.
1. Change string in matrix A to
lowercase:
B = lower(A)
2. Change string in matrix A to
2
uppercase:
B = upper(A)
Concatenate Strings
„ Concatenating string means merging two or
more strings together.
„ To concatenate strings, use the strcat
command.
„ Example:

to concatenate
str1 and str2:
newStr = strcat(str1,str2)
Replace String
„ To replace part of the
string with a new
value, use the strrep
command.
„ Example: replace the
word ‘study’ with the
word ‘read’ in the
string str1.
… strrep(str1,’study’,’r
ead’)
Plot
MATLAB supports many types of graph
and surface plots:

„ line plots (x vs. y),


„ filled plots,
„ bar charts,
„ pie charts,
„ parametric plots,
„ polar plots,
„ contour plots,
„ density plots,
„ log axis plots,
„ surface plots,
„ parametric plots in 3 dimensions and spherical plots.
Plot Function
„ The plot function can be used to draw the
relationship between two variables.
„ Format: plot(x,y,lineParameters)

Example: Plot the values


of a random matrix
clear, close all
clc
xxx = 1:100
yyy = rand(1,100)
plot(xxx,yyy)
Example: Draw sin(x)

clear, close all


clc

x = 0:pi/36:10*pi
y = sin(x)

plot(x,y,‘m')
Plot Styles
Example: Plotting the lines
using various line parameters
clear, close all
clc
xxx = 1:100

yyy = rand(1,100)
plot(xxx,yyy)
figure, plot(xxx,yyy,'g:')
figure, plot(xxx,yyy,'r--')%
figure, plot(xxx,yyy,':mo')

% the command figure is used to create a


% new figure each time a plot is made.
Example: Drawing two plots in
the same figure

clear, 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
Subplots
Subplots
x=0:0.1:2*pi;

subplot(2,2,1);
plot(x, sin(x));

subplot(2,2,2);
plot(x, cos(x));

subplot(2,2,3)
plot(x, exp(-x));

subplot(2,2,4);
plot(peaks);
Drawing Different Types of Graphs
clear, close all
clc
x = 0:pi/36:2*pi
y = cos(x)
bar(x,y,'b')

clear, close all


clc
x = -10:0.5:10
y = x.^2 + 2.*x + 2
stairs(x,y,'b')
Display Facilities

„ Title
>>title(‘This is the sinus function’)

„ x axis label
>>xlabel(‘x (secs)’)

„ y axis label
>>ylabel(‘sin(x)’)
„ Legend
>> legend ('string1','string2',...)
Display Facilities
x = -pi: pi/20: pi;
plot(x,cos(x),'-ro',
x, sin(x),'-.b')
legend('cos_x','sin_x');

x = -pi:pi/20:pi;
>> plot(x,cos(x),'-
ro',x,sin(x),'-.b')
>> hold on
>> ylabel('cos_x, sin_x');
>> hold on
>> xlabel('pi');
>> title('Plot')
>> legend('cos_x', 'sin_x')
log-log Plot

x = 0:10^2;
>> y = exp(x);
>> loglog(x,y,'-s')
Semi log Plot

x = 0:1000;
y = log(x);
figure
semilogx (x,y)
3D Plot
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
>> surf(x,y,z)

[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure
mesh(Z)

You might also like