Sunteți pe pagina 1din 5

Graphing/ Plotting Instructions

Written Instructions

Visual Instructions

1. Open MATLAB so you are


looking at the main screen.
(Figure 10)

Figure 10:

For these instructions lets us


consider and example function
with a set of data (x,y)
y=x2
x: 1, 2, 3, 4, 5, 6, 7
y: 1, 4, 9, 16, 25, 36, 49
2. First we need to generate
values for our graph. There are
two ways to do this. One uses a
function and one uses a data set.
If you have not yet generated a
data set with your function, you
can do so on MATLAB.
To do this you must first create
your domain by typing it into
the command window.
(Figure 11)
Input: x= [1 2 3 4 5 6 7]
3. After you enter your domain
you can then enter your function
into command window to
generate your y-values. A
period must be entered before
the carrot symbol when entering
the exponent. (Figure 11)
Input: y=x.^2
Note: If you already have your
data set then both the x and y
values can be entered the same
way you entered your domain
(x-values) in the second step.

Figure 11:

4.Now that you have


generated your x and y values
you can plot this set of data
by typing the plotting
function into the command
window. (Figure 12)

Figure 12:

Input: plot (x, y)


This will prompt a smaller
window to open with the
graph of your data. This
window is called the
Graphics Window.
Right now the graph is very
plain which is why the
following set of steps will be
used to add various features
to it.
5. With the title command we
can name our graph.
(Figure 13)
Input: title (Insert title here)
Ill name this x vs. y.
6. Now we are going to use
the command to label the xaxis. (Figure 13)
Input: xlabel (title)
The name I chose was xvalues.
7. For the y-axis use the same
command but with a y
instead of an x. (Figure 13)
Input: ylabel (title)
The name I chose was yvalues.

Figure 13:

Now that we have a properly


labeled graph we can input
commands to change the
appearance of the graph.
8. To start we are going to add
symbols to the points on our
graph. To do this there are two
different input options.
Input 1: plot(x,y, insert letter).
This creates a graph with just
symbols at the points and no
line. (Figure 14)
Input 2: plot(x,y,x,y, insert
letter). This creates a graph
with symbols and a line. (Figure
15)
Where it says insert letter in
both potential inputs is where
you have the option to choose
the symbol type. Possible letters
to insert include:
o=circles
d=diamonds
s=squares

Figure 14:

Figure 15:

Input: plot(x,y,o)
Input: plot(x,y,x,y,s)
9. Along with adding symbols
to our graph we can also change
its color. To do this input:
plot(x,y,x,y,insert
symbol,color,insert color)
Where the command says insert
symbol place the letter for the
symbol of your choice.
Where the command says insert
color place the letter for the
color of your choice.
Options include:
r=red
y=yellow
b=blue
g=green
Input:
plot(x,y,x,y,o,color,g)
Your graph should now match
Figure 16.

Figure 16:

Note: While entering the


plotting commands is the
quickest and easiest way to plot
a set of data according to your
preferred appearance, there is
one other way you can edit the
appearance of a graph. Inputting
the command, plottools on,
opens a new window above the
workspace where you may edit
the graph. (Figure 17)

Figure 17:

The last important addition to a


basic MATLAB graph is a
linear regression.
10. Use the linear regression
command to find the slope and
y-intercept of the linear
regression.
Command: polyfit(x,y,n)
The n is the number to which
order the curve fits the x,y data.
Input: polyfit(x,y,1)
This will give you two numbers.
The first is the slope and the
second is the y-intercept.
(Figure 18)
11. Insert your original x-values
into the command window
again.
Input: x=[1 2 3 4 5 6 7]
12.Using the slope and intercept
of the regression you were given
write a slope-intercept form
equation. Insert this into the
command window to find your r
(regression) values. (Figure 18)
Input: r=8*x-12
13. The last step is to graph the
regression. For just the
regression use the input from
step 9 but use r instead of y. To
graph the regression against the
original curve input:
plot(x,y,-,x,r,-)
Refer to Next Page

Figure 18:

By using the input, plot(x,y,-,x,r,-), MATLAB plots the original curve as well as the new linear regression
line on the same graph. This is shown in the image below.

This is the end of the instructions on the basic calculating and graphing functions of MATLAB. To continue to
refine your MATLAB skills you can practice calculating/ graphing various functions and sets of data using the
commands that you have just learned. If you have any questions that these instructions have not answered input,
help, into the command bar and you will be given a list of help options for the program where you will be able
to find your answer.

S-ar putea să vă placă și