Top Banner
Matlab Graphics Visualization and Plotting 2D Plotting Given the arrays x = [0 1 2 3] and y = [0 1 4 9], use the plot function to produce a plot of x versus y. Matlab Code >> x=[0 1 2 3]; >> y=[0 1 4 9]; >> plot(x,y) Output
26

Matlab Graphics

Feb 11, 2016

Download

Documents

Umer Ishfaque

Matlab Graphics,Graphs 2d and 3d practice
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Matlab Graphics

Matlab Graphics

Visualization and Plotting

2D PlottingGiven the arrays x = [0 1 2 3] and y = [0 1 4 9], use the plot function toproduce a plot of x versus y.

Matlab Code

>> x=[0 1 2 3];

>> y=[0 1 4 9];

>> plot(x,y)

Output

Page 2: Matlab Graphics

Make a plot of the function f (x) = x2 for −5 ≤ x ≤ 5Matlab Code

>> x=linspace(-5,5,100);

>> plot(x,x.^2)

Output

Symbol Descriptionb blueg greenr redc cyanm magentay yellowk blackw white. pointo circlex x-mark+ plus* star

Page 3: Matlab Graphics

s squared diamondv triangle (down)ˆ triangle (up)< triangle (left)> triangle (right)p pentagramh hexagram- solid: dotted-. dashdot- - dashed(none) no line

Make a plot of the function f (x) = x2 for −5 ≤ x ≤ 5 using a dashed green lineMatlab Code>> x=linspace(-5,5,100);

>> plot(x,x.^2,'g--')

Output

Page 4: Matlab Graphics

Make a plot of the function f (x) = x2 and g(x) = x3 for −5 ≤ x ≤ 5 Use different colors and markers for each functionM -File Code%Matlab Graphics 1 x=linspace(-5,5,20);hold onplot(x,x.^2,'ko');plot(x,x.^3,'r*');hold off

Output

Add a title and axis labels to the previous plot

Page 5: Matlab Graphics

M-File Codex=linspace(-5,5,20);hold onplot(x,x.^2,'ko');plot(x,x.^3,'r*');hold offtitle('Plot of Various Poynmials')xlabel('x')ylabel('y')

Output

Use the sprintf function to change the title of the previous plot to “Plot of VariousPolynomials from −5 to 5"

M - File Codex=linspace(-5,5,20)hold onplot(x,x.^2,'ko')plot(x,x.^3,'r*')

Page 6: Matlab Graphics

hold offtitle(sprintf('Plot of various polynomials for %d to %d',x(1), x(end)))xlabel('xaxis')ylabel('yaxis')grid on

OutPut

Add a legend to the previous plot using the legend function

M-File Codex=linspace(-5,5,20)hold onplot(x,x.^2,'ko')plot(x,x.^3,'r*')hold offtitle(sprintf('Plot of various polynomials for %d to %d',x(1), x(end)))xlabel('xaxis')ylabel('yaxis')legend('quadratic','cubic')

Page 7: Matlab Graphics

Output

Use the axis function to change the limits such that x is visible from −6 to 6 and y is visible from −10 to 10

M-Filex=linspace(-5,5,100)hold onplot(x,x.^2,'ko')plot(x,x.^3,'r*')hold offtitle(sprintf('Plot of various polynomials for %d to %d',x(1), x(end)))xlabel('xaxis')ylabel('yaxis')legend('quadratic','cubic')axis([-6 6 -10 10])grid on

OutPut

Page 8: Matlab Graphics

Given the arraysx = [0 1 2 3 4 5]andy = [1 2 4 8 16 2], create a 2×3subplot where each subplot plots x versus y using plot, scatter, bar, loglog, semilogx,and semilogy. Title and label each plot appropriately. Use a grid, but a legend is not necessary.

M-File Code

x=[0 1 2 3 4 5];y=[1 2 4 8 16 2]; subplot(2,3,1)plot(x,y)title('Plot')xlabel('X')ylabel('Y')grid on subplot(2,3,2)scatter(x,y)title('scatter')xlabel('X')ylabel('Y')

Page 9: Matlab Graphics

grid on subplot(2,3,3)bar(x,y)title('Bar')xlabel('X')ylabel('Y')grid on subplot(2,3,4)loglog(x,y)title('LogLog')xlabel('X')ylabel('Y')grid on subplot(2,3,5)semilogx(x,y)title('Semilogx')xlabel('X')ylabel('Y')grid on subplot(2,3,6)semilogy(x,y)title('semilogy')xlabel('x')ylabel('y')grid on

OutPut

Page 10: Matlab Graphics

3D Plotting

M-File Codet=[0:pi/50:10*pi];plot3(sin(t),cos(t),t)grid onaxis squaretitle('Parametric curve')xlabel('x')ylabel('y')zlabel('t')

OutPut

Page 11: Matlab Graphics

Make a 1 × 2 subplot of the surface f (x, y) = sin (x) ・ cos (y) for −5 ≤ x ≤ 5,−5 ≤y ≤ 5, the first using the surf function and the second using the contour function. Take care touse a sufficiently fine discretization in x and y to make the plot look smooth.

M-File Code

x=-5:.2:5;y=-5:.2:5;[X,Y]=meshgrid(x,y);z=sin(X).*cos(Y);subplot(1,2,1)surf(x,y,z)title('Surface plot using Surf')xlabel('x')ylabel('y')zlabel('z')subplot('1,2,2')contour(x,y,z)title('Surface plot using contour')xlabel('x')ylabel('y')

OutPut

Page 12: Matlab Graphics

Fplot

f(t)= e−2t sin t for 0 ≤ t ≤ 4,

Command Window code

>> fplot('exp(-2*t)*sin(t)',[0,4]),xlabel('t'),ylabel('f(t)'),title('Damped Spring Forcing')

Page 13: Matlab Graphics

Plotting Discrete DataNow let’s touch on some practical uses of MATLAB that you might need at workor in the field. First let’s see how we can use plot(x, y) to plot a discrete set of datapoints and connect them with a line. Let’s say we have a class with five students:Adrian, Jim, Joe, Sally, and Sue. They took an exam and received scores of 50, 98,75, 80, and 98, respectively. How can we plot this data?

1.Bar PlotM file Code>> x=[1:5];>> y=[50,98,75,80,98];>> bar(x,y),xlabel('student'),ylabel('score')>> bar(x,y),xlabel('student'),ylabel('score');>> bar(x,y),xlabel('student'),ylabel('score'),title('Final Exam')

OutPut

Page 14: Matlab Graphics

2.Stem Plot

M-File Code>> t=[0:5:200];>> f=exp(-0.01*t).*sin(t/4);>> stem(t,f),xlabel('time(sec)'),ylabel('spring response');

OutPut

Page 15: Matlab Graphics

Polar Plot

command window

>> a=2;>> theta=[0:pi/20:2*pi];>> r=a*theta;>> polar(theta,r)

Out Put

2.polar plot

Command window

a=2;>> theta=[0:pi/90:2*pi];>> >> r=1+2*cos(theta);

Output

Page 16: Matlab Graphics

Contour Plot

Command Window

>> [x,y]=meshgrid(-5:0.1:5,-3:0.1:3);>> z=x.^2+y.^2;>> contour(x,y,z)

Output

Page 17: Matlab Graphics

Contour 3d plot

Command Window>> [x,y]=meshgrid(-5:0.1:5,-3:0.1:3);>> z=x.^2+y.^2;>> contour3(x,y,z)

output

Contour 3d plot

Command Window

>> [x,y]=meshgrid(-5:0.1:5,-3:0.1:3);>> z=cos(x).*sin(y);>> contour3(x,y,z)>>

Page 18: Matlab Graphics

Difference between plot,semilogx,semilogy

1.plot

Command Window>> x=0:0.2:100;>> y=x.^2;>> plot(x,y

Out Put

Page 19: Matlab Graphics

2.semilogx

Command Window

>> x=0:0.2:100;>> y=x.^2;

OutPut

3. semilogy

command window

>> x=0:0.2:100;>> y=x.^2;>> semilogy(x,y)

output

Page 20: Matlab Graphics

4.loglog

Command Window

>> x=0:0.2:100;>> y=x.^2;

output

Plotting of different arbitrary points

>> plot([0,1],[4,3],[2,0],[5,-2])

output

Page 21: Matlab Graphics

New Command

gtextcommand window >> x=[0:0.1:10];>> y=x.^3+x.^2+3.*x;>> plot(x,y),xlabel('X'),ylabel('Y'),title(' gtext comand checking'),legend('x.^3+x.^2+3.*x'),gtext('pakistan')

output

Page 22: Matlab Graphics
Page 23: Matlab Graphics