Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Plotting

An introduction
April 2016
Dr. Marco A Arocha
ezplot, plot, fplot
(http://www.mathworks.com/help/matlab/ref/plot.html?refresh=true)

2D PLOTS
ezplot function
% Heart Plot Allow to print more
than one line in a
clc, clear all, close all graph

hold on

y='x^2+(y-x^(2/3))^2-1';
ezplot(y,[-2,2]);

yy='((-x)^2)+(y-(-x)^(2/3))^2-1';
ezplot(yy,[-2,2]);

hold off
X-axis range
plot function
x = 0:pi/10:2*pi; • Plot three sine curves
with a small phase
y1 = sin(x); shift between each
line.
y2 = sin(x-0.25); • Use a green line with
y3 = sin(x-0.5); no markers for the
first sine curve.
• Use a blue dashed line
with circle markers for
figure the second sine curve.
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*') • Use only cyan star
markers for the third
sine curve.
y1 y2 y3
plot function, output
LineSpec — Line style, marker, and color
• https://www.mathworks.com/help/matlab/ref/plot.html
• https://www.mathworks.com/help/matlab/ref/linespec.html
Ball falling example
% ballFalling.m
% Ball Vertical falling

clc, clear, clf


x(1)=10; % [distance from ground]
vo=-15; % initial velocity
a=-9.81; % acceleration

fprintf(' t, x \n'); % Table Title


t=[0:0.05:0.5];
fprintf(' %.3f %.3f \n', t(1), x(1)); % Statements produce table
for ii=2:numel(t)
x(ii)=x(1)+vo*t(ii)+a*t(ii)^2;
fprintf(' %.3f %.3f \n', t(ii), x(ii)); % Statements produce table
end
plot(t,x);
xlabel('time');ylabel('x, distance from ground');
title('falling ball [distance from ground vs time]');
grid on
fplot [will replace ezplot]
% Root Finding
% fzero example
% File: funPlot.m
Anonymous
clc, clear function

f=@(x) x^3-2*x^2-5*x+6;
fplot(f,[-5,5])
grid on

R2015b version NEEDS range specification to run


polar plot (matlab R2016a)
% Polar Plots
clc,clear, close

theta = 0:0.01:2*pi;
rho = sin(2*theta).*cos(2*theta);
polarplot(theta,rho)

% polarplot function doesn’t


% work in ML earlier versions

http://www.mathworks.com/help/matlab/ref/polarplot.html
polar plot (matlab R2015b)
% Polar Plots
clc,clear, close

theta = 0:0.01:2*pi;
rho = sin(2*theta).*cos(2*theta);
polar(theta,rho);
3D PLOTS
new temperatures, file: Tn.m
50.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 12.5
100.0 49.8 29.9 20.5 15.4 12.5 11.1 11.2 14.3 25.0
100.0 69.4 49.2 36.7 28.7 23.6 20.6 19.5 20.9 25.0
100.0 78.4 61.0 48.2 39.0 32.5 28.1 25.5 24.6 25.0
100.0 83.2 68.3 56.2 46.7 39.3 33.8 29.8 27.0 25.0
100.0 85.9 72.9 61.6 52.1 44.4 38.0 32.9 28.7 25.0
100.0 87.6 75.8 65.2 55.9 47.9 41.1 35.1 29.9 25.0
100.0 88.5 77.5 67.4 58.3 50.3 43.1 36.7 30.7 25.0
100.0 89.0 78.4 68.6 59.8 51.8 44.5 37.7 31.3 25.0
• grid has 10 values in x-
100.0 88.9 78.5 68.9 60.3 52.5 45.4 38.5 31.7 25.0
direction and 15 values in y-
100.0 88.3 77.6 68.3 60.1 52.7 45.9 39.1 32.2 25.0
direction
100.0 86.7 75.5 66.4 59.0 52.4 46.3 39.9 32.9 25.0
y • The values of temperatures
100.0 83.0 71.1 63.0 56.9 51.8 46.9 41.5 34.5 25.0
at each node representes the
100.0 74.0 63.2 57.5 53.8 50.9 48.1 44.5 38.5 25.0
z-direction.
75.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 37.5
x
% 3D plot of new Temperatures (Tn) in Heat Transfer Problem

clear all, close all

x=1:1:10; y=1:1:15; % Create the grid; Decide the number of x and y nodes
[X,Y] = meshgrid(x,y); % set up 2-D plane

load Tn.m % Loads 2D array named Tn 3D plot, new


Z = Tn; % Tn is the Z axis
temperatures
figure % plot 3rd dimension over a plane

surf(X,Y,Z,gradient(Z)) % surface plot, with gradient(Z)


% gradient(Z) determines the color distribution

colorbar % display color scale, can adjust location similarly to legend


Tn
plot
Can rotate the figure
Tn
plot
Other examples
• Mountain Chunk Removal for construction of a road:
– See document: CP#2 Mountain Integration-REVISED-v3

You might also like