Introduction To Matlab For Experimental Physics
Introduction To Matlab For Experimental Physics
Data analysis and representation are vital steps in any experimental exercise.
They lend meaning to the experiment and provide insight leading to a more
fundamental understanding of the underlying concept. Intelligent data processing
and representation also help the experimenter in re-designing the experiment for
increased accuracy and precision. Clever thinking may even encourage her to
adapt and tailor the procedural steps to elicit some otherwise hidden facet.
The present write-up serves as a rst introduction to Matlab. Students who are
not familiar with Matlab, or even with the computer, need not to worry. We will
proceed slowly, allowing everyone to familiarize and acclimatize with the culture
of computing. Luckily, Matlab is a highly user-friendly and interactive package
that is very easy to learn. Furthermore, subsequent laboratory sessions will give
all of us ample opportunity to practice Matlab.
It is important that every student independently works through all the examples
1
given in this hand-out and attempts all challenge questions. These challenge
questions are labelled with the box Q .
3. Curve tting
You can start Matlab by double-clicking on the Matlab icon located on the Desk-
top. The Matlab environment launches showing three windows. On the top left
is the directory window, showing the contents of the working directory. On the The Matlab
bottom left is the history window, displaying your recently executed commands. icon
On the right is the larger-sized command window. This is where you will type in
your commands and where the output will be displayed.
Now let us get started with the exercise. The simplest calculation is to add two
numbers. In the command window, type
2+3
What do you see? Indeed, 5, displayed as the answer (ans) in the command
window. If we terminate the command with the semi-colon, 2 + 3;
the output 5 will not be displayed.
5^2
and verify if you get the correct answer.
2
1.1.2 Creating Vectors and Matrices
Matlab is centred around the manipulation of matrices. In fact, the word Matlab
is acronym for MATrix LABoratory. Let us generate a simple list of numbers,
called a vector. This vector comprises all even numbers greater than or equal to
2 and less than 20. We call this vector evenlist.
evenlist = [2 4 6 8 10 12 14 16 18]
The vector will be displayed, all entries ranging from 2 to 18 lined up in a row.
We have just created a row vector. A compact way of creating evenlist would be
to write,
evenlist2 = 2:2:18
with the rst 2 representing the rst element, the second 2 representing the
step size and the 18 showing the last element of the vector. Indeed evenlist and
evenlist2 are equal. At some later stage, if we want to recall what the vector
evenlist2 is, we just retype the label.
evenlist2
How do we make a column vector, where all the entries are arranged vertically
instead of horizontally? We can use the semicolon as a delimiter among rows.
evenlist4 = evenlist3';
a = [2 4 6; 1 3 5; 7 9 11];
The above operation generates a matrix of order 3 3.
3
2 4 6
1 3 5 : (1.1)
7 9 11
a ^ 2;
This performs the product of the matrices as a a and the resulting matrix is,
50 74 98
40 58 76 : (1.2)
100 154 208
a. ^ 2
This operation just takes the square of each entry as shown,
4 19 36
1 9 25 : (1.3)
49 81 121
By typing a' in the command window, we get the transpose of the generated
matrix a as,
2 1 7
4 3 9 : (1.4)
6 5 11
To understand how MATLAB interprets the forward slash / and the backward
slash n, we try some simple commands.
By typing,
a=4/2
4
We obtain the answer 2, the result of a division operation. That is, the number
on the left hand side of the forward slash is being divided by the number on the
right hand side. On the other hand, if we type,
b=4n2
The answer is 0.5, which clearly indicates that the number on the right hand side
is being divided by the number on the left hand side.
For loops are very powerful when we want to continuously update elements of
any vector. The typical structure of a for loop is
for (condition)
statements
end
a = [1 2 3 4 5 6 7 8 9 10]
A row vector stores information in the following way,
1 2 3 4 5 6 7 8 9 10
a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
If we now want to add +1 to all the elements of a, we can write a for loop,
for k = 1:10
a(k) = a(k) + 1;
end
Matlab now updates every element of a by +1. The new array will look like,
5
2 3 4 5 6 7 8 9 10 11
a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Note that a for statement needs an accompanying end statement marking the
end of the statements that are being executed.
Now suppose, we wish to select some entries from a generated row or column
vector or from matrices. Dene the row vector,
a = [2 4 6 8 10 12 14 16 18 20]
We want to extract the entries from column 3 to 7. We write,
b = a(3:7);
The colon operator will extract the entries from column 3 to 7, thus giving us
the output,
b = [6 8 10 12 14]
Similar procedure can be repeated with a column vector.
We dene a matrix by,
a = [5 8 9; 2 4 6; 1 3 5]
5 8 9
2 4 6 : (1.5)
1 3 5
The order of the matrix a is 3 3. Starting from the easiest concept of selecting
one single entry from a matrix, we will move on to select the whole row or column
of that matrix. Suppose we want to select the entry 4 in the above matrix. We
look at the position of that specied entry inside the matrix. The element is
located in the second row and second column of the matrix.
6
b =a(2,2)
This command takes the value from second row and second column of a and
saves it in b. The displayed output is 4.
To select a complete row or column of any matrix we have to use the colon
operator, \:" which means that all entries of that specied row or column will
be selected. For example,
a(2,:)
displays all the entries of the second row of the matrix, and
a(:,2)
displays all the entries in the second column of the matrix a.
If we write d=a(:,:) in the command window, we get the complete matrix again,
i.e., we have selected all the rows and all the columns.
7
Let's label our tri-layered object as F.
Let us rst generate the object F. We pre-allocate some space in the memory by
the command,
F = zeros(3,3,3);
Now, in all the three layers we have to initiate the appropriate values. For example,
F(:,:,2)= F(:,:,1) . ^ 2 ;
generates the squares of the rst layer into the second layer. We can view the
layer by writing,
F(:,:,2)
and the displayed matrix is, indeed,
1 4 9
16 25 36 : (1.10)
49 64 81
F(:,:,3)=F(:,:,1) . ^ 3
To have a look at the generated data we type,
F(:,:,3)
yielding,
8
1 8 27
64 125 216 : (1.11)
343 512 729
If we wish to see the matrix element in the second row, third column and in the
second layer, we use the command,
a= F(2,3,2)
F = zeros(3,3,3);
F(:,:,1)=[1 2 3;4 5 6; 7 8 9];
for k = 1:2
F(:,:,k+1) = F(:,:,1) . ^ (k+1);
end
9
Yet another alternative approach of creating F, is outlined below. Understand
how these options of creating F work.
F = zeros(3,3,3);
p = 1:1:9;
F(:,:,1) = reshape(p,3,3)';
for k = 1:3
for m = 1:3
F(k,m,2) = F(k,m,1). ^ 2;
F(k,m,3) = F(k,m,1). ^ 3;
end
end
Graphs are extremely important in experimental physics. There are three impor-
tant uses of graphs [1].
First, with the help of graphs we can easily determine slopes and intercepts.
Second, they act as visual aids indicating how one quantity varies when the
other is changed, often revealing subtle relationships. These visual patterns
also tell us if there exist conditions under which simple (linear) relationships
break down or sudden transitions take place.
10
1.2.1 Plotting Basics
Let's consider the seminal experiment [4] performed by Millikan in 1917 for the
calculation of the value of Planck's constant h. This experiment, based on the
photoelectric eect, also veried Einstein's earlier predictions that light is com-
posed of discrete particles called photons. Millikan's original apparatus as well as
our simplied schematic is shown in Figure 1.2.
Light
P Q
(a) (b)
Figure 1.2: (a) Millikan's experimental setup for the determination of h (repro-
duced from [4]) and (b) the simplied illustration of Millikan's experiment.
The experiment works as follows. Monochromatic light (of a xed wavelength and
frequency) falls on a freshly cut surface of sodium metal attached to the electrode
P . As a result electrons are ejected from the metal surface and because of their
ejection momentum, they cruise their way to the electrode Q. These electrons
constitute a photocurrent that is measured by the ammeter. But this motion
is opposed by a voltage that makes Q more negative than P . As Q becomes
more and more negative, fewer electrons reach the electrode and the current
diminishes. At a certain potential dierence, called the stopping voltage Vs , the
current nally approaches zero. Millikan repeated the experiment for various light
sources. One such set of his readings is listed in Table 1.1.
Now let's plot Vs as a function of the frequency f , keeping Vs on the vertical and
f on the horizontal axis. The rst step is to input the data in the form of vectors.
11
Stopping voltage Vs (V) 2:100 1:524 1:367 0:9478 0:3718 +0:3720
Wavelength (
A) 5466 4339 4047 3650 3126 2535
Table 1.1: Millikan's readings for the stopping voltage as a function of the wave-
length of the incident length; results extracted from [4].
c=3e8;
f=c./wlength;
Here c is the speed of light, c=3e8; is a compact way of writing 3 108 . Also
note the pointwise division of the speed of light by the wavelength, using the
familiar \." operator. The graph is achieved using the command,
gure; plot(f,vs);
and the horizontal and vertical axes are labelled using,
xlabel(`frequency f (Hz)');
ylabel(`stopping voltage Vs (V)');
The resulting graph is shown in Figure 1.3(a). The plot is a solid black line
joining the individual data points, even though the points themselves are not
distinguished. These points can in fact be highlighted using symbols such as \o",
\+" and \". The colours can also be adjusted. For example, to plot a solid
red-coloured line with circles for the data points, we use the command,
gure; plot(f,vs,`r-o');
Furthermore, if it is required to display the data points only, suppressing the line
that connects between these points, we type,
gure; plot(f,vs,'ro');
This latter plot, shown in Fig. 1.3(c) in fact, represents a more justiable picture
of the experimental data. This is because the lines drawn in (a) and (b) represent
more than what the data warrants: the lines show that the frequency and the
12
.5 .5
0 0
-1.0 -1.0
-1.5 -1.5
-2 -2
(a) (b)
-2.5 -2.5
5 6 7 8 9 10 11 12 5 6 7 8 9 10 11 12
14 14
frequency f (Hz) x 10 frequency f (Hz) x 10
.5
0
stopping voltage Vs (V)
-.5
-1.0
-1.5
-2
(c)
-2.5
5 6 7 8 9 10 11 12
14
frequency f (Hz) x 10
Figure 1.3: (a) Output from gure; plot(f,vs); a solid jagged line connects the
data points; (b) output from gure; plot(f,vs,`r-o'); a solid red line connects
the data points that are now highlighted; (b) output from gure; plot(f,vs,`ro');
showing just the data points.
stopping voltage have some kind of jagged relationship, something that is highly
likely. A more reasonable prediction is that the relationship is a straight line. In
the next section, we will discuss how to draw one such line, using the procedure
of least squares curve tting.
for 0 t 8.
Q 5. Draw a graph of the function,
y = x exp( x ) (1.15)
for 0 x 10.
Q 6. Biomedical engineers often design instrumentation to measure physio-
logical processes, such as blood pressure. To do this, they must develop mathe-
matical models of the process. The following equation is a specic case of one
model used to describe the blood pressure in the aorta during systole (the period
14
following the closure of the heart's aortic valve). The variable t represents time in
seconds and the dimensionless variable y represents the pressure the aortic valve,
normalized by a constant reference pressure.
8t
y (t ) = e sin (9:7t + ): (1.16)
2
It is also possible to plot multiple curves on the same gure. This is a highly useful
feature as you will soon realize. Consider for example, the load-line analysis
of electrical circuits. A voltage source V1 , having an internal resistance R1 is
connected to the load as shown in the Figure 1.5. This power supply produces
a xed voltage supplying current i1 to the load resulting in a potential drop V2
across the load.
Figure 1.5: A power supply with resistance R1 and the load are shown in a circuit.
15
An experimenter built the circuit shown in Figure 1.5. The current-voltage rela-
tionship approximated from the experiment was,
Let's suppose that we have a supply voltage of V1 =15 V and the resistance of the
supply is 30 Ohms. In the rst step, we write the equation of the circuit using
Kircho's Voltage Law.
V1 = i1 R1 + V2 ; (1.19)
which implies,
V1 i1 R1 V2 = 0 : (1.20)
Load line tells us how the current across the load changes as the voltage across
the load is changed. So, we write Equation 1.20 in terms of current as,
1 V
i1 = V + 1; (1.21)
R1 2 R1
which can be re-written after using the values as,
1
i1 = V + 0 : 5: (1.22)
30 2
From equations 1.18 and 1.22, it is dicult to calculate the values of i1 and V2
because of the exponential factor present in equation 1.18. But we can plot the
two curves individually and then overlap to nd the solution.
We will use Matlab to plot the load voltage V2 against the experimentally obtained
relation of current and the relation for current obtained from the rearrangement
of Kircho's Voltage Law. The point at which these two curves intersect gives
us the solution.
For the calculation of the values of current from the Equation 1.22, we write,
The load voltage can then be plotted against current by using the command,
16
gure; plot(V2,current exp,`b-o');
where we have used b for blue and o for circles. The output is shown in the
Figure 1.6 (a).
To plot the load voltage versus current using Equation 1.22, write,
where we have used g for green and + for plus sign. The output of the above
mentioned command is shown in Figure 1.6 (b).
Figure 1.6: (a) The load voltage V2 and the Current iexp . (b) The load voltage
V2 and the Current ith .
axis([0 20 0 1]);
The result is shown is Figure 1.7.To get the point of intersection press the Data
cursor button in the Figure window and then click at the point of intersection.
The value shown is the solution.
17
Figure 1.7: Overlaying of two plots and nding the point of intersection.
over the range x 2 [0; 3] and use the curves to nd the solution of the equation
x = cos x .
x
y = ae +b (1.27)
where a and b are constants. Sketch a curve of y versus x using arbitrary values
of a and b. Is it possible to obtain a straight line that represents this functional
relationship?
sin (t ) (1.28)
18
1 1
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0. 2 0. 2
0. 4 0. 4
0. 6 0. 6
0. 8 0. 8
(a) (b)
1 1
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
0.8
0.6
0.4
0.2
0. 2
0. 4
0. 6
0. 8
(c)
1
0 1 2 3 4 5 6 7 8 9 10
time t (s)
Figure 1.8: (a) Output from gure; plot(t1,x1,`g-o'); lower resolution graph; (b)
output from gure; plot(t2,x2,`b-v'); higher resolution graph; (b) output from
gure; plot(t1,x1,`g-o'); hold on; plot(t2,x2,`b-v'); whereby the two graphs
have been drawn on top of each other.
t1=0:1:10;
x1=sin(t1);
19
gure; plot(t1,x1,`g-o'); (for the subgure (a))
t2=0:.1:10;
x2=sin(t2);
gure; plot(t2,x2,`b-v'); (for the subgure (b))
However, these plots cannot be overlaid one on top of each other using the com-
mand gure; plot(t1,x1,`g-o',t2,x2,`b-v'); as t1 and t2 are essentially dierent
vectors. A way around this is to use the following set of commands.
x=-pi:pi/10:pi;
y=tan[sin(x)]-sin[tan(x)];
gure; plot(x,y,`- -rs',`LineWidth',2,`MarkerEdgeColor',`k',`MarkerFaceColor',`g',`MarkerSize',10);
y = tan[sin(x)] - sin[tan(x)]
20
a dashed line having red color and square markers,
Lines with dierent styles like solid, dashed and dotted etc. can be drawn with
marker types x, *, + and o. The color options include cyan, magenta, yellow,
black, red, green, blue and white with symbols c, m, y, k, r, g, b, w. There are
plenty of equations in Matlab for you to explore.
Consider, once again, Millikan's famous experiment for determining the Planck
constant. Observe Figure 1.3(c). Can we draw a straight line through these
points, not necessarily touching them? What could be the signicance of such a
line? In the present section, we will explore answers to this question.
0.5 -0.6
-0.8
0
-1
-0.5
-1.2
Vs / V
Vs / V
-1 -1.4
-1.6
-1.5
-1.8
-2
-2
-2.5 -2.2
5 6 7 8 9 10 11 12 5 5..5 6 6..5 7 7..5 8 8..5 9
14 14
f / Hz x 10 f / Hz x 10
Figure 1.10: (a) Data points from Millikan's experiment [4] with two possible
lines dening the functional relationship between f and Vs ; (b) magnied region
from the graph (a), closely showing the data points and the straight lines.
21
Figure 1.10(a) is a reproduction of the data points shown in Figure 1.3(c). How-
ever, in this graph we have also drawn two straight lines. Why straight lines?
Linear relationships occur naturally in numerous natural instances and that is
why they have become the scientist's favourite. Linear relationships are direct
manifestations of direct proportionality. If the variables x and y are directly pro- F = ma
portional (x / y ), an equal increase in x always results in an equal increase in (Newton's law)
y . Be it the extension of a spring when loaded with masses, the acceleration F = kx
of an object as it experiences a force or the magnetic eld that winds around a (Hooke's law)
current carrying conductor, linear relationships are ubiquitous. When these linear B = 0 NI
functions are drawn on paper (or on the computer screen), they become straight (Ampere's law)
lines.
The straight lines we have drawn in Figure 1.10(a) represent a kind of interpola-
tion. In the real experiment, we measure the variables, (xi ; yi ). In our case these
are frequency and stopping voltage. In a set of measurements, we have six pairs
of data points (x1 ; y1 ); (x2 ; y2 ); : : : ; (x6 ; y6 ). What if we want to determine the
stopping voltage for a frequency that was not used by Millikan? We could either
repeat his experiment with a light source with the desired frequency or estimate
using available data. In the latter case, we draw a straight line around the avail-
able measurements (xi ; yi ). This line negotiates data points not available to the
experimenter.
But what straight line do we actually draw? This is a matter of choice. For
example, we have drawn two lines in the Figure. The light colored line takes the
rst and the last data points as reference and connects these points; whereas
the dark colored line connects the mean (or the centre of gravity of the data) to
the end point. Both lines are dierent and at the outset, are equally suitable for
dening the linear relationship between the variables of interest.
Let's brie
y digress to see how we plotted, say, the red line. To plot a line, we
need an equation for the line. Given two points (x1 ; y1 ) and (x6 ; y6 ), a straight
line through these will be given by,
y y1 x x1
= ; (1.30)
y6 y1 x6 x1
and in our case (x1 ; y1 ) = (0:5488 1015 ; 2:1) and (x6 ; y6 ) = (1:1834
1015 ; 0:3720). (These numbers have been taken from the row vectors f and
vs.) After some basic arithmetic (also done in Matlab) we arrive at the following
22
equation for the red line,
where in our particular case y is the stopping voltage vs and x is the frequency
f . Similarly, the equation for the blue line was computed by rst calculating the
means of the x and y values. The resulting equation is,
yielding a line parallel to the rst, but displaced upwards. Figure 1.10(b) shows a
close-up of (a), revealing that these lines do not actually touch a majority of the
data points, they just graze within that region.
The graph has been plotted by using the following set of commands.
line1=3.895e-15*f-4.2375;
line2=3.895e-15*f-4.2018;
gure; plot(f,vs,`ro',f,line1,`g-',f,line2,`b-');
y = mx + c di
mxi + c
yi
xi
origin
Consider Figure 1.11 where a straight line has been drawn around a set of exper-
imentally measured data points (xi ; yi ). In this example we have N = 7 pairs of
23
measurements. The line is represented by the equation,
y = mx + c (1.33)
where m is the slope and c is the intercept. Of the many lines that can be
drawn, this particular line has a special property that we now investigate. If the
reading along the abscissa (x axis) is xi , the corresponding measurement along
the ordinate (y axis) is yi , but the line we have just drawn takes up the value,
mxi + c instead, which in general, is dierent from yi . This dierence
di = yi mxi c (1.34)
is called the residual or deviation. The special line we have drawn has the property
that it minimizes the sum of the squares of the deviations,
and hence the name least squares curve t. If the di 's are considered to be the
errors, the least squares curve t is the best t in the sense that it minimizes the
squares of the errors.
Q 10. Why do we minimize the sum squares of the residuals Ni=1 di2 instead
of the sum of the residuals Ni=1 di ?
There is an algorithmic procedure for deriving the equation for the least squares
t. The goal is to nd the parameters m and c that minimize the quantity S . The
minimum of S can be determined from elementary calculus. Take the derivative
of S , rst with respect to m and then with respect to c and put the derivatives
equal to zero,
N
X
@S
= 2 xi (yi mxi c) = 0 (1.36)
@m i =1
X N
@S
= 2 (yi mxi c ) = 0: (1.37)
@c i =1
24
Rearranging Equation 1.37, we obtain,
N
X
(yi mxi c) = 0
i =1
N
X N
X
yi m xi cN = 0
i =1 i =1
PN PN
yi m xi
=) c = i i
; (1.38)
N
N
X N
X
where : (1.39)
i i =1
The expression for c is inserted into Equation 1.36 and after some algebraic
manipulation,
N
X
xi (yi mxi c) = 0
i
N
X N
X N
X
(xi yi ) m xi2 c xi = 0
i i i
N
X N
X PN PN N
X
£ yi m xi ¤
(xi yi ) m xi2 i i
xi = 0
i i
N i
N
X N
X N N N
1 ¡X ¢¡X ¢ m ¡X ¢2
(xi yi ) m xi2 x y + x = 0;
i i
N i i i i N i i
(1.40)
25
merator and denominator of the above expression,
N
X N
X N
X
(xi yi ) Nx y = (xi yi ) ( yi )x
i i i
N
X
= yi (xi x ); and (1.45)
i
N
X N
X
xi2 Nx 2 = xi2 + Nx 2 2Nx 2
i i
N
X N
X
= xi2 + Nx 2 2x xi
i i
N
X N
X N
X
= xi2 + x2 2x xi
i i i
N
X
= (xi2 + x 2 2xxi )
i
N
X
= (xi x )2 : (1.46)
i
This tedious but fruitful exercise yields the following compact expression for the
slope of the least squares curve t,
PN
yi (xi x )
m = Pi N : (1.47)
i (xi x )2
Substituting the expression for m back into (1.39) we can determine the intercept,
c =y mx: (1.48)
Q 11. Prove that the least squares curve t passes through the centre of
gravity (x; y ) of the measured data.
Now we use Matlab to nd the least squares curve for Millikan's experimental
data. The commands that generate the best t line are given below.
numerator=sum(vs.*(f-mean(f)));
denominator=sum((f-mean(f)).^ 2);
m=numerator/denominator;
c=mean(vs)-m*mean(f);
The values are m = 3:9588 10 15 V/Hz and c = 4:2535 V. We can now
easily plot the least squares t, shown in Figure 1.12.
line3=m*f+c;
26
gure; plot(f,vs,`ro',f,line3,`g-');
0.5
- 0.5
Vs / V
-1
-1.5
-2
-2.5
5 -6 7 8 9 10 11 12
14
f / Hz x 10
Figure 1.12: Data points for Millikan's experiment and the least squares curve
t.
The straight line, in fact, has real physical value as well. For example, according
to Einstein's interpretation of the photoelectric eect, light is carried in the form
of small packets called photons. Corresponding to the frequency f , the photon
carries an energy hf , where h is Planck's constant. As light is shone on the
metal surface, a fraction of the energy called the work function W < hf is
absorbed by the metal surface. The ejected electron carries the energy dierence
hf W appearing as its kinetic energy. As the voltage Vs is made more and more
negative, the number of electrons reaching electrode Q diminishes with only the
more energetic electrons being able to overcome the opposing voltage. At the
stopping voltage, the maximum kinetic energy equals the voltage barrier. Given
a potential of Vs , the corresponding potential energy is eVs , e being the charge
of the electron. This description allows us to write the following equation,
eVs = hf W
¡h¢ ¡W ¢
Vs = f : (1.49)
e e
Comparing this with the least squares tted equation 1.33, we immediately rec-
27
ognize that the slope m is in fact an estimate of h=e and the intercept c is an
estimate of W=e . Using the slope and intercept from the best-t and a value of
e = 1:6022 10 19 C, the Planck constant calculates to h = 6:342 10 34 Js
and the work function to W = 6:814 10 19 J or 4:2535 eV.
The concept of curve tting can also be applied to the nonlinear data. Suppose
we route a sinusoidal ac voltage through a data acquisition system bringing it
into the computer. The hardware samples the voltage, acquiring one sample
every 50 ms and saves the rst 21 points. The time sampling information is
stored in the form of the row vector t where 0:5 s shows the separation between
two sample points..
t=0:0.05:1;
The voltage measurements made by the acquisition software are given by another
row vector v.
Note that size(t)=size(v). We are asked to t this data to a least squares curve,
a sinusoidal function. Our best t will be of the form,
where A is the amplitude, ! is the angular frequency and is the phase. The
curve tting procedure determines approximations to these parameters, A, ! and
; however, the simple algorithm outlined above for linear ts does not work here.
Instead we use the inbuilt Matlab command lsqcurvet. We rst make a new
function le named sinusoid.m that contains the tting function. Follow the
following steps to make a new function le, also called an \m-le".
1. From the File menu item, click New and M-le. A blank text editor opens.
28
function Fout=sinusoid(p,Fin)
Fout=p(1)*(sin(p(2)*Fin+p(3)));
end
Let's parse this le, line by line. The rst line starts with the label function
indicating that this m-le is a function le, or in other words, this le contains
the declaration of a function named sinusoid that can be called from inside the
command window. The function sinusoid takes in two vector arguments, p and
Fin. The former is a vector containing the unknown parameters. In our case p
has three elements p(1), p(2) and p(3) which are respectively A, ! and . The
latter Fin is the input vector, in our case this is the vector containing the time
values. The second line denes the tting function; this is the Matlab way of
writing Equation (1.50). Finally, the m-le ends with the statement end.
Once the tting function has been dened, we can nd the least squares curve
using the command,
ct=7.9551*sin(10.0256*t2+0.7971);
gure; plot(t,v,`ro'); hold on;
plot(t2,ct,`g-');
The results are shown in Figure 1.13.
The command,
29
8
2
v1 / V
-2
-4
-6
-8
-10
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t/s
Figure 1.13: Acquired voltage samples. The measurements are plotted as circles
whereas the least squares curve t is drawn as a solid line.
N
X
di2 (1.51)
i
which is a measure of the goodness of the t. Note that lsqcurvet will also work
for linear curve tting.
Q 12. Suppose a rocket is red into the space from rest. The distance covered
(in miles) by the rocket and the height gained (in miles) is given in the table below,
Distance (miles) 0 1 2 3 4 5 6 7 8 9 10 11 12
Height (miles) 0 0:53 0:75 0:92 1:07 1:20 1:31 1:41 1:51 1:60 1:69 1:77 1:85
Plot the graph of distance against height and perform curve tting using an
30
equation,
p
y = a bx (1.52)
t (s) 1 2 3 4 5 6 7 8
d (m) 0:20 0:43 0:81 1:57 2:43 3:81 4:80 6:39
Plot the distance with respect to t . Then plot with respect to t 2 . If the object
was initially at rest, calculate the acceleration. Use curve tting.
v (t ) = a1 + a2 e 3t=T
v (t ) = a1 + a2 e 3t=T + a3 te 3t=T
(1.53)
where t is the time and T is an unknown constant. The data given in Table 1.5
gives the voltage v of a certain device as a function of time. Which of the above
functions is a better description of the data [7]?
31
where the time constant = RC . Fit the given data in Table 1.7 to the equation
for the voltage increase and nd the value of .
t (s) 0 3 6 9 12 15 18 21 24 27 30
V (V) 0 6:55 10 13 14:5 15 16 16:2 16:3 16:5 16:55
Q 16. When a constant voltage was applied to a certain motor initially at rest,
its rotational speed S (t ) versus time was measured. The table given below shows
the values of speed against time.
Time (s) 1 2 3 4 5 6 7 8 10
Speed (rpm) 1210 1866 2301 2564 2724 2881 2879 2915 3010
Try to t the given data with the function given below. Calculate the constants
b and c.
S (t ) = b(1 e ct ) (1.55)
Fit the given data using the relation given below and calculate the unknown
coecients.
u = A(e BV ) (1.56)
32
u (ft/s) 66:77 59:16 54:45 47:21 42:75 32:71 25:43 8:18
V (volts) 7:58 7:56 7:55 7:53 7:51 7:47 7:44 7:28
Q 18. The yield stress of many metals, y , varies with the size of the grains.
Often, the relationship between the grain size, d, and the yield stress is modelled
with the Hall-Petch equation,
y = 0 + kd 1=2 (1.57)
33
Bibliography
[2] http://nsbri.tamu.edu/HumanPhysSpace/focus6/student2.html.
[3] http://zirka.dp.ua/Instructions.htm.
[8] The Math Works, "The Language of Technical Computing", (The Math
Works, 2000).
[9] Amos Gilat and Vish Subramaniam, "Numerical Methods for Engineers and
Scientists", (Wiley Companies, 2007).
34