Notes 21 25

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

1.4.

SOME DEFINITIONS 21

[powers@darrow2-p ame.20214]$ /usr/bin/time -p a.out


6000 3.6000000E+07
real 0.46
user 0.40
sys 0.05
[powers@darrow2-p ame.20214]$
The execution of this particular code was achieved in

tFortran ∼ 1.42 s. (1.7)

The MATLAB code, available in matrix.m, is given by


tic
n = 5*10^3;
for i=1:n
for j=1:n
x(i,j) = i*j;
end
end
n
x(n,n)
toc
The MATLAB commands tic and toc are for timing the code. Relative to the Fortran code,
the MATLAB code has a similar, but simpler structure and syntax. However, the execution of
this particular code was achieved in a much longer time:

tMATLAB ∼ 12.724 s. (1.8)

It must be emphasized that these numbers are typical, but unreliable. The machine may
have been performing different background tasks. Also, one could use optimizing Fortran
compilers to improve Fortran execution time. The main reason for the fast execution of the
Fortran program is that it was compiled prior to execution, whereas the MATLAB program,
using a so-called interpreted language was not. All things equal, compiled languages are typ-
ically much faster than interpreted languages. However, in most cases, interpreted languages
have been designed to be more user-friendly. Note that it is possible to use compiled versions
of MATLAB to improve its execution time significantly.

1.4 Some definitions


Here are a few key concepts and terms associated with computer systems:
• Bits and bytes are the representation of data by combinations of 0 and 1. A byte is
usually 8 bits. Most common machines today are based on a 64-bit architecture.

CC BY-NC-ND. 29 April 2021, J. M. Powers.


22 CHAPTER 1. OVERVIEW

• Central Processing Unit (CPU) is, in colloquial terms, the main brain of the computer.
• One of the more important memory devices is Random Access Memory (RAM). This
memory is usually transient in that it is only active when the machine is turned on.
It is not used for permanent storage of files. Permanent storage is achieved via hard
drives, memory sticks, etc.
• A terminal is a device which allows direct input and output communications between
a user and the CPU of a machine. Terminals come in many flavors, including virtual.
• Software is an imprecise term which describes programs which allow interaction be-
tween the user and the computer. High level software is often more user-friendly and
has several layers of abstraction between the user and CPU. Low level software has
fewer levels of abstraction, is often more difficult to understand, but often tightly linked
to the CPU architecture. Microsoft Excel is a high level software tool. MATLAB is
somewhat lower level, but still at a high level. Fortran is at a lower level still, but in
many ways a high level language. So called assembly language is much closer to the
machine, and is certainly a low level software tool.
• A compiler, such as ifort, is a tool to convert a high level language such as Fortran
into a fast executable binary low level language usable by the machine. Use of com-
piled languages is highly advantageous for large scale problems in scientific computing
because of the speed of execution. For small scale problems, non-compiled interpreted
languages, such as MATLAB, are advantageous because of their relative simplicity of use.
• A text editor allows the user to alter the source code. Text editors typically refer to
tools which give the user access to the entire ASCII (American Standard Code for
Information Interchange) text of the file and avoid use of hidden and/or binary text.

1.5 Introduction to engineering plotting with MATLAB


Well done plots are an important part of communication for engineers. One should take care
to try to adhere to some general plot design principles:
• Use font sizes which are close to those of the printed text. Most software tools today,
unfortunately, use default font sizes which are too small for printed text; occasionally
they are too large. Whatever the case, the text on your plot should have a font size
very similar to that of your main text.
• Label your axes; include units when appropriate.
• In axes labels and text, use fully formatted mathematical symbols. For example, if the
proper symbol is φ, use it instead of “phi.” Or if the proper symbol is y1 , do not
substitute y 1 or y1 or y1. The same holds for units; use as a label ρ mkg3 , not “rho
(kg/m3).”

CC BY-NC-ND. 29 April 2021, J. M. Powers.


1.5. INTRODUCTION TO ENGINEERING PLOTTING WITH MATLAB 23

• If there is more than one curve, be sure the reader knows which curve corresponds to
which case. There are a variety of ways to achieve this. The best is probably to use
combinations of solid lines, dashed lines, dotted lines, etc. Different lines can also be
assigned different colors, but one should recognize that sometimes the user will print
files with black and white printers, obscuring the meaning. Also, some colors are hard
to discern on varieties of computer screens and printers.

• Generally, reserve point markers like small circles or dots for cases where

– The data comes from a finite, discrete set of experimental observations, or


– The data comes from a finite, discrete set of computational runs.

Often it is best not to “connect the dots” especially if there is no reason to believe that
another data point, if sampled, would lie exactly on the interpolating curve.

• Generally, reserve smooth curves with no small circles or dots for cases where

– The curve is representing a continuous mathematical function such as y = x2 +


2x + 1; Indeed, one must sample a discrete number of points to generate the plot,
but the underlying function is continuous.
– There is good reason to believe that the discrete data points are actually samples
of a continuous function. We may not know the function, but when we continue
to sample, we find the continuity assumption holds.

• Use linear, log-linear, and log-log scaled plots as appropriate for the data at hand.
Sometimes linear scales hide the true nature of the data, and sometimes they reveal it.
The same can be said for log-linear and log-log scales. Be sure to examine your data,
and choose the most revelatory scaling.

1.5.1 A bad MATLAB plot.


Let us say we have the following data to plot:

x y
0 1
2 4
3 8
4 7
7 10
10 19

MATLAB defaults typically produce faulty plots. The following MATLAB script is an example:

CC BY-NC-ND. 29 April 2021, J. M. Powers.


24 CHAPTER 1. OVERVIEW

x = [0,1,3,4,7,10];
y = [2,4,8,7,10,19];
plot(x,y)
This script generates the plot of Fig. 1.2. Figure 1.2 has the following problems:

20

18

16

14

12

10

2
0 1 2 3 4 5 6 7 8 9 10

Figure 1.2: Badly formatted plot of some data.

• The numbers on the axes are much smaller than the font size of the surrounding text.
This problem is pervasive when MATLAB default values are used.
• The axes are not labeled.
• The raw data should be indicated with distinct points. Though optional, it is probably
best not to connect them.

1.5.2 A better MATLAB plot


A few simple changes to the MATLAB script can improve the plot dramatically:
x = [0,1,3,4,7,10];
y = [2,4,8,7,10,19];
plot(x,y,’o’),...
xlabel(’x’,’FontSize’,24),...
ylabel(’y’,’FontSize’,24);...
axis([0,12,0,25]),...
set(gca,’FontSize’,20)
This script generates the plot of Fig. 1.3. Here the axes were expanded, the data were plotted
as individual points, and the font size was increased.

CC BY-NC-ND. 29 April 2021, J. M. Powers.


1.5. INTRODUCTION TO ENGINEERING PLOTTING WITH MATLAB 25

25

20

15
y

10

0
0 2 4 6 8 10 12
x

Figure 1.3: Properly formatted plot of some data.

1.5.3 Another bad MATLAB plot


Let us say we want to plot on a log-log scale the two continuous curves
y = x2 , y = x3 , x ∈ [0.01, 1000].
Shown next is a MATLAB script which generates a bad plot:
clear;
n = 1000;
xmin = 0.01;
xmax = 1000;
dx = (xmax-xmin)/(n-1);
for i=1:n
x(i) = xmin+(i-1)*dx;
y1(i) = x(i)^2;
y2(i) = x(i)^3;
end
loglog(x,y1,’bo’,x,y2,’bo’)
This script generates the plot of Fig. 1.4. Figure 1.4 has the following problems:
• The underlying curves are continuous, but only discrete points have been plotted.
• It is difficult to distinguish the two curves, especially for small x.
• The numbers on the axes are much smaller than the font size of the surrounding text.
• The axes are not labeled.

CC BY-NC-ND. 29 April 2021, J. M. Powers.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy