0% found this document useful (0 votes)
47 views15 pages

Matlab 6

To plot a function in MATLAB: 1. Define the x and y variables, specifying the range of x values. 2. Define the function as y = f(x). 3. Use the plot command plot(x,y) to generate the graph. Additional commands like title, xlabel, ylabel allow adding labels and scales to customize the plot. Multiple functions can be plotted on the same graph using different colors or styles. Bar charts and 3D surface plots can also be generated.

Uploaded by

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

Matlab 6

To plot a function in MATLAB: 1. Define the x and y variables, specifying the range of x values. 2. Define the function as y = f(x). 3. Use the plot command plot(x,y) to generate the graph. Additional commands like title, xlabel, ylabel allow adding labels and scales to customize the plot. Multiple functions can be plotted on the same graph using different colors or styles. Bar charts and 3D surface plots can also be generated.

Uploaded by

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

MATLAB

PLOTTING
To plot the graph of a function, you need to take
the following steps:
Define x, by specifying the range of values for the
variable x, for which the function is to be plotted
Define the function, y = f(x)
Call the plot command, as plot(x, y)
For example: Plot y= x
• x = [0:5:100];
• y = x;
• plot(x, y)
Adding Title, Labels, Grid Lines, and
Scaling on the Graph
• MATLAB allows you to add title, labels along the
x-axis and y-axis, grid lines and also to adjust the
axes to spruce up the graph.
• The xlabel and ylabel commands generate labels
along x-axis and y-axis.
• The title command allows you to put a title on
the graph.
• The grid on command allows you to put the grid
lines on the graph.
• The axis equal command allows generating the
plot with the same scale factors and the spaces
on both axes.
– title(‘this is the title’)

– xlabel(‘x’)

– ylabel(‘y’)
Example
• x = [0:0.01:10];
• y = sin(x);
• plot(x, y), xlabel('x'), ylabel('Sin(x)'),
title('Sin(x) Graph'),
• grid on, axis equal
Line Style Description

'-' Solid line

'--' Dashed line

':‘ Dotted line

'-.' Dash-dotted line


Drawing Multiple Functions on the
Same Graph
You can draw multiple graphs on the same plot. The
following example demonstrates the concept:
Example
Create a script file and type the following code:
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g), legend('Sin(x)', 'Cos(x)')
Plot
y1 = 2 cos(x), y2 = cos(x), and y3 =0.5cos(x), in
the interval [0,2pi]
Setting Colors on Graph
Code Color
w White
k Black
b Blue
r Red
c Cyan
g Green
m Magenta
y Yellow
Example
Let us draw the graph of two polynomials
f(x) = 3x^4 + 2x^3+ 7x^2 + 2x + 9 and
g(x) = 5x^3 + 9x + 2
Create a script file and type the following code:
x = [-10 : 0.01: 10];
y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9;
g = 5 * x.^3 + 9 * x + 2;
plot(x, y, 'r', x, g, 'g')
Setting Axis Scales
The axis command allows you to set the axis scales.
You can provide minimum and maximum values for
x and y axes using the axis command in the
following way:
axis ( [xmin xmax ymin ymax] )
Example
Create a script file and type the following code:
x = [0 : 0.01: 10];
y = exp(-x).* sin(2*x + 3);
plot(x, y), axis([0 10 -1 1])
Drawing Bar Charts
The bar command draws a two dimensional bar
chart. Let us take up an example to demonstrate
the idea.
Example
Let us have an imaginary classroom with 10
students. We know the percent of marks
obtained by these students are 75, 58, 90, 87,
50, 85, 92, 75, 60 and 95. We will draw the bar
chart for this data.
x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
bar(x,y), xlabel('Student'),ylabel('Score'),
title(‘Third Sem:')
Three-Dimensional Plots
To define g, we first create a set of (x,y) points over the
domain of the function using the meshgrid command. Next,
we assign the function itself. Finally, we use the surf command
to create a surface plot.
The following example demonstrates the concept:
Example
Let us create a 3D surface map for the function
−(𝑥 2 +𝑦 2 )
g = 𝑥𝑒
Create a script file and type the following code:
[x,y] = meshgrid(-2:.2:2);
g = x .* exp(-x.^2 - y.^2);
surf(x, y, g)

You might also like