You are on page 1of 21

Imam Ja'afar Al-Sadiq University

Kirkuk
Computer Technical Engineering
Second Class

Computer Applications: MATLAB


Dr. Yousif A. Hamad
- M-Files Scripts
- 2D & 3D Plotting
Lec. 8
Example We begin by entering and running the code:

To proceed we type the above code into the editor and then use the
File Menu (sub item Save As) to change the name of the code and
Save it as (twonums.m). We will need to erase the current default
name (Untitled.m) and type the new filename.The contents of the
file can be displayed by typing type twonums.

Press to open new


script

4
Initial script name Untitled

5
M-File functions
•Functions are programs (or routines) that accept input arguments
and return output arguments.
•Function m- file must start with the word function, followed by
the output variable(s), an equal's sign, the name of the function, and
the input variable(s).
•Functions do not have to have input or output statements. If there
are more than one input or output argument, they must be separated
by commas. If there are one or more input arguments, they must be
enclosed in brackets, and if there are two or more output
arguments, they must be enclosed in square brackets.
•Functions names must follow the same rules as variable names. If
the file name and the function name are different, MATLAB uses
the file name and ignores the function name. You should use the
same name for both the function and the file to avoid confusion.

6
Example Suppose we want to plot contours of a function of two
variables z = x2+ y2. We can use the code:

Which should be saved in the file func.m. The first line indicates
this is a function which has two inputs x and y, and returns a single
output. Then second line calculates the function x2 + y2.
Again we have used dot arithmetic to allow for the possibility of
vector or matrix arguments. For the calculation to be valid the
vectors x and y must have the same size.
>> x= linspace(1,5,10);
>> y=linspace(2,7,10);
>> z=func(x,y);

7
•The difference between scripts and function can be summarized
with the following table:
SCRIPTS FUNCTIONS

-Do not accept input arguments or -Can accept input arguments or


return output arguments. return output arguments.
-Store variables in a workspace that -Store variables in a workspace
is shared with other scripts. internal to the function.
-Are useful for automating a series -Are useful for extending the
of commands. MATLAB language for your
application.

8
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.
Creating Simple 2D Plots
The basic MATLAB graphing procedure, for example in 2D, is to
take a vector of x- coordinates, x = (x1,….., xN), and a vector of y-
coordinates, y = (y1,…..,yN), locate the points (xi, yi), with i = 1,
2,…..,n and then join them by straight lines. You need to prepare x
and y in an identical array form; namely, x and y are both row arrays
and column arrays of the same length.

The MATLAB command to plot a graph is plot(x , y).

1
Examples:

>> n=5;
>> x=0:1/n:3;
>> y= sin(5*x);
>> plot (x,y)

>> n=25;
>> x= 0:1/n:3; >> y= sin(5*x);
>> plot (x,y)

2
Adding Titles, Axis Labels, Grid , Line Style and Annotations
MATLAB enables you to add axis labels, grid and titles.
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y), xlabel ('x = 0:2\pi') ,ylabel ('Sine of x') ,title ('Plot of
the Sine function') , grid % Remove grid by calling grid off

3
•The color of a single curve is, by default, blue, but other colors are
possible. The line styles, colors, and markers (e.g., circles, plus
signs,. ) can be specified using the plot command:
>> plot (x,y,'style_color_marker')
Another colors and line styles can be used , see Table below:

4
Example Write a program to plot cos(2x) with the range of x
from 1 to 10 in step of 0.1 with dotted line style and circle mark.
>>x=1:0.1:10;
>> y= cos(2*x);
>>plot(x,y,': o '), xlabel('x'), ylabel('cos(2x)'),title('cos function')

5
Multiple Data Sets in One Plot
Multiple (x,y) pairs arguments create multiple graphs with a single
call to plot.
Example:
Write a program to plot a three function of x (in one plot) , where
y1= 2 cos(x) , y2 = cos(x) , and y3 = 0.5 cos(x) in the interval
of 0≤ x ≤ 2π in step of π / 100, then add labels and titles to the
plot.
>> 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('Cosinefunctions'),legend('2*cos(x)','cos(x)','0.5*cos(x)'),title
('Typical example of multiple plots') , axis([0 2*pi -3 3])
6
7
Multiple Data Sets in Multiple Plots
It is also possible to produce a few subplots in on figure window.
With the command subplot, the window can be horizontally and
vertically divided into p×r subfigures, which are counted from 1 to
p×r, row-wise, starting from the top left. The commands: plot, title,
grid etc. work only in
the current subfigure.
>> subplot (m , n , p )

8
Example:
Write a program to Plot a three function of x (in a separate
plot) , where y1= sin(3x) , y2 = cos (5x ) ,y3 = sin (3x ) * cos (5x)
in the interval of x from 1 to 4 in step of 0.1 then add titles to
the plot.

>> x = 1:.1:4;
>> y1= sin(3*x);
>> y2= cos(5*x);
>> y3= sin (3*x).*cos(5*x);
>> subplot(1,3,1);plot(x,y1,'m-');title
('sin(3*x)'),subplot(1,3,2);plot(x,y2,'g');title('cos(5*x)'),subplot(1,3,3
);plot(x,y3,'k-'); title('sin(3*x)*cos(5*x)')

9
10
Three – Dimentional Graphics
Basic Plots
A MATLAB surface is defined by the z coordinates associated with
a set of (x,y) coordinates . for example , suppose we have the set of
(x, y) coordinates:

11
Which is the distance of each (x,y) point from the origin (0,0) . to
calculate z in MATLAB for the x and y matrices given above, we
begin by using the mesh grid function, which generates the required
, x and y matrices.
>> [x,y] = meshgrid (1:4)
Example Write a program to draw a 3-D plot of sinc function
for x,y in the interval from -8 to 8 in step of 0.5.

>> [x,y] = meshgrid (-8:.5:8);


>> R= sqrt (x.^2 + y.^2 ) + eps;
>> z = sin (R). / R;
>> mesh (x , y , z )
>> Z = sin (R)./R

12
Example Plot the function

>> [x,y] = meshgrid(–2:0.1:2);


>> z = y.*exp(–x.^2–y.^2);
>> mesh(x,y,z),xlabel('x'),ylabel('y'),zlabel('z')

13
Example

>> t= 0:pi/10:2*pi;
>>[X,Y,Z] = cylinder(1+cos(t));
>>surf(X,Y,Z)

14

You might also like