Docsity

Creating A 2d Plot-Computational Physics-Lecture Slides, Slides of Computational Physics

Toggle sidebar
bg1
Creating a 2D Plot
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y,'LineWidth',3)
Now label the axes and add a title. The characters \pi create the symbol .
See text strings for more symbols:
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine
Function','FontSize',12)
For example, these statements use the colon operator to create a vector
of x values ranging from 0 to 2, compute the sine of these values, and
plot the result:
Graph is on
next page
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Creating A 2d Plot-Computational Physics-Lecture Slides and more Slides Computational Physics in PDF only on Docsity!

Creating a 2D Plot

x = 0:pi/100:2pi; y = sin(x); plot(x,y,'LineWidth',3)* Now label the axes and add a title. The characters \pi create the symbol. See text strings for more symbols: xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function','FontSize',12)

For example, these statements use the colon operator to create a vector of x values ranging from 0 to 2, compute the sine of these values, and plot the result:

Graph is on

next page

Creating a 2D Plot

Result of the example

Plotting Elementary Functions

Resulting graph

Specifying the Color and Size of Markers

You can also specify line characteristics using graphics properties: LineWidth -- Specifies the width (in points) of the line. MarkerEdgeColor -- Specifies the color of the marker or the edge color for filled markers (circle, square, diamond, pentagram, hexagram, and the four triangles). MarkerFaceColor -- Specifies the color of the face of filled markers. MarkerSize -- Specifies the size of the marker in units of points.

t = 0:.4:50; alpha =.055; y = exp(-alphat).sin(.5t) plot(t,y,'--rs','LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)*

Example: Prog3.m

Graph is on

next page

Plotting commands

PLOT(X,Y) plots vector Y versus vector X. TITLE('text') adds text at the top of the current plot. XLABEL('text') adds text beside the X-axis on the current axis. YLABEL('text') adds text beside the Y-axis on the current axis. GRID, by itself, toggles the major grid lines of the current axes. GTEXT('string') displays the graph window, puts up a cross-hair, and waits for a mouse button or keyboard key to be pressed. SUBPLOT(m,n,p), or SUBPLOT(mnp),

breaks the Figure window into an m-by-n matrix of small axes. STEM(Y) stemDiscrete sequence or "stem" plot. STEM(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value. SEMILOGX(...) is the same as PLOT(...), except a logarithmic (base 10) scale is used for the X-axis. SEMILOGY(...) is the same as PLOT(...), except a logarithmic (base 10) scale is used for the Y-axis..

Multiple Graphs

Two ways to make multiple plots on a single graph are illustrated here. Another way is with the hold command. The command hold freezes the current graphics screen so that subsequent plots are superimposed on it. Entering hold again releases the "hold". The commands hold on and hold off are also available. One can override the default line types and point types. For example,

% prog3.m t = 0: 0.01: 2pi; y1 = sin(t); y2 = sin(2t); y3 = sin(4t); plot(t, y1, '--', y2, ā€˜', y3, '+')**

Graph is on

next page

Plotting colors and line styles

Colors Line Styles y yellow. point M magenta o circle C cyan x x-mark R red + plus G green - solid B blue * star W white : dotted K black -. Dashdot -- dashed More mark types are; square(s), diamond(d), up-triangle(v), down- triangle(^), left-triangle(<), right-triangle(>), pentagram(p), hexagram(h) See also help plot for more line and mark color.

Subplot

The command subplot can be used to partition the

screen so that up to four plots can be viewed

simultaneously. See help subplot.

Example for use of subplot:

% prog4.m % Line plot of a chirp x = 0 : 0.05 : 5; y = sin(x.^2); subplot(2,1,1), plot(x,y); % Bar plot of a bell shaped curve x = -2.9 : 0.2 : 2.9; subplot(2,1,2), bar(x,exp(-x.x));*

Graph is on

next page

Various Plotting Functions:

 2 dimensions line plots (x vs. y),  filled plots,  bar charts,  pie charts,  parametric plots,  polar plots,  contour plots,  density plots,  log axis plots,  surface plots,  parametric plots in 3 dimensions  and spherical plots.

To preview some of these capabilities and others as well, enter the command demos.

MATLAB supports many types of graph and surface plots:

Line Plots of 3-D Data

The 3-D analog of the plot function is plot3. If x, y, and z are three vectors of the same length, plot3(x,y,z) generates a line in 3-D through the points whose coordinates are the elements of x, y, and z. Then it produces a 2-D projection of that line on the screen. For example, these statements produce a helix.

% prog5.m t = 0:pi/50:10pi; plot3(sin(t),cos(t),t,'LineWidth',3) axis square; grid on*

Graph is on

next page

3-D Plots

MATLAB Commands for Mesh and Surface

meshgrid: generate X and Y arrays for 3-D plots mesh: draws a wireframe mesh surf: plot a 3-D shaded surface colorbar: add a color bar tool patch: creating patch graphics objects hidden: remove hidden lines from mesh plot

Three-D meshgrid

To illustrate the use of meshgrid, consider the sin(r)/r or sinc function. To evaluate this function between -8 and 8 in both x and y, you need pass only one vector argument to meshgrid, which is then used in both directions. [X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; The matrix R contains the distance from the center of the matrix, which is the origin. Adding eps prevents the divide by zero (in the next step) that produces Inf values in the data. Forming the sinc function and plotting Z with mesh results in the 3-D surface. Z = sin(R)./R; mesh(X,Y,Z)

Mesh and Surface

Three-D meshgrid

Integration by MATLAB

We seek to compute:

If m is a point between a and b than:

The value of the integral is the area under curve



b a

f ( x ) dx

   ļ€«ļƒ²

b m

m a

b a

f ( x ) dx f ( x ) dx f ( x ) dx