NC Lab 2
NC Lab 2
EXPERIMENT 02
Software Tool:
• MATLAB installed on PCs
Background Knowledge:
MATLAB (short for MATrix LABoratory) is a platform (program) organized for optimized
engineering programming and scientific calculations. The MATLAB program implements
the MATLAB programming language and provides an extensive library of predefined
functions and make technical programming tasks easier and more efficient. MATLAB has
incredibly large variety of functions and toolkits with more functions and various
specialties
MATLAB is a very powerful and well-known software package that is used in science and
engineering disciplines, for numerical computation, data analysis, and graphical
visualization. It is available in almost all platforms such as personal computers, and
workstations running under several operating systems.
MATLAB contains a large collection of built-in functions and commands that are used in an
interactive mode, when you are in the command window. As soon as the name of a function
or a command is typed at the prompt in the command window, with the proper syntax, the
answer is displayed immediately. But there are two other windows, the edit window, and
graphics window, which will be discussed later.
The software package is designed to use additional sets of functions that are more
applicable disciplines such as control systems, digital signal processing, communications
engineering, and image processing. There are more than 20 sets known as “toolboxes” (e.g.,
control toolbox, digital signal processing toolbox, communication toolbox, and image
processing toolbox). All of them run under MATLAB and implement the functions based on
matrix manipulation of numerical data, and that is why the software is called MATLAB
(matrix laboratory).
Numerical Analysis
MATLAB allows us to construct new functions using the enormous number of built-in
functions, commands, and operations in MATLAB and in the many toolboxes, without
having to know how to compile, link, load, and create executable code, because MATLAB
uses its own language to carry out all these steps, which are invisible to the user. It carries
out the steps and gives the answer very fast.
2D plotting:
Line Plots
The plot function creates simple line plots of x and y values.
Get
x = 0:0.05:5;
y = sin(x.^2);
figure
plot(x,y)
Bar Plots
The bar function creates vertical bar charts. The barh function creates horizontal bar
charts.
Get
x = -2.9:0.2:2.9;
y = exp(-x.*x);
bar(x,y)
Stairstep Plots
The stairs function creates a stairstep plot. It can create a stairstep plot of Y values only or a
stairstep plot of x and y values.
Get
x = 0:0.25:10;
y = sin(x);
stairs(x,y)
Numerical Analysis
Errorbar Plots
The errorbar function draws a line plot of x and y values and superimposes a vertical error
bar on each observation. To specify the size of the error bar, pass an additional input
argument to the errorbar function.
Get
x = -2:0.1:2;
y = erf(x);
eb = rand(size(x))/7;
errorbar(x,y,eb)
Polar Plots
The polarplot function draws a polar plot of the angle values in theta (in radians) versus
the radius values in rho.
Get
theta = 0:0.01:2*pi;
Numerical Analysis
rho = abs(sin(2*theta).*cos(2*theta));
polarplot(theta,rho)
Stem Plots
The stem function draws a marker for each x and y value with a vertical line connected to a
common baseline.
Get
x = 0:0.1:4;
y = sin(x.^2).*exp(-x);
stem(x,y)
Scatter Plots
Numerical Analysis
Use optional arguments to the scatter function to specify the marker size and color. Use
the colorbar function to show the color scale on the current axes.
Get
scatter(Height,Weight,20,Systolic)
xlabel('Height')
ylabel('Weight')
colorbar
Numerical Analysis
3D plotting:
This example shows how to create a variety of 3-D plots in MATLAB®.
Mesh Plot
The mesh function creates a wireframe mesh. By default, the color of the mesh is
proportional to the surface height.
Get
z = peaks(25);
figure
mesh(z)
Surface Plot
The surf function is used to create a 3-D surface plot.
Get
surf(z)
Numerical Analysis
Contour Plot
The contour function is used to create a plot with contour lines of constant value.
Get
contour(z,16)
Numerical Analysis
Quiver Plot
The quiver function plots 2-D vectors as arrows.
Get
x = -2:.2:2;
y = -1:.2:1;
[xx,yy] = meshgrid(x,y);
zz = xx.*exp(-xx.^2-yy.^2);
[px,py] = gradient(zz,.2,.2);
quiver(x,y,px,py)
xlim([-2.5 2.5]) % set limits of x axis
The slice function displays data at planes that slice through volumetric data.
Get
x = -2:.2:2;
y = -2:.25:2;
z = -2:.16:2;
[x,y,z] = meshgrid(x,y,z);
v = x.*exp(-x.^2-y.^2-z.^2);
slice(x,y,z,v,xslice,yslice,zslice)
xlabel('x')
ylabel('y')
zlabel('z')
Multiple Plots:
In MATLAB, the subplot command is used to create axes in tiled positions within a figure
window. It divides the current figure into a grid of rows and columns, and then specifies
the position where subsequent plots or graphics objects will be drawn.
Example:
Numerical Analysis
subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')
subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')
subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')
subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')
Numerical Analysis
Lab Tasks:
Numerical Analysis