Está en la página 1de 24

Matlab Graphics

Visualization and Plotting


2D Plotting
Given the arrays x=[0123]and y=[0149], use the plot function
to
produce a plot of xversus y.
Matlab Code
>> x=[0 1 2 3];
>> y=[0 1 4 9];
>> plot(x,y)
Output

Make a plot of the function f (x) = x2 for 5 x 5


Matlab Code

>> x=linspace(-5,5,100);
>> plot(x,x.^2)
Output

Symbol Description

bblue
ggreen
rred
ccyan
mmagenta
yyellow
kblack
wwhite
.point
ocircle
xx-mark

+plus
*star
ssquare
ddiamond
vtriangle (down)
triangle (up)
<triangle (left)
>triangle (right)
ppentagram
hhexagram
solid
:dotted
.dashdot
dashed
(none)no line

Make a plot of the function f (x) = x2 for 5 x 5 using a


dashed green line
Matlab Code
>> x=linspace(-5,5,100);
>> plot(x,x.^2,'g--')

Output

Make a plot of the function f (x) = x2 and g(x) = x3 for 5 x 5


Use different colors and markers for each function
M -File Code
%Matlab Graphics 1
x=linspace(-5,5,20);
hold on
plot(x,x.^2,'ko');
plot(x,x.^3,'r*');
hold off
Output

Add a title and axis labels to the previous


plot
M-File Code
x=linspace(-5,5,20);
hold on
plot(x,x.^2,'ko');
plot(x,x.^3,'r*');
hold off
title('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 Code
x=linspace(-5,5,20)
hold on
plot(x,x.^2,'ko')
plot(x,x.^3,'r*')
hold off
title(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 legendfunction


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

Use the axisfunction to change the limits such that xis visible
from 6 to 6 and yis visible from 10 to 10
M-File
x=linspace(-5,5,100)
hold on
plot(x,x.^2,'ko')
plot(x,x.^3,'r*')
hold off
title(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

Given the arraysx=[012345]andy=[1248162],


create a 23subplot where each subplot plots xversus yusing
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')

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

3D Plotting
M-File Code
t=[0:pi/50:10*pi];
plot3(sin(t),cos(t),t)
grid on
axis square
title('Parametric curve')
xlabel('x')
ylabel('y')
zlabel('t')
OutPut

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 surffunction and the
second using the contourfunction. Take care touse a sufficiently
fine discretization in xand yto 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

Fplot

f(t)= e2t 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')

Plotting Discrete Data


Now lets touch on some practical uses of MATLAB that you might
need at work
or in the field. First lets see how we can use plot(x, y) to plot a
discrete set of data
points and connect them with a line. Lets 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 Plot
M 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

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

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

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

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)
>>

Difference between plot,semilogx,semilogy


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

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

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

New Command
gtext
command 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

También podría gustarte