Sin (1/x), Fplot Command - y Sin (X), Area (X, Sin (X) ) - y Exp (-X. X), Barh (X, Exp (-X. X) )

You might also like

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

Specialized 2-D plotting functions

Practical Questions
Q1: By using Subplot command Draw the graphs of following functions each one with
its specified command:
- sin(1/x) , fplot command
- y='x^2+x+1, ezplot('x^2+x+1')
- y=sin(x),area(x,sin(x))
- y=exp(-x.*x),barh(x, exp(-x.*x));
put the suitable labels for x and y axis.
Put a suitable title for each graph refer to used command .
Hint : x=0:.01:1
Solution:
x=0:.01:1;
subplot(221), fplot(@(x)sin(1./x))
xlabel('x'),ylabel('sin (1/x)'),title(' fplot command')
subplot(222), ezplot('x^2+x+1')
xlabel('x'),ylabel('y=x^2+x+1'),title(' ezplot command')
subplot(223),area(x,sin(x))
xlabel('x'),ylabel('sin(x)'),title(' area command')
subplot(224),barh(x, exp(-x.*x));
xlabel('x'),ylabel('exp(-x.*x)'),title(' barh command')
Q2:
1- for fplot command of the y=sin(x)(at interval[0.02,0.2]) use the line style of : black
color, diamond symbol character, and solid line style.
2- for semilogx command of y=exp(-x^2) (at x=[0,5]) use red , point , dotted line.
3- for semilogy command of y=exp(-x^2) (at x=[0,5]) use yellow , square , dashdot line.
4- for loglog command of y=exp(-x^2) (at x=[0,5]) use green , circle, dashed.
5- for polar command of y=sin(2t)cos(2t)(at t=[0,2pi]) use magenta, star, dashdot.
Solution :
1- fplot('sin(x)', [0.02 0.2],'k+:');

2-x=0:0.05:5;
y=exp(-x.^2);
semilogx(x,y,'r.:');

3-x=0:0.05:5;
y=exp(-x.^2);
semilogy(x,y,'ys-.');
4-x=0:0.05:5;
y=exp(-x.^2);
loglog(x,y,'go--');

5-t=0:.01:2*pi;
polar(t,sin(2*t).*cos(2*t),'m*-.');
Q3: Draws the graph of following function Y=sin (1/x) , at interval [-2, 2]
By using these commands:
1- fplot
2- ezplot
3- area
4- semilogx
5- semilogy
6- LogLog
7-bar
8-barh
9-stem
10- stairs
Discuss the resulting graphs and explain why there is no commands such as pie, plotyy,hist,
polar, errorbar.

1- fplot('sin(1/x)', [-2 2]);

2- ezplot('sin(1/x)’, [-2, 2])

3-x=-2:.01:2; area(x,sin(1./x));
4-x=-2:0.01:2;
y=sin(1./x);
semilogx(x,y,'k+:');

5- x=-2:0.01:2;
y=sin(1./x);
semilogy(x,y,'k+:');

6-x=-2:0.01:2;
y=sin(1./x);
loglog(x,y,'k+:');
7- x=-2:0.01:2;
bar(x,sin(1./x));

8- x=-2:0.01:2; barh(x, sin(1./x));

9- x=-2:0.01:2; stem (x, sin(1./x));


10- stairs
x=-2:0. 1:2; stairs (x, sin(1./x));
Questions Sheet 1
Q1:
Plot the graph with a red color of the rational function y=x/ (1+x^2) at interval [-2, 2] with
use the linspace command to 10 elements. Put a suitable Title for graph and x, y axis, Then
after plotting change the range of x and y values by ([-2, 2,-.8, .8]) and activate the Grid.
Answer:
x = linspace(-2,2,10);
y = x./(1+x.^2);
plot(x,y,'r')
grid
xlabel('x'),ylabel('y'),title(' Graph plot ')
axis([-2 2 -0.8 0.8])

Q2: plot two ellipses by using command plot the Graphs of two ellipses
x(t) = 3 + 6cos(t), y(t) = -2 + 9sin(t) with red color.And
x(t) = 7 + 2cos(t), y(t) = 8 + 6sin(t)with blue color.
At t= [0,2pi].Put a suitable Title for graph and x,y axis and a Grid.
Answer
t = 0:pi/100:2*pi;
x1 = 3 + 6*cos(t);
y1 = -2 + 9*sin(t);
x2 = 7 + 2*cos(t);
y2 = 8 + 6*sin(t);
h1 = plot(x1,y1,'r',x2,y2,'b');
grid
xlabel('x'),ylabel('y'),
title('Graphs of (x-3)^2/36+(y+2)^2/81 = 1 and (x-7)^2/4+(y-8)^2/36 = 1.')
Q 3 plot the graphs of following functions on three separated graphs in one graphic window
with interval [0,2pi] :
Y=cos(x)
Y=sin(x)
Z= tan(x)
Put a suitable Title for graph and x,y axis and a Grid.
Answer
x=0:2*pi/10:2*pi;
subplot(131), plot(x,cos(x))
title('cos(x)');xlabel('x'),ylabel('cos(x)');grid
subplot(132), plot(x, sin(x))
title('sin(x)');xlabel('x'),ylabel('sin(x)');grid
subplot(133), plot(x, tan(x))
title('tan(x)');xlabel('x'),ylabel('tan(x)');grid
Q 4 plot the graphs of following functions on Multiple Plots with Hold with interval [0,2pi] :
Y=cos(x), plot in black color.
Y=sin(x), plot in yellow color.
Put a suitable Title for graph and x,y axis and a Grid.
Answer:
x=0:2*pi/10:2*pi;
y=cos(x)
title('cos(x)');xlabel('x'),ylabel('cos(x)');
z= sin(x)
title('sin(x)');xlabel('x'),ylabel('sin(x)');
plot(x,y,'k'); % plot cos in black
grid on
hold on;
plot(x,z,'y'); % plot sin in yellow
grid on
hold off;
Q 5 plot the graphs of following functions on Multiple Data Vectors Plots (by using plot
command )or(by using one plot command for two funcions)with interval [0,2pi] :
Y=cos(x), plot in black color.
Y=sin(x), plot in yellow color.
Put a suitable Title for graph and x,y axis and a Grid.
Answer:
x=0:2*pi/10:2*pi;
y1=cos(x)
y2= sin(x)
plot(x,y1,'k',x,y2,'y');
title('sin(x) and cos(x)');xlabel('x'),ylabel('y');
grid on
Q :Use area command to plot year’s vs population data of some country and add a suitable
title, xlabel, ylabel refer to this data.
year= [1860 1880 1900 1920 1940 1960 1980 2000]
population1= [15 25 40 80 120 180 200 285]
population2= [20 35 50 100 150 200 250 300]
population3= [12 20 30 65 100 150 170 260]
Hint: population /1000000
Solution
year=[1860 1880 1900 1920 1940 1960 1980 2000]
population1=[15 25 40 80 120 180 200 285]
population2=[20 35 50 100 150 200 250 300]
population3=[12 20 30 65 100 150 170 260]
area(year,[(population1)',(population2)',(population3)'])

Q
A- the climate data for some country show the temperature data for each month of the year
was reported as following:
Months= [1 2 3 4 5 6 7 8 9 10 11 12]
T= [17 21 27 35 40 44 48 52 41 36 25 19]
1- create the barh graph
2- change axis limit to [0 60 0 13]
3- Add a suitable title, xlabel, ylabel refer to this data.
B- Repeat the solution using bar command
c- Discus the results of A and B and explain the difference between then.
D- Explain the difference between using bar and barh.
Solution:
months=[1 2 3 4 5 6 7 8 9 10 11 12]
T= [17 21 27 35 40 44 48 52 41 36 25 19],
barh(months,T)
months=[1 2 3 4 5 6 7 8 9 10 11 12]
T= [17 21 27 35 40 44 48 52 41 36 25 19],
bar(months,T)
Q:
Using ezplot draw the graphs of following functions:
1- y=x2-y4 at interval[2pi,2pi]
2- sin(x) over the range [–?, ?].

Answer:
1-
x=[-2*pi,2*pi]
ezplot('x^2-y^4')

2-
ezplot('sin(x)',[-pi, pi])

Q: Draw the following functions: y1 = cos(x), y2 = sin(x) at interval [0:2pi] in magenta and
green color, Turn on the grid, change the axis limits at([0 2*pi -1.5 1.5]) then add a suitable
title and axis labels.
Solution:
% Define values for x, y1, and y2
x = 0: .1 : 2*pi;
y1 = cos(x);
y2 = sin(x);
% Plot y1 vs. x (blue, solid) and y2 vs. x (red, dashed)
figure
plot(x, y1, 'm', x, y2, 'g-.')
% Turn on the grid
grid on
% Set the axis limits
axis([0 2*pi -1.5 1.5])
% Add title and axis labels
title('Trigonometric Functions')
xlabel('angle')
ylabel('sin(x) and cos(x)')

Q:Use plotyy to draw the following two curves : y1 = 200exp(-0.05x)sin(x),y2 = 0.8exp(-


0.5x)sin(10x) at range[0,20] and Add title and x axis label.
Answer:
% Create some data for the two curves to be plotted
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

% Create a plot with 2 y axes using the yyaxis function


plotyy(x,y1,x,y2);
% Add title and x axis label
xlabel('Time in \mu sec.')
title('Frequency Response')
Q:
Create a stem plot for the following data and add a labels, title
profit = [20 40 50 40 50 60 70 60 70 60 60 70 80 90];
yr = 2001:2014;
Adjust the axis limits to axis([10 100 2000 2016])
Answer:
% Create a stem plot for the following data and add a labeles title
profit = [20 40 50 40 50 60 70 60 70 60 60 70 80 90];
yr = 2001:2014;
stem(yr,profit)

% Add label and title


xlabel('Month')
ylabel('Profit')
title('Amount of profit over 14 year')
Q9
Use the following data to create a y-axis semilog plot. Plot y1 data in blue,o,- and y2 data in
red,^,- With using hold on, also Turn on the grid and Add title and axis labels
x = 0:5;
y1 = [0.1447 0.1112 0.0722 0.0438 0.0243 0.0122];
y2 = [0.0753 0.0574 0.0370 0.0222 0.0122 0.0061];

Solution:

% Create a y-axis semilog plot using the semilogy function


% Plot y1 data in blue,o,- and y2 data in red,^,-
figure
semilogy(x, y1, 'bo-')
hold on
semilogy(x, y2, 'r^-')

% Turn on the grid


grid on

% Add title and axis labels


title('Performance of Baseband QPSK')
xlabel('EbNo (dB)')
ylabel('SER and BER')
Q 10
Draw the population of some country at year 2000 if you know the following data:
groups : 5-19,<5,65+,45-64,20-44 and The population of each category : 20 , 5, 12,35,60

solutions:
x=[20,5,12,35,60]
groups={'5-19','<5','65+','45-64','20-44'}
pie(x, groups)
Q 11
Use the subplot command to draw the graphs of following function each one with its
specified command:
1- Rho = 1 + 0.5*sin(7*t).*cos(3*t); polar command
t = 0:0.01:2*pi;
2-y=sin (2x), fplot at interval [0 2*pi]
3-y=0.5tan (0.8x), ezplot at [-pi/2 pi/2])
4- y = -0.7tan(0.8x),plot at [-pi/2 pi/2]

Solution:
t=0:.01:2*pi;
x2 = -pi/2:0.01:pi/2;
y3 = -0.7*tan(0.8*x2);
subplot(221), polar(t,1 + 0.5*sin(7*t).*cos(3*t));
xlabel('x'),ylabel('sin & cos'),title(' polar command')
subplot(222), fplot('sin(2*x1)',[0 2*pi])
xlabel('x'),ylabel('sin(2*x1)'),title(' fplot command')
subplot(223),ezplot('0.5*tan(0.8*x2)',[-pi/2 pi/2])
xlabel('x'),ylabel('tan'),title(' ezplot command')
subplot(224),plot(x2,y3)
xlabel('x'),ylabel('tan(0.8*x2)'),title(' plot command')
Q: Plot the log of the values from 1 to 100.
Ans:
x=1:100;
y=log(x);
plot(x,y,'r');

Q: Plot the parametric curve x = t cos(t), y = t sin(t) for 0 < t < 10.
Ans
t = 0:0.01*pi:10*pi;
x = t.*cos(t);
y = t.*sin(t);
plot(x,y);
Q:Plot the cubic curve y = x³. Label the axis and put a title "a cubic curve" on the top of the
graph.
Ans:
x=-2:.05:2; y=x.^3;
plot(x,y)
title('a cubic curve')

Q: Create surface plots for the functions


1- z = (x – 2)2 + 2xy + y2
2- z = x2 – 2xy + 4y2
3- z = -x2 + 2xy + 3y2
4- z = (x –y2) ( x – 3y2)

1- ‫نفرض قيم الفترة في حالة عدم أعطائها بالسؤال‬


[x, y]=meshgrid(-4:0.5:4,-4:0.5:4);
z=(x-2).^2+2*x*y+y.^2;
surf(x,y,z)
2-
x=linspace(-2, 2, 25);
y=linspace(-2, 2, 25);
[xx,yy]=meshgrid(x, y);
zz = xx.^2 - 2*xx.*yy + 4*yy.^2;
surf(xx, yy, zz);

3-
x=linspace(-2, 2, 25);
y=linspace(-2, 2, 25);
[xx,yy]=meshgrid(x, y);
zz = -xx.^2 + 2*xx.*yy + 3*yy.^2
surf(xx, yy, zz);

4-
x=linspace(-2, 2, 25);
y=linspace(-2, 2, 25);
[xx,yy]=meshgrid(x, y);
zz = (xx -yy.^2)*( xx - 3*yy.^2)
surf(xx, yy, zz);
Q3A–
Draw a 3D Graph represent a solid filled colored surface display changing temperature T
(600 t0 850 Kalvin) with time t (1 to 20 minute) if you know that:
Y= k1 t (k1+k3)/(k2t+1)(k1+k3)(1+(t(k1+k3)))
k1 = 107 exp(-12700/T)
k2 = 5* 104 exp(-10800/T)
k3 = 7*107 exp(-15000/T)
Activate the grid; label the graph and three axes.
B- Repeat the drawing using plot3. Display the two figures with hold command.
Answer:
A-
T=600:10:850;
t=1:2:20;
[tm,Tm] = meshgrid(t, T);
k1 = 10^7.*exp(-12700./Tm);
k2 = 5*10^4.*exp(-10800./Tm);
k3 = 7*10^7.*exp(-15000./Tm);
Y_B = (k1.*tm.*(k1+k3))./(((k2.*tm)+1).*(k1+k3).*(1+(tm.*(k1+k3))));
figure(1)
surf(t,T,Y_B)
grid on
xlabel('t')
ylabel('T')
zlabel('Y\_B')

B-
Y_Bmax = max(Y_B(:));
Idx = find(Y_B(:) == Y_Bmax);
[Y_BmaxRow,Y_BmaxCol] = ind2sub(size(Y_B), Idx);
figure(1)
surf(t,T,Y_B)
hold on
plot3(tm(Y_BmaxRow,Y_BmaxCol), Tm(Y_BmaxRow,Y_BmaxCol),
Y_B(Y_BmaxRow,Y_BmaxCol), '^r', 'MarkerFaceColor','r')
hold off
grid on
xlabel('t')
ylabel('T')
zlabel('Y\_B')
view(40, 45)
DisplayCoordinates = [tm(Y_BmaxRow,Y_BmaxCol), Tm(Y_BmaxRow,Y_BmaxCol),
Y_B(Y_BmaxRow,Y_BmaxCol)]
Q 4: By using Subplot command draw the three 3D Cylinder mesh graphs of following
functions:
- r=cos (t)
- r=sin (t)
- r=0.2 (t-pi)2
t= 0: 2*pi
N=30
Put suitable labels for axes. Put a suitable title for each graph.

Solution:
t=linspace(0,2*pi,50)
subplot(131), r=cos(t);[a,b,c]=cylinder(r,30);mesh(a,b,c)
xlabel('x'),ylabel('sin & cos'),title(' polar command')
subplot(132), r=sin(t);[a,b,c]=cylinder(r,30);mesh(a,b,c)
xlabel('x'),ylabel('sin(2*x1)'),title(' fplot command')
subplot(133), r=0.2*(t-pi).^2;[a,b,c]=cylinder(r,30);mesh(a,b,c)
xlabel('x'),ylabel('tan'),title(' ezplot command')

Q: By using figure command draw the three 3D Cylinder graphs using (plot3,mesh,surf) of
following function:
- r=cos (t)
t= 0: 2*pi
N=30
Put suitable labels for axes. Put a suitable title for each graph.
Define : plot3, mesh ,surf commands.
Compare and discuss the difference between resulting graphs.
Solution:
t=linspace(0,2*pi,50); r=cos(t);
[a,b,c]=cylinder(r,30);
plot3(a,b,c) figure;
t=linspace(0,2*pi,50); r=cos(t);
[a,b,c]=cylinder(r,30);
mesh(a,b,c) figure;
t=linspace(0,2*pi,50); r=cos(t);
[a,b,c]=cylinder(r,30); surf(a,b,c)
Q7: Draw the surf surface of the following:
V=2 r2 + 2 p2 , r=[3:14], p=[4,1,7,8,21,5,6,10,7]
Answer:
r= [3 4 5 9 7 8 10 15 14];
p= [4 1 7 8 21 5 6 10 7] ;
[R,P]=meshgrid(r,p);
V= 2*R.^2+2*P.^2 ;
surf(R,P,V)
Q8
Plot the x,y,z data as a solid filled colored surface if you know:
x=[1:10]
y=[1:20]
z= sin(x)+cos(x)

Answer:
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z)

You might also like