Matlab Lec 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

MATLAB

LEC 3
Basic plotting
• MATLAB has an excellent set of graphic tools. Plotting a given data
set or the results of computation is possible with very few commands.
• The MATLAB command to plot a graph is plot(x,y). The vectors x =
(1; 2; 3; 4; 5; 6) and y = (3;¡1; 2; 4; 5; 1) produce the picture shown in
Figure in the next slide
• >> x = [1 2 3 4 5 6];
• >> y = [3 -1 2 4 5 1];
• >> plot(x,y)
• If we specify two vectors, as mentioned above, plot(x,y) produces a
graph of y versus x. For example, to plot the function sin (x) on the
interval [0; 2π], we first create a vector of x values ranging from 0 to 2π,
then compute the sine of these values, and finally plot the result:
• >> x = 0:π/100:2*π;
• >> y = sin(x);
• >> plot(x,y)
Adding titles, axis labels, and annotations
• MATLAB enables you to add axis labels and titles. For example, using
the graph from the previous example, add an x- and y-axis labels.
• >> xlabel('x = 0:2\pi')
• >> ylabel('Sine of x')
• >> title('Plot of the Sine function’)
Multiple data sets in one plot
Multiple (x, y) pairs arguments create multiple graphs with a single call
to plot. For example, these statements plot three related functions of x:
y1 = 2 cos(x), y2 = cos(x), and y3 = 0.5 × cos(x), in the interval
0 ≤ x ≤ 2π.
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,'--',x,y2,’*',x,y3,’+')
>> xlabel('0 \leq x \leq 2\pi')
>> ylabel('Cosine functions')
>> legend('2*cos(x)','cos(x)','0.5*cos(x)’)
>> title('Typical example of multiple plots')
>> axis([0 2*pi -3 3])
Pie chart
• Pie charts are useful for visualizing the relative sizes of different but
related quantities. For example, the table below shows the grades that were
assigned to a class. The data is used to create the pie chart that follows

Grade A B C D F
Number of 10 19 12 5 2
students

• grd=[10 19 12 5 2];
• pie(grd)
• title('Class Grades')
histogram
y=[58 73 73 53 50 48 56 73 73 66 69 63 74 82 84 91 93 89
91 80 59 69 56 64 63 66 64 74 63 69];
>> hist(y)
Specifying line styles and colors
• It is possible to specify line styles, colors, and markers (e.g., circles,
plus signs, . . . ) using the plot command:
• plot(x,y,'style_color_marker')
• Another nice feature that can be used in conjunction with plot is the
command grid, which places grid lines to the current axis (just like you
have on graphing paper).
• Other commands for data visualization that exist in MATLAB include
• meshgrid Cartesian grid in 2-D/3-D space
[X,Y] = meshgrid(xgv,ygv) replicates the grid vectors xgv and ygv to
produce the coordinates of a rectangular grid (X, Y). The grid vector
xgv is replicated numel(ygv) times to form the columns of X. The grid
vector ygv is replicated numel(xgv) times to form the rows of Y.
[X,Y,Z] = meshgrid(xgv,ygv,zgv) replicates the grid vectors xgv, ygv, zgv
to produce the coordinates of a 3D rectangular grid (X, Y, Z). The grid
vectors xgv,ygv,zgv form the columns of X, rows of Y, and pages of Z
respectively. (X,Y,Z) are of size numel(ygv)-by-numel(xgv)
by(numel(zgv).
• x=-3:0.25:3;
• y=-3:0.25:3;
• [X,Y]=meshgrid(x,y);
• Z=1.8.^(-1.5*sqrt(X.^2+Y.^2)).*cos(0.5*Y).*sin(X);
• surfl(X,Y,Z)
• xlabel('x'); ylabel('y')
• zlabel('z')
Introduction to programming in
MATLAB
• So far in these lab session, all the commands were executed in the
Command Window. The problem is that the commands entered in the
Command Window cannot be saved and executed again for several
times. Therefore, a different way of executing repeatedly commands
with MATLAB is:
• 1. to create a file with a list of commands,
• 2. save the file, and
• 3. run the file.
• If needed, corrections or changes can be made to the commands in the
file. The files that are used for this purpose are called script files or
scripts for short.
• This section covers the following topics:
• M-File Scripts
M-File Scripts
• A script file is an external file that contains a sequence of MATLAB
statements. Script files have a file name extension .m and are often
called M-files. M-files can be scripts that simply execute a series of
MATLAB statements, or they can be functions that can accept
arguments and can produce one or more outputs.
• A script file is a sequence of MATLAB commands, also called a
program.
• When a script file runs (is executed), MATLAB executes the
commands in the order they are written just as if they were typed in the
Command Window.
Using a script file is convenient because it can be edited (corrected or
otherwise changed) and executed many times.
Script files can be typed and edited in any text editor and then pasted
into the MATLAB editor.
Example 1
Consider the system of equations:
x + 2y + 3z = 1
3x + 3y + 4z = 1
2x + 3y + 3z = 2
Find the solution x to the system of equations
solution
• Use the MATLAB editor to create a file: Home New script.
• Enter the following statements in the file:
A = [1 2 3; 3 3 4; 2 3 3];
b = [1; 1; 2];
x = A\b
• Save the file, for example, example1.m.
• Run the file, in the command line, by typing:
• >> example1
x=
-0.5000
1.5000
-0.5000
• Example 2
• Plot the following cosine functions, y1 = 2 cos(x), y2 = cos(x), and
y3 = 0.5 × cos(x), in the interval 0 ≤ x ≤ 2π.
Here we put
• the commands in a file.
• Create a file, say example2.m, which contains the following
commands:
• x = 0:pi/100:2*pi;
• y1 = 2*cos(x);
• y2 = cos(x);
• y3 = 0.5*cos(x);
• plot(x,y1,'--',x,y2,'-',x,y3,':')
• xlabel('0 \leq x \leq 2\pi')
• ylabel('Cosine functions')
• legend('2*cos(x)','cos(x)','0.5*cos(x)')
• title('Typical example of multiple plots')
• axis([0 2*pi -3 3])
• %script file that creates a plot of
• % the function: 3.5.^(-0.5*x).*cos(6x)
• x=[-2:0.01:4];
• y=3.5.^(-0.5*x).*cos(6*x);
• plot(x,y)
• Plot the function , y = 3x3 – 26x + 10 and its first and second
derivatives, for, all in the same plot.
Solution
• The first derivative of the function is: y' = 9x2 – 26
• The second derivative of the function is: 18x
• A script file that creates a vector x and calculates the values of y, ,y’
and y’’is
x=[-2:0.01:4];
y=3*x.^3-26*x+6;
yd=9*x.^2-26;
ydd=18*x;
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
Output commands
• As discussed before, MATLAB automatically generates a display when
commands are executed. In addition to this automatic display, MATLAB
has several commands that can be used to generate displays or outputs.
• Two commands that are frequently used to generate output are:
• disp and fprintf.
example finding friction factor
• % friction factor
• % done by kebede on 16 Feb 2019
• D = input (‘Dia in meter= ‘ );
• V=input (‘velocity in m/s = ‘);
• Rho=100; % density of water in kg/m3
• Mu=0.001;% viscosity of water in kg/m.s
• Re=D*V*Rho/mu;
• f=0.079*Re^(-0.25);
• disp(f)
student grade
a=input('enter the student marks');
b=input('enter maximum mark');
c=a*100/b
if(c>=84.5)
disp('A');
elseif((c>=80) && (c<84.5))
disp('B+');
elseif((c>=75) && (c<80))
disp('B');
• elseif((c>=70) && (c<75))
• disp('C+');
• elseif((c>=55) && (c<70))
• disp('C');
• elseif((c>=45) && (c<55))
• disp('D');
• else
• disp('F');
• end

You might also like