0% found this document useful (0 votes)
10 views26 pages

2D Graphythgxgnk

The document outlines various built-in array functions in MATLAB, such as mean, max, min, and operations for generating random numbers and plotting. It covers expressions, statements, and commands, as well as high-level 2-D plotting facilities, including customization options for line styles and markers. Additionally, it discusses multiple plots, logarithmic axes, and adding titles and labels to graphs.

Uploaded by

Grenlighttt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

2D Graphythgxgnk

The document outlines various built-in array functions in MATLAB, such as mean, max, min, and operations for generating random numbers and plotting. It covers expressions, statements, and commands, as well as high-level 2-D plotting facilities, including customization options for line styles and markers. Additionally, it discusses multiple plots, logarithmic axes, and adding titles and labels to graphs.

Uploaded by

Grenlighttt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

 Additional Array Functions.

 Expressions, Statements, Commands,


Functions.

 High-level 2-D plotting facilities.


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
std(A) : returns the standart deviation
det(A) : Returns the determinant of a square matrix A
dot(a,b) : Calculates the scalar product of two vectors
cross(a,b) : Calculates the cross products of a and b
inv(A) : Returns the inverse of A
diary(‘The name of the text file’) : Starts to save the following
steps
diary off :Finishes the saving
 Create a vector include 30 grades
 Sort the vector using related function
 Calculate the mean and the standart deviation of the
grades
 Save your calculation steps ?????
Expressions-Statements-Commands-Functions
Expressions
An expression is a formula consisting of variables, numbe
operators, and function names. Such as 2π as follows: 2 *
MATLAB’s response is: ans = 6.2832
Left side

Statements
Statements are frequently of the form: variable = express

EXAMPLE: s=u*t-g/2*t.ˆ2; assignment statement


A common mistake ; a+b=c

A statement which is too long to fit onto one line may be


continued to the next line with an ellipsis of at least three
dots
x = 3 * 4 - 8 ...
/ 2 ˆ 2;
High-level 2-D
plotting facilities
GENERATION OF RANDOM NUMBERS
Rand: generates uniformly distributed numbers with values
between 0 and 1
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): mxn matrix random numbers between 0 and1

plot(x) : To plot data series

Application: Create 20 random numbers


and plot the graph using these numbers
Basic 2-D graphs : graphs (in 2-D)
are drawn with the plot statement.
In its simplest form: plot(y).
(plots y vs indexes)

Example : plot(rand(1, 20))


Example :
y=0:0.1:1;
y=[y' y.^2']

If y is a matrix, its columns


are plotted against element
The most common form: plot(x, y)

Example :
x = 0:pi/40:4*pi;
plot(x, sin(x))
Line styles, markers and color
Line styles, markers and colors may be selected for a
graph with a string argument to plot, e.g. plot(x, y, ’--’)
joins the plotted points with dashed lines.
Plot(x, y, ‘line specifiers’ , ‘PropertyName’, PropertyValue)

vectors
Optional: Type and color Optional properties : line
of the line markes width Marker’s size and edge,
fill colors

plot(x,y,’o’):Draws circles at the data points with no


lines joining them.

You can specify all three properties:


plot(x,sin(x), x, cos(x), ’om--’) : o, magenta, dashed
Line styles, markers and color
Plot(x, y, ‘line specifiers’ , ‘PropertyName’, PropertyValue

Line Specifiers Line Specifiers


Line Specifiers Line Specifier Line Specifie
Style Color s color rs
solid -
(Default red r magen m
) ta
Dashed -- green g Yellow y
Dotted : blue b Black k

Dash- -. cyan c white w


Line Specifiers
dot
Marke Specifie marker Specifiers marker Specifier
r rs s
Plus + cross x diomand d
circle o triangl ^ Five-pointed- p
e star

asteri * triangl v six-pointed- h


sk e star

point c square s Triangle >Or<


Line styles, markers and color
Plot(x, y, ‘line specifiers’ , ‘PropertyName’,
PropertyValue)
Property Name and Property Values
Prop. Name Val
linewidth 0.5
markersize #of points
markeredgecolor
markerfacecolor

Plot(x, y,‘- - mo’, ‘linewidth’,2, ‘markersize’,12, ’markeredgecolor’,


‘g’, ‘markerfacecolor’, ‘y’)

EXAMPLE : Plot a graph include


x-
sinx (circle, green dashed)
x-cosx(circle magenta, dashed)
Straight-line graphs are drawn by giving the
x and y coordinates:

general Form : plot([xin xfin], [yin yfin])

Example :
if coordinates (0, 1) & (4, 3)

plot([0 4], [1 3])


The fplot COMMAND
fPlot(‘function‘, limits, ‘line specifiers’)

Example : fplot(‘x^2+4*sin(2*x)-1‘, [-3


3] )
MULTIPLE PLOTS ON THE SAME AXES : HOLD COMMAND

use hold to keep the current plot on the


axes.
( with hold off)
MULTIPLE PLOTS ON THE SAME AXES: WITH SAME PLOT
FUNCTION

plot with multiple arguments:


plot(x,y, x,z,.....);
plots the(vector) pairs (x,y) and
(x,z)

(MATLAB automatically selects a


MULTIPLE PLOTS ON THE SAME AXES : WITH IN SAME MATRIX

plot(x, y) : If one of x or y is a
matrix and the other is a vector,
the rows or columns of the matrix
are plotted against the vector,
using a different color.

EXAMPLE : x= 0:0.1:1
y =[x.^2;
x.^3; x.^4]
y=y'
plot(x,y)
MULTIPLE PLOTS ON THE DIFFERENT plotyy()
AXIS :

If you are plotting two graphs


on the same axes you may find
plotyy useful—it allows you to
have independent y-axis labels
on the left and the right
Example : plotyy(x,sin(x), x,
10*cos(x))
EXAMPLES
x=1:0.1:3;
y=x.^2;
z = x.^1.2
plot(x,y,x,z)

x=1:0.1:3
y=x.^2
z =10000*x.^1.2;
plotyy(x,y,x,z)
PLOTS WITH LOGARITHMIC AXIS
• 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 : x=linspace(0.1, 60, 1000)
y=2.^(-0.2*x+10);
semilogx(x,y)
OR
loglog(x,y)
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
Tittle, Label’s, Grid, text
•If x and y are vectors, the text is written at each point. If the
text is an indexed list, successive points are labeled with
corresponding rows of the text.

title(’text’) : writes the text as a title on top of the graph.


xlabel(’horizontal’) : labels the x-axis.
ylabel(’vertical’) : labels the y-axis.

•Graphs may be labeled with : gtext(’text’) in desired location


•gtext puts a cross-hair in the graph window

The cross-hair can be positioned with the mouse or the arrow


keys. or on a graph interactively with Tools -> Edit

Grid : adds&removes grid lines to/from the current graph.


•text(x, y, ’text’) writes text in the graphics window.
“ˆ “ can be used to produce exponents in the title.
“_ “ can be used to produce exponents in the title.

Greek letter can be used precede its name


with a backslash

EXAMPLE : title('The graph of x vs \theta=x^2')

You might also like