Professional Documents
Culture Documents
Experiment # 02
Performed on ____________________
Student Name:
Roll Number: Section:
Experiment evaluated by
Signature:
In the command window in MATLAB window, input the time from t=0 sec to t=10 sec in
increments of 0.1 sec as follows:
>>t=[0:0.1:10];
>>y=cos(t);
>>plot(t,y)
>>t=0:pi/100:2*pi;
3. Sub Plots:
It can sometimes be useful to display
multiple plots on the same figure for comparison. This can be done using the subplot
function that takes arguments for number of rows of plots, number of columns of plots,
and plot number currently being plotted:
Example:
clear all
close all
clc
x=0:0.1:2*pi;
subplot(2,2,1)
plot(x,sin(x));
subplot(2,2,2)
plot(x,cos(x));
subplot(2,2,3)
plot(x,exp(-x));
subplot(2,2,4);
plot(x, x.^3);
x=0:pi/16:2*pi;
y=sin(x);
plot(x,y,'r *--')
xlabel('x')
ylabel('sin(x)')
Scatter Plot:
Contour Plot:-
Contour plots (sometimes called Level Plots) are a way to show a three-dimensional
surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis
and a response variable Z as contours. These contours are sometimes called z-slices or
iso-response values. This type of graph is widely used in cartography, where contour
lines on a topological map indicate elevations that are the same. Many other disciples use
contour graphs including: astrology, meteorology, and physics. Contour lines commonly
show altitude (like height of a geographical features), but they can also be used to show
density, brightness, or electric potential.
Command:
contour(X,Y,Z)
Fig: Heatmap
parula
jet
hsv
hot
cool
spring
summer
autumn
winter
gray
bone
copper
lines
colorcube
prism
flag
white
Fig: Default color map color range
Command:
colormap(bone)
Tasks:
Q1. Plot sine wave w.r.t time interval t = [0:0.5:20]
Q2: What is function of linspace()? Describe the function on the basis of the following
code.
x=linspace(0,2*pi,25);
y=sin(x);
y1=cos(x);
subplot(1,2,1)
plot(x,y);
subplot(1,2,2)
plot(x,y1);
Q3: In above question, change line color for sine wave to cyan and for cosine wave to
magenta as indicated in color table. Also assign legends and appropriate title to each
plot.
Q6: Using the code in Q3, generate and plot the discrete form of both y and y1 using
appropriate command.
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
scatter3(x,y,z)