Javascript required
Skip to content Skip to sidebar Skip to footer

Function to Draw a Circle in Matlab

MATLAB - Plotting


To plot the graph of a function, y'all 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)

  • Phone call the plot command, every bit plot(x, y)

Post-obit case would demonstrate the concept. Let us plot the simple role y = x for the range of values for x from 0 to 100, with an increment of 5.

Create a script file and type the following code −

x = [0:5:100]; y = 10; plot(ten, y)        

When yous run the file, MATLAB displays the following plot −

Plotting y = x

Let u.s.a. take one more example to plot the part y = ten2. In this example, nosotros volition depict 2 graphs with the same function, simply in second time, nosotros volition reduce the value of increment. Please note that equally we decrease the increase, the graph becomes smoother.

Create a script file and blazon the following code −

ten = [1 2 three 4 v half-dozen 7 8 9 ten]; x = [-100:twenty:100]; y = x.^two; plot(10, y)        

When you run the file, MATLAB displays the following plot −

Plotting y = x^2

Change the code file a niggling, reduce the increase to 5 −

x = [-100:five:100]; y = x.^two; plot(10, y)        

MATLAB draws a smoother graph −

Plotting y = x^2 with less increment

Calculation Title, Labels, Grid Lines and Scaling on the Graph

MATLAB allows you to add together championship, labels along the x-axis and y-axis, grid lines and also to conform the axes to bandbox upwardly the graph.

  • The xlabel and ylabel commands generate labels along ten-centrality and y-centrality.

  • The title control allows you to put a title on the graph.

  • The grid on command allows yous to put the grid lines on the graph.

  • The axis equal command allows generating the plot with the aforementioned scale factors and the spaces on both axes.

  • The centrality foursquare command generates a square plot.

Example

Create a script file and type the post-obit code −

x = [0:0.01:10]; y = sin(x); plot(x, y), xlabel('10'), ylabel('Sin(x)'), title('Sin(x) Graph'), grid on, centrality equal        

MATLAB generates the following graph −

Sprucing up our graphs

Drawing Multiple Functions on the Aforementioned Graph

You tin can draw multiple graphs on the same plot. The following example demonstrates the concept −

Instance

Create a script file and blazon the post-obit code −

x = [0 : 0.01: 10]; y = sin(x); k = cos(10); plot(x, y, 10, g, '.-'), legend('Sin(x)', 'Cos(ten)')        

MATLAB generates the following graph −

Multiple Functions on the Same Graph

Setting Colors on Graph

MATLAB provides eight basic color options for drawing graphs. The post-obit tabular array shows the colors and their codes −

Lawmaking Color
westward White
k Blackness
b Blue
r Cerise
c Cyan
g Dark-green
k Magenta
y Yellow

Example

Let us draw the graph of ii polynomials

  • f(x) = 3xfour + 2xthree+ 7xtwo + 2x + 9 and

  • g(x) = 5x3 + 9x + 2

Create a script file and blazon the post-obit lawmaking −

x = [-x : 0.01: ten]; y = 3*x.^4 + ii * x.^3 + 7 * 10.^2 + 2 * x + 9; thousand = 5 * ten.^3 + 9 * x + 2; plot(10, y, 'r', 10, g, 'g')        

When you run the file, MATLAB generates the following graph −

Colors on Graph

Setting Axis Scales

The axis control allows y'all to fix the centrality scales. You can provide minimum and maximum values for x and y axes using the axis command in the post-obit way −

axis ( [xmin xmax ymin ymax] )        

The post-obit example shows this −

Instance

Create a script file and type the following code −

10 = [0 : 0.01: ten]; y = exp(-x).* sin(ii*x + 3); plot(x, y), axis([0 10 -1 1])        

When you run the file, MATLAB generates the following graph −

Setting Axis Scales

Generating Sub-Plots

When yous create an assortment of plots in the aforementioned figure, each of these plots is chosen a subplot. The subplot command is used for creating subplots.

Syntax for the control is −

subplot(yard, n, p)        

where, chiliad and due north are the number of rows and columns of the plot assortment and p specifies where to put a particular plot.

Each plot created with the subplot command can have its own characteristics. Following case demonstrates the concept −

Example

Let united states generate two plots −

y = e−ane.5xsin(10x)

y = eastward−2xsin(10x)

Create a script file and type the post-obit code −

x = [0:0.01:v]; y = exp(-1.5*x).*sin(ten*x); subplot(ane,2,one) plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -one ane]) y = exp(-ii*x).*sin(ten*10); subplot(ane,2,2) plot(10,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),centrality([0 five -one 1])        

When you lot run the file, MATLAB generates the following graph −

Generating Sub-Plots

Useful Video Courses


Data Preprocessing for Machine Learning using MATLAB

Video

Complete MATLAB Tutorial: Go from Beginner to Expert

Video

Image Processing Toolbox in MATLAB

Video

Matlab - The Complete Course

Video

Digital Image Processing using MATLAB

Video

Backpropagation Learning Method in Matlab

Video

carringtonlickeply1959.blogspot.com

Source: https://www.tutorialspoint.com/matlab/matlab_plotting.htm