0% found this document useful (0 votes)
450 views

Exercise 3

The document provides examples of plotting different functions in MATLAB: 1) It shows how to create a simple sine plot and modify it to display unconnected data points with circles. 2) It demonstrates plotting an exponentially decaying sine function with different numbers of points. 3) A circular helix is plotted in 3D. 4) Help for the plot command is accessed and log scale plots as well as overlaying multiple plots are illustrated.

Uploaded by

ashutosh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
450 views

Exercise 3

The document provides examples of plotting different functions in MATLAB: 1) It shows how to create a simple sine plot and modify it to display unconnected data points with circles. 2) It demonstrates plotting an exponentially decaying sine function with different numbers of points. 3) A circular helix is plotted in 3D. 4) Help for the plot command is accessed and log scale plots as well as overlaying multiple plots are illustrated.

Uploaded by

ashutosh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXERCISE 3

PLOTS
A simple sin plot:
Plot y = sin x, 0 ≤ x ≤ 2π, taking 100 linearly spaced points in
the given interval. Label the axes and put ‘Plot created by
yourname’ in the title.
A simple sin plot:
Plot y = sin x, 0 ≤ x ≤ 2π, taking 100 linearly spaced points in
the given interval. Label the axes and put ‘Plot created by
yourname’ in the title.

ANSWER:
 x=linspace(0,2*pi,100);
 plot(x,sin(x))
 xlabel(’x’), ylabel(’sin(x)’)
 title(’Plot created by Amar Partap Singh’)
Line-styles:
Make the same plot as above, but rather than displaying the
graph as a curve, show the unconnected data points. To
display the data points with small circles, use plot(x,y,’o’).
Now combine the two plots with the command
plot(x,y,x,y,’o’) to show the line through the data points as
well as the distinct data points.
Line-styles:
Make the same plot as above, but rather than displaying the
graph as a curve, show the unconnected data points. To
display the data points with small circles, use plot(x,y,’o’).
Now combine the two plots with the command
plot(x,y,x,y,’o’) to show the line through the data points as
well as the distinct data points.

ANSWER:
 plot(x,sin(x),x,sin(x),’o’)
 xlabel(’x’), ylabel(’sin(x)’)
An exponentially decaying sine plot:
Plot y = e−0.4x sin x, 0 ≤ x ≤ 4π, taking 10, 50, and 100 points
in the interval. [Be careful about computing y.You need array
multiplication between exp(-0.4*x) and sin(x).
An exponentially decaying sine plot:
Plot y = e−0.4x sin x, 0 ≤ x ≤ 4π, taking 10, 50, and 100 points in the interval.
[Be careful about computing y.You need array multiplication between exp(-
0.4*x) and sin(x).

ANSWER:
 x=linspace(0,4*pi,10); % with 10 points
 y=exp(-.4*x).*sin(x);
 plot(x,y)
 x=linspace(0,4*pi,50); % with 50 points
 y=exp(-.4*x).*sin(x);
 plot(x,y)
 x=linspace(0,4*pi,100); % with 100 points
 y=exp(-.4*x).*sin(x);
 plot(x,y)
Space curve:
Use the command plot3(x,y,z) to plot the circular helix x(t)
= sin t, y(t) = cost, z(t) = t, 0 ≤ t ≤ 20.
Space curve:
Use the command plot3(x,y,z) to plot the circular helix x(t)
= sin t, y(t) = cost, z(t) = t, 0 ≤ t ≤ 20.

ANSWER:
 t=linspace(0,20,100);
 plot3(sin(t),cos(t),t)
On-line help:

 Type help plot on the MATLAB prompt and hit return.

 If too much text flashes by the screen, type more on, hit return, and then
type help plot again.

 This should give you paged screen output.

 Read through the on-line help.

 To move to the next page of the screen output, simply press the spacebar.
Log scale plots:
The plot commands semilogx, semilogy, and loglog, plot the x-
values, the y-values, and both x- and y-values on a log10 scale,
respectively. Create a vector x = 0:10:1000. Plot x vs. x3 using the
three log scale plot commands. [Hint: First, compute y=x.^3 and
then, use semilogx(x,y) etc.]
Log scale plots:
The plot commands semilogx, semilogy, and loglog, plot the x-
values, the y-values, and both x- and y-values on a log10 scale,
respectively. Create a vector x = 0:10:1000. Plot x vs. x3 using the
three log scale plot commands. [Hint: First, compute y=x.^3 and
then, use semilogx(x,y) etc.]

ANSWERS:
 x=0:10:1000;
 y=x.^3;
 semilogx(x,y)
 semilogy(x,y)
 loglog(x,y)
Overlay plots:
Plot y = cos x and z = 1− x2/2 + x4/24 for 0 ≤ x ≤ π on the
same plot.

ANSWER:
 x=linspace(0,pi,100);
 y=cos(x); z=1-x.^2/2+x.^4/24;
 plot(x,y,x,z)
 plot(x,y,x,z,’--’)
 legend(’cos(x)’,’z’)
Overlay plots:
Plot y = cos x and z = 1− x2/2 + x4/24 for 0 ≤ x ≤ π on the
same plot.

ANSWER:
 x=linspace(0,pi,100);
 y=cos(x); z=1-x.^2/2+x.^4/24;
 plot(x,y,x,z)
 plot(x,y,x,z,’--’)
 legend(’cos(x)’,’z’)

You might also like