MATLAB Plotting: Introduction and Examples
This content provides a hands-on guide to plotting functions in MATLAB, including creating arrays, plotting basic trigonometric functions like sin(x), manipulating arrays, and displaying plot properties. It covers essential plotting techniques, functions, display facilities, customization options, and examples, offering a comprehensive introduction to MATLAB plotting capabilities.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Introduction to MATLAB Plotting LAB 3
Basic Task: Plot the function sin(x) between 0 x 4 Create an x-array of 100 samples between 0 and 4 . >>x=linspace(0,4*pi,100); Calculate sin(.) of the x-array 1 0.8 0.6 >>y=sin(x); 0.4 0.2 Plot the y-array 0 -0.2 -0.4 -0.6 >>plot(y) -0.8 -1 0 10 20 30 40 50 60 70 80 90 100
Plot the function e-x/3sin(x) between 0 x 4 Create an x-array of 100 samples between 0 and 4 . >>x=linspace(0,4*pi,100); Calculate sin(.) of the x-array >>y=sin(x); Calculate e-x/3 of the x-array >>y1=exp(-x/3); Multiply the arrays y and y1 >>y2=y*y1;
Plot the function e-x/3sin(x) between 0 x 4 Multiply the arrays y and y1 correctly >>y2=y.*y1; Plot the y2-array 0.7 >>plot(y2) 0.6 0.5 0.4 0.3 0.2 0.1 0 -0.1 -0.2 -0.3 0 10 20 30 40 50 60 70 80 90 100
Display Facilities 0.7 0.6 plot(.) 0.5 0.4 0.3 Example: >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y) >>plot(x,y) 0.2 0.1 0 -0.1 -0.2 -0.3 0 10 20 30 40 50 60 70 80 90 100 stem(.) 0.7 0.6 0.5 0.4 0.3 Example: >>stem(y) >>stem(x,y) 0.2 0.1 0 -0.1 -0.2 -0.3 0 10 20 30 40 50 60 70 80 90 100
Display Facilities title(.) This is the sinus function >>title( This is the sinus function ) 1 0.8 xlabel(.) 0.6 0.4 >>xlabel( x (secs) ) 0.2 sin(x) 0 ylabel(.) -0.2 -0.4 -0.6 -0.8 >>ylabel( sin(x) ) -1 0 10 20 30 40 50 60 70 80 90 100 x (secs)
Plot Properties title legend ylabel xlabel
Plot function where the variables x and y are vector and must be of same size i.e the number of elements should be equal. plot(x,y) x=[1 2 3 5 7 7.5 8 10]; y=[2 6.5 7 7 5.5 4 6 8]; plot(x,y)
Plot function plot(x,y, line specifiers , PropertyName ,PropertyValue) Line specifiers Line specifiers are optional and can be used to define the style and color of the line and the type of markers (if markers are desired). Line Style Specifier solid (default) dashed dotted dash-dot - -- : -. Line color Specifier r g b c Line color Specifier m y k w red green blue cyan magenta yellow black white
Plot function plot(x,y, line specifiers , PropertyName ,PropertyValue) Line specifiers Line specifiers are optional and can be used to define the style and color of the line and the type of markers (if markers are desired). Marker type Marker type Specifier Specifier s d p h < > square diamond five pointed star six pointed star triangle (pointed left) triangle (pointed right) plus sign circle asterisk point cross triangle (pointed up) triangle (pointed down) + o * . ^ v
Stem plot figure X = linspace(0,2*pi,50)'; Y = [cos(X), 0.5*sin(X)]; stem(Y)
Subplots x = linspace(0,10); y1 = sin(x); y2 = sin(2*x); y3 = sin(4*x);y4 = sin(8*x); figure subplot(2,2,1) plot(x,y1) title('Subplot 1: sin(x)') subplot(2,2,2) plot(x,y2) title('Subplot 2: sin(2x)') subplot(2,2,3) plot(x,y3) title('Subplot 3: sin(4x)') subplot(2,2,4) plot(x,y4) title('Subplot 4: sin(8x)')
Example: dual y-axis 45 40 80 35 Output Power (dBm) 60 PAE (%) 30 40 25 20 20 15 400 0 0 5 5 10 10 15 15 20 20 25 25 30 30 35 35 40 Input Power (dBm)
Polar Plot theta = 0:0.01:2*pi; rho = sin(2*theta).*cos(2*theta); figure polar(theta,rho,'--r')
Plot Matrix: comparison b/w datasets X = randn(50,3); plotmatrix(X,'*r')
3D Shaded surface plot: SURF [X,Y,Z] = peaks(25); figure surf(X,Y,Z); Sphere with two colors
MESH Plot [X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R; figure mesh(Z)