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

MATLAB Array Functions and Plotting Guide

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

MATLAB Array Functions and Plotting Guide

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

 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')

Common questions

Powered by AI

Using multiple plot functions on the same axes allows for the simultaneous display of different data sets, enhancing comparison and data interpretation. This can be achieved with commands like plot(x,y, x,z) which plots (x,y) and (x,z) using different colors Aesthetic customization with line styles (e.g., dashed '--', dotted ':') and colors (e.g., red 'r', blue 'b') further enhances the clarity and differentiation. Such customization aids in identifying patterns and trends by visually distinguishing data groups .

Primary functions for array manipulation include mean(A) for calculating the mean value, max(A) for finding the maximum element, min(A) for the minimum element, sum(A) for the total sum, sort(A) for ordering elements, std(A) for standard deviation, det(A) for determinant of a matrix, dot(a,b) for scalar product of vectors, cross(a,b) for vector cross product, and inv(A) for matrix inversion. These functions can be effectively used in data analysis to perform basic statistical analysis, matrix operations, and vector calculations, which are essential in understanding data distributions, relationships, and transformations .

Vector operations, particularly dot (scalar) and cross products, are foundational in physics and engineering. The dot product aids in calculating projection and work, while the cross product is crucial for torque calculation and understanding rotational dynamics. These operations enable the precise modeling of forces and interactions in mechanical and electromagnetic systems, underscoring their importance in practical applications .

Log scale plots, such as semilogy for log y-axis, semilogx for log x-axis, and loglog for both axes, are useful for visualizing data with wide-ranging values. These plots transform exponential data into linear, making trends and patterns more apparent. This functionality is critical for analyzing growth patterns, frequency distributions, or any scenario where data spans several orders of magnitude .

The gtext() function allows interactive addition of text to plots by positioning it with the mouse, while the 'grid' feature toggles grid lines for better data alignment and readability. These tools facilitate interactive data exploration, making plots more informative and engaging by allowing users to annotate points of interest and enhance visual structure .

Matrix operations such as determinant (det(A)) and inversion (inv(A)) play key roles in solving linear algebra problems, like systems of equations. The determinant helps identify singular matrices, while inversion aids in finding solutions via matrix equations. These operations are integral in fields such as physics and engineering, where they facilitate modeling and analysis of linear systems .

Markers and line specifiers, such as '+', 'o', '- -', and ':', allow detailed customization of plots in Matlab, making data values distinct and enhancing visual interpretation. Different styles and colors help differentiate data series and highlight trends or specific data points, essential for effective data comparison and presentation in research and presentations .

Expressions in programming, such as those in Matlab, use variables, numbers, operators, and function names to perform calculations, e.g., 2π as 2*pi. Statements typically involve assigning the outcome of an expression to a variable, e.g., s=u*t-g/2*t.^2. Complex calculations require proper syntax and often multi-line continuation using ellipses (...) for legibility. These tools allow for concise, efficient coding of mathematical formulas pivotal in simulations and calculations within programming environments .

Plot annotations, including titles, labels, and text, enhance data understanding by providing context and highlighting key data points. The 'title' function adds a graph's title, 'xlabel' and 'ylabel' label axes, and 'text' or 'gtext' add annotations at specific points. Effective annotation ensures readers can intuitively grasp the data's story, making it crucial for successful data presentation and communication .

Random numbers can be generated using functions like rand to create uniformly distributed numbers. To visualize these numbers, they can be plotted using tools such as plot(x) for simple 2D graphs. For example, using rand(1,20) generates 20 random numbers, which can be visualized on a graph with the 'plot' function, providing insights into their distribution and randomness .

You might also like