Uploaded by Yusuf Batuhan

3 1 Graphs

advertisement
Adana Alparslan Türkeş Science and Technology University
Introduction to Programming
(with MATLAB)
Graphs
Dr. Onur GÜVEN
Built-in Array Functions
 mean(A)
Returns the mean value of the elements of A.
 max(A)
Returns the largest element in A.
 min(A)
Returns the smallest element in A.
 sum(A)
Returns the sum of elements of A.
 sort(A)
Arranges the elements in ascending order.
 median(A)
Returns the median value of the elements.
 std(A)
Returns the standard deviation.
Example;
>>grades=[10 25 45 50 64 54 56 79 90 23 78 87 65 43 23 45 78 90 66];
>>length(grades)
>>mean(grades)
>>sort(grades)
>>sum(grades)
>>std(grades)
>>median(grades)
Generation of Random Numbers
 rand
Generates a single random number between 0 and 1.
 rand(1,n)
n element random numbers between 0 and 1.
 rand(n)
n x n matrix random numbers between 0 and 1.
 rand(m,n)
m x n matrix random numbers between 0 and1.
Generation of Trigonometric Functions
 sin(A)
Returns the sine of the elements of A in radians.
 sind(A)
Returns the largest element in A in degrees.
 asin(A)
Returns the arcsine (inverse sine) of the element of A in radians.
 asind(A)
Returns the arcsine of the elements of A in degrees.
 cos(A)
Returns the cosine of the elements of A.
 tan(A)
Returns the tangent of the elements of A.
 cot(A)
Returns the cotangent of the elements of A.
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Line Styles, Markers and Colors
plot(x, y, ‘Line Specifiers’, ‘Property Name’, Property Value)
vectors
Optional: Type and
color of the line
markers
Optional: Type of properties
such as line width,
marker’s size, edge, and fill
colors
Optional: Value of properties such
as line width,
marker’s size, edge, and fill colors
Basic 2-D Graph Functions
plot(x, y, ‘Line Specifiers’, ‘Property Name’, Property Value)
Line Specifiers
Line Style
Line Color
Sp.
Line color
Sp.
Sp.
solid (Default)
-
red
r
magenta
m
Dashed
--
green
g
Yellow
y
Dotted
:
blue
b
Black
k
Dash-dot
-.
cyan
c
white
w
Marker
Sp.
marker
Sp.
marker
Sp.
Plus
+
cross
x
diomand
d
circle
o
triangle
^
Five-pointed-star
p
asterisk
*
triangle
v
six-pointed-star
h
point
c
square
s
Triangle
>Or<
Example;
>>plot(x, y, ’om—’)
Basic 2-D Graph Functions
plot(x, y, ‘Line Specifiers’, ‘Property Name’, Property Value)
Property Name and Property Value
Property
Property Code
Value
Line Width
LineWidth
0.5
Marker Size
MarkerSize
#of points
Marker Edge Color
MarkerEdgeColor
Marker Face Color
MarkerFaceColor
Example;
>>plot(x, y, 'om--', 'LineWidth', 2, 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'g', 'MarkerSize', 10)
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Basic 2-D Graph Functions
Multiple Plots on the Same Axes
 The easiest way is simply to use
«hold» to keep the current plot on
the axes (either with «hold off» or
«hold»).
 Plot
with
multiple
plot(x,y,x,z,.....);
arguments:
plots the(vector)
pairs (x,y) and (x,z) etc. MATLAB
automatically selects a different
color for each pair.
Multiple Plots on the Same Axes
 You can obtain a multiple plots by
drawing a graph as matrix vs vector. If
x is a vector and y is a matrix, the rows
of the matrix are plotted against the
vector, using a different color.
 If you are plotting two graphs on the
same axes (x) you may find plotyy
useful. It allows you to have
independent y-axis labels on the left
and the right.
Multiple Plots on the Same Axes
Example-1:
Draw a multiple plot with the properties listed below by using plot function.

Create an x vector with
elements from 0 to 2 with 0.2
increments.

Create a y vector as a function
of x square and an z vector as
a function of square root of x.

While the line color, line style
and marker style of y vs x plot
should be blue, dashed and
circle, respectively. The same
properties for z vs x plot
should be red, dotted and
triangle.
Multiple Plots on the Same Axes
Example-2:
Draw a multiple plot with two different y axes
x= 𝟏, 𝟏. 𝟏, 𝟏. 𝟐, 𝟏. 𝟑, … , 𝟐. 𝟗, 𝟑. 𝟎
on the same x axes by using plotyy function
y=𝒙𝟐
and the equations shown on the right:
z=𝒙𝟏.𝟐
The "fplot" Command
fplot(‘function’, [limits], ‘Line Specifiers’)
For example:
x^2+4*sin(2*x)-1
Limits for x and y axes
can be specified as
[Xmin Xmax Ymin Ymax]
Optional: Type and
color of the line
markers
Example:
Draw y vs x graph with the
equation shown below by
using fplot function as;
𝒚 = 𝒙𝟐 + 𝟒 𝐬𝐢𝐧 𝟐𝒙 − 𝟏
1.
The limits of x axis
should be from -3 to 3.
2.
Additionally, the limits of
y axis should be from -2
to 6.
3.
Moreover, the line color,
line style and marker style
of graph should be red,
dotted
and
respectively.
circle,
Plots with Logarithmic Axes
 semilogy(x,y) Plot y vs x with a
log scale for the y axis and linear
scale for the x axis.
 semilogx(x,y) Plot y vs x with a
log scale for the x axis and linear
scale for the y axis.
 loglog(x,y)
Plot y vs x with a
log scale for both axes.
Example;
>>plot(x, y)
>>semilogx(x, y)
>>semilogy(x, y) >>loglog(x, y)
x= 𝟎: 𝟎. 𝟏: 𝟓
y=𝟐𝒙(𝟑𝒙+𝟓)
Plots with Error Bars
 Experimental data that is measured and then displayed in plots frequently
contains error and scatter.
Plots with Special Graphics
Function
Graphics
bar(x,y)
Vertical Bar Plot
barh(x,y)
Horizontal Bar Plot
stairs(x,y)
Stairs Plot
pie
Pie Plot
hist(x,y)
Histogram
Other Commands for Plotting
 title(‘text’)
Adds text at the top of the current axis.
 xlabel(‘text’) Adds text beside the X-axis on the current axis.
 ylabel(‘text’) Adds text beside the Y-axis on the current axis.
 zlabel(‘text’) Adds text above the Z-axis on the current axis.
 text(x,y,’text’) Writes text in the graphics window.
 gtext(‘text’) Places text with mouse in the graphics window.
 grid, grid on, grid off Adds/removes grid lines to/from the current graph.
 axis([Xmin Xmax Ymin Ymax]) Sets the scaling on the current plot.
Download