Computer Applications in Metallurgical Industries
Computer Applications in Metallurgical Industries
Faculty of Engineering
Mining, Petroleum and Metallurgy Department
4th Year Metallurgy
Computer Applications in
Metallurgical Industries
LECTURE 2
By Dr. Ahmed Hatem Al-Khoribi
• Working with Files
• MATLAB uses several types of files that enable
you to save programs, data, and session results.
MATLAB function files and program files are
saved with the extension .m, and thus are called
M-files. MAT-files have the extension .mat and
are used to save the names and values of
variables created during a MATLAB session.
• Note:
• 1e-2=1*10-2
• 0.8e1=0.8*101
• 20e3=20*103
• Stem, Stairs, and Bar Plots
• MATLAB has several other plot types that are
related to xy plots. These include the stem,
stairs, and bar plots. Their syntax is very
simple, namely, stem(x,y), stairs(x,y), and
bar(x,y).
• Consider the following session:
>> u = [1, 2, 3, 4];
>> k = [4, 6, 10, 3];
>> bar(u,k)
• Separate y Axes
• The plotyy function generates a graph with two y
axes. The syntax plotyy (x1,y1,x2,y2) plots y1
versus x1 with y axis labeling on the left, and
plots y2 versus x2 with y axis labeling on the
right.
• The syntax plotyy(x1,y1,x2,y2,’type1’,’type2’)
generates a ‘type1’ plot of y1 versus x1 with y
axis labeling on the left, and generates a ‘type2’
plot of y2 versus x2 with y axis labeling on the
right.
• For example, plotyy(x1,y1,x2,y2,‘plot’,‘stem’) uses
plot(x1,y1) to generate a plot for the left axis, and
stem(x2,y2) to generate a plot for the right axis.
To see other variations of the plotyy function,
type help plotyy.
• grid and axis Commands
• The grid command displays gridlines at the
tick marks corresponding to the tick labels.
You can use the axis command to override the
MATLAB selections for the axis limits. The
basic syntax is axis([xmin xmax ymin ymax]).
• This command sets the scaling for the x and y
axes to the minimum and maximum values
indicated. Note that, unlike an array, this
command does not use commas to separate
the values.
• The axis command has the following variants:
• axis square selects the axes’ limits so that the
plot will be square.
• axis equal selects the scale factors and tick
spacing to be the same on each axis. This
variation makes plot(sin(x),cos(x)) look like a
circle, instead of an oval.
• axis auto returns the axis scaling to its default
auto scaling mode in which the best axes’
limits are computed automatically.
• trapz Command
• trapz command can be used to apply trapezoidal
rule to a curve in order to calculate the area
under it. Consider the following session:
>> x = [7,8,10,15,50];
>> T = 2*x;
>> Area_under_curve = trapz(x,T)
Area_under_curve =
2451
• You cannot directly specify a function to integrate
with the trapz function; you must first compute
and store the function’s values ahead of time in
an array.
• polyxpoly Command
• polyxpoly command can be used to find
intersection points of two curves and return their
x and y coordinates. Consider the following
session:
>> x1 = 1:0.1:9;
>> y1 = 20*x1;
>> x2 = 1:0.1:9;
>> y2 = 2*x2+100;
>> [x,y] = polyxpoly(x1,y1,x2,y2)
x=
5.5556
y=
111.1111
• Plotting Polynomials
• We can plot polynomials more easily by using the
polyval function. The function polyval(p,x) evaluates
the polynomial p at specified values of its independent
variable x. For example, to plot the polynomial 3x5 +
2x4 - 100x3 + 2x2 - 7x + 90 over the range - 6 x 6
with a spacing of 0.01, you type
>> x = -6:0.01:6;
>> p = [3,2,-100,2,-7,90];
>> plot(x, polyval(p,x)), xlabel(‘x’), ylabel(‘f(x)’)
OR
>> x = -6:0.01:6;
>> p = [3,2,-100,2,-7,90];
>> f = polyval(p,x);
>> plot(x, f), xlabel(‘x’), ylabel(‘f(x)’)
• Exporting/Saving Figures
• If you want to save the figure in a format that
can be used by another application, such as
the standard graphics file formats BMP, TIFF,
or EPS, perform these steps.
1. Select Export Setup from the File menu. This
dialog provides options you can specify for the
output file, such as the figure size, fonts, line
size and style, and output format.
2. Select Export from the Export Setup dialog. A
standard Save As dialog appears.
3. Select the format from the list of formats in
the Save As type menu. This selects the
format of the exported file and adds the
standard file name extension given to files of
that type.
4. Enter the name you want to give the file, less
the extension.
5. Click Save.