banner



How To Draw Tangent Line On Graph In Matlab

MATLAB - Plotting


To plot the graph of a role, you need to take the following steps −

  • Ascertain x, past specifying the range of values for the variable x, for which the function is to be plotted

  • Ascertain the role, y = f(x)

  • Call the plot control, as plot(ten, y)

Following example would demonstrate the concept. Let us plot the uncomplicated role y = ten for the range of values for 10 from 0 to 100, with an increment of 5.

Create a script file and type the following code −

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

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

Plotting y = x

Let usa have ane more example to plot the role y = 102. In this instance, we will depict ii graphs with the same part, but in second time, we will reduce the value of increment. Please note that as we decrease the increment, the graph becomes smoother.

Create a script file and type the following code −

10 = [1 2 three 4 5 half dozen 7 eight 9 ten]; x = [-100:20:100]; y = x.^2; plot(x, y)        

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

Plotting y = x^2

Change the lawmaking file a little, reduce the increment to five −

10 = [-100:5:100]; y = ten.^ii; plot(ten, 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 title, labels along the 10-axis and y-centrality, 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-centrality.

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

  • The grid on command allows you lot 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 axis square control generates a square plot.

Case

Create a script file and blazon the following code −

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

MATLAB generates the following graph −

Sprucing up our graphs

Drawing Multiple Functions on the Same Graph

You can draw multiple graphs on the same plot. The post-obit instance demonstrates the concept −

Example

Create a script file and type the following code −

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

MATLAB generates the following graph −

Multiple Functions on the Same Graph

Setting Colors on Graph

MATLAB provides viii basic color options for cartoon graphs. The following tabular array shows the colors and their codes −

Code Color
w White
grand Black
b Blue
r Red
c Cyan
thousand Green
m Magenta
y Yellowish

Example

Permit us depict the graph of two polynomials

  • f(x) = 3x4 + 2x3+ 7x2 + 2x + nine and

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

Create a script file and type the following code −

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

When you run the file, MATLAB generates the post-obit graph −

Colors on Graph

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 centrality command in the following way −

axis ( [xmin xmax ymin ymax] )        

The following example shows this −

Case

Create a script file and type the following code −

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

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

Setting Axis Scales

Generating Sub-Plots

When y'all create an assortment of plots in the same effigy, each of these plots is called a subplot. The subplot control is used for creating subplots.

Syntax for the command is −

subplot(m, n, p)        

where, m and north are the number of rows and columns of the plot array and p specifies where to put a item plot.

Each plot created with the subplot command can take its own characteristics. Post-obit case demonstrates the concept −

Example

Permit u.s.a. generate two plots −

y = e−1.5xsin(10x)

y = due east−2xsin(10x)

Create a script file and type the following code −

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

When you 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

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

Posted by: juarezroake1984.blogspot.com

0 Response to "How To Draw Tangent Line On Graph In Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel