SlideShare una empresa de Scribd logo
1 de 6
Descargar para leer sin conexión
http://www.tutorialspoint.com/matlab/matlab_plotting .htm Copyright © tutorialspoint.com
MATLAB - PLOTTING
To plot the graphof a function, youneed to take the following steps:
1. Define x, by specifying the range of values for the variable x, for whichthe functionis to be plotted
2. Define the function, y = f(x)
3. Callthe plot command, as plot(x, y)
Following example would demonstrate the concept. Let us plot the simple functiony = x for the range of values
for x from0 to 100, withanincrement of 5.
Create a script file and type the following code:
x = [0:5:100];
y = x;
plot(x, y)
Whenyourunthe file, MATLAB displays the following plot:
Let us take one more example to plot the functiony = x2. Inthis example, we willdraw two graphs withthe same
function, but insecond time, we willreduce the value of increment. Please note that as we decrease the
increment, the graphbecomes smoother.
Create a script file and type the following code:
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
y = x.^2;
plot(x, y)
Whenyourunthe file, MATLAB displays the following plot:
Change the code file a little, reduce the increment to 5:
x = [-100:5:100];
y = x.^2;
plot(x, y)
MATLAB draws a smoother graph:
Adding Title, Labels, Grid Lines and Scaling on the Graph
MATLAB allows youto 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 youto put a title onthe graph.
The grid on command allows youto put the grid lines onthe graph.
The axis equal command allows generating the plot withthe same scale factors and the spaces onboth
axes.
The axis square command generates a square plot.
Example
Create a script file and type the following code:
x = [0:0.01:10];
y = sin(x);
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
grid on, axis equal
MATLAB generates the following graph:
Drawing Multiple Functions on the Same Graph
Youcandraw multiple graphs onthe 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)')
MATLAB generates the following graph:
Setting Colors on Graph
MATLAB provides eight basic color options for drawing graphs. The following table shows the colors and their
codes:
Color Code
White w
Black k
Blue b
Red r
Cyan c
Green g
Magenta m
Yellow y
Example
Let us draw the graphof two polynomials
1. f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and
2. g(x) = 5x3 + 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')
Whenyourunthe file, MATLAB generates the following graph:
Setting Axis Scales
The axis command allows youto set the axis scales. Youcanprovide minimumand maximumvalues for x and y
axes using the axis command inthe following way:
axis ( [xmin xmax ymin ymax] )
The following example shows this:
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])
Whenyourunthe file, MATLAB generates the following graph:
Generating Sub-Plots
Whenyoucreate anarray of plots inthe same figure, eachof these plots is called a subplot. The subplot
command is for creating subplots.
Syntax for the command is:
subplot(m, n, p)
where, m and n are the number of rows and columns of the plot array and p specifies where to put a particular
plot.
Eachplot created withthe subplot command canhave its owncharacteristics. Following example demonstrates
the concept:
Example
Let us generate two plots:
y = e−1.5xsin(10x)
y = e−2xsin(10x)
Create a script file and type the following code:
x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1])
y = exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1])
Whenyourunthe file, MATLAB generates the following graph:

Más contenido relacionado

La actualidad más candente

Window to viewport transformation
Window to viewport transformationWindow to viewport transformation
Window to viewport transformation
Ankit Garg
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
Haitham El-Ghareeb
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
myrajendra
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
Mohd Arif
 

La actualidad más candente (20)

Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
3D transformation in computer graphics
3D transformation in computer graphics3D transformation in computer graphics
3D transformation in computer graphics
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Window to viewport transformation
Window to viewport transformationWindow to viewport transformation
Window to viewport transformation
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
2D viewing & clipping
2D viewing & clipping2D viewing & clipping
2D viewing & clipping
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
 
Depth Buffer Method
Depth Buffer MethodDepth Buffer Method
Depth Buffer Method
 
Computer graphics curves and surfaces (1)
Computer graphics curves and surfaces (1)Computer graphics curves and surfaces (1)
Computer graphics curves and surfaces (1)
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
 
BRESENHAM’S LINE DRAWING ALGORITHM
BRESENHAM’S  LINE DRAWING ALGORITHMBRESENHAM’S  LINE DRAWING ALGORITHM
BRESENHAM’S LINE DRAWING ALGORITHM
 
Weiler atherton
Weiler athertonWeiler atherton
Weiler atherton
 
Dip Morphological
Dip MorphologicalDip Morphological
Dip Morphological
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 

Destacado (7)

matlab functions
 matlab functions  matlab functions
matlab functions
 
Matlab tutorial
Matlab tutorialMatlab tutorial
Matlab tutorial
 
graphs plotting in MATLAB
graphs plotting in MATLABgraphs plotting in MATLAB
graphs plotting in MATLAB
 
Matlab
MatlabMatlab
Matlab
 
Mit6 094 iap10_lec01
Mit6 094 iap10_lec01Mit6 094 iap10_lec01
Mit6 094 iap10_lec01
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 

Similar a Matlab plotting

More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
gilpinleeanna
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
agnesdcarey33086
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
asguna
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
anhlodge
 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
TUOS-Sam
 

Similar a Matlab plotting (20)

Lesson 3
Lesson 3Lesson 3
Lesson 3
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
 
Programming with matlab session 6
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Mat lab
Mat labMat lab
Mat lab
 
Tutorial 2
Tutorial     2Tutorial     2
Tutorial 2
 
COMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptxCOMPANION TO MATRICES SESSION III.pptx
COMPANION TO MATRICES SESSION III.pptx
 
Matlab algebra
Matlab algebraMatlab algebra
Matlab algebra
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Lecture 2 f17
Lecture 2 f17Lecture 2 f17
Lecture 2 f17
 
Lec2
Lec2Lec2
Lec2
 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
 

Más de pramodkumar1804 (20)

Matlab syntax
Matlab syntaxMatlab syntax
Matlab syntax
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
Matlab simulink
Matlab simulinkMatlab simulink
Matlab simulink
 
Matlab polynomials
Matlab polynomialsMatlab polynomials
Matlab polynomials
 
Matlab overview 3
Matlab overview 3Matlab overview 3
Matlab overview 3
 
Matlab overview 2
Matlab overview 2Matlab overview 2
Matlab overview 2
 
Matlab overview
Matlab overviewMatlab overview
Matlab overview
 
Matlab operators
Matlab operatorsMatlab operators
Matlab operators
 
Matlab variables
Matlab variablesMatlab variables
Matlab variables
 
Matlab numbers
Matlab numbersMatlab numbers
Matlab numbers
 
Matlab matrics
Matlab matricsMatlab matrics
Matlab matrics
 
Matlab m files
Matlab m filesMatlab m files
Matlab m files
 
Matlab loops 2
Matlab loops 2Matlab loops 2
Matlab loops 2
 
Matlab loops
Matlab loopsMatlab loops
Matlab loops
 
Matlab gnu octave
Matlab gnu octaveMatlab gnu octave
Matlab gnu octave
 
Matlab operators
Matlab operatorsMatlab operators
Matlab operators
 
Matlab functions
Matlab functionsMatlab functions
Matlab functions
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
Matlab decisions
Matlab decisionsMatlab decisions
Matlab decisions
 
Matlab data import
Matlab data importMatlab data import
Matlab data import
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Matlab plotting

  • 1. http://www.tutorialspoint.com/matlab/matlab_plotting .htm Copyright © tutorialspoint.com MATLAB - PLOTTING To plot the graphof a function, youneed to take the following steps: 1. Define x, by specifying the range of values for the variable x, for whichthe functionis to be plotted 2. Define the function, y = f(x) 3. Callthe plot command, as plot(x, y) Following example would demonstrate the concept. Let us plot the simple functiony = x for the range of values for x from0 to 100, withanincrement of 5. Create a script file and type the following code: x = [0:5:100]; y = x; plot(x, y) Whenyourunthe file, MATLAB displays the following plot: Let us take one more example to plot the functiony = x2. Inthis example, we willdraw two graphs withthe same function, but insecond time, we willreduce the value of increment. Please note that as we decrease the increment, the graphbecomes smoother. Create a script file and type the following code: x = [1 2 3 4 5 6 7 8 9 10]; x = [-100:20:100]; y = x.^2; plot(x, y) Whenyourunthe file, MATLAB displays the following plot:
  • 2. Change the code file a little, reduce the increment to 5: x = [-100:5:100]; y = x.^2; plot(x, y) MATLAB draws a smoother graph: Adding Title, Labels, Grid Lines and Scaling on the Graph MATLAB allows youto 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 youto put a title onthe graph. The grid on command allows youto put the grid lines onthe graph. The axis equal command allows generating the plot withthe same scale factors and the spaces onboth axes.
  • 3. The axis square command generates a square plot. Example Create a script file and type the following code: x = [0:0.01:10]; y = sin(x); plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'), grid on, axis equal MATLAB generates the following graph: Drawing Multiple Functions on the Same Graph Youcandraw multiple graphs onthe 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)') MATLAB generates the following graph:
  • 4. Setting Colors on Graph MATLAB provides eight basic color options for drawing graphs. The following table shows the colors and their codes: Color Code White w Black k Blue b Red r Cyan c Green g Magenta m Yellow y Example Let us draw the graphof two polynomials 1. f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and 2. g(x) = 5x3 + 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') Whenyourunthe file, MATLAB generates the following graph:
  • 5. Setting Axis Scales The axis command allows youto set the axis scales. Youcanprovide minimumand maximumvalues for x and y axes using the axis command inthe following way: axis ( [xmin xmax ymin ymax] ) The following example shows this: 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]) Whenyourunthe file, MATLAB generates the following graph:
  • 6. Generating Sub-Plots Whenyoucreate anarray of plots inthe same figure, eachof these plots is called a subplot. The subplot command is for creating subplots. Syntax for the command is: subplot(m, n, p) where, m and n are the number of rows and columns of the plot array and p specifies where to put a particular plot. Eachplot created withthe subplot command canhave its owncharacteristics. Following example demonstrates the concept: Example Let us generate two plots: y = e−1.5xsin(10x) y = e−2xsin(10x) Create a script file and type the following code: x = [0:0.01:5]; y = exp(-1.5*x).*sin(10*x); subplot(1,2,1) plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1]) y = exp(-2*x).*sin(10*x); subplot(1,2,2) plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1]) Whenyourunthe file, MATLAB generates the following graph: