14:440:127– Introduction to Computers for Engineers
Notes for Lecture 11
Rutgers University, Fall 2008
Instructor Blase E. Ur
We’re going to begin with a corrected version of a section from Lecture 10 with 2 stupid and
serious mistakes I had left in there, but noticed and corrected as I was giving the lecture:
1
Correction from last lecture:
Fitting To More Compli
cated Functions
Oftentimes in engineering, you’ll want to find equations for functions that are not polynomial.
However, you’ll sometimes be able to transform these functions into polynomials:
1.0.1
Power Functions
A function such as
y
=
bx
m
can be called a power function since x is raised to a power. Note that
this is a polynomial, but you have no idea what degree it is, so it might be hard to choose which
degree polynomial to specify for
polyfit
.
Note that if you plot this function on a loglog plot using the
loglog
command, the data will appear
in a straight line.
Now, the key is that if you take the logarithm of both sides of this equation,
you’ll get
log
(
y
) =
log
(
bx
m
). Using log properties, you can rewrite this as
log
(
y
) =
log
(
b
)+
log
(
x
m
),
and then rewrite it as
log
(
y
) =
m
*
log
(
x
) +
log
(
b
). This has the form
j
=
m
*
h
+
k
if you define
j
=
log
(
y
),
h
=
log
(
x
), and
k
=
log
(
b
). This is perfectly analogous to
y
=
mx
+
b
, which is a linear
equation.
Now, find the coefficients using
polyfit(log(x),log(y),1)
, and call these coeffients [m k].
Now, go
ing back to the original equation, you can realize that you’ve already found
m
.
To find
b
, you
can recall that
k
=
log
(
b
), so
b
=
e
k
.
You can now write an equation for your data as
y
=
exp
(
coeffs
(2))
*
x
coeffs
(1)
.
Thus, you’ve used a transformation, fit the log of the power function to a line, and then reversed
your transformation to find the original coefficients.
1.0.2
Exponential Functions
Let’s say you’re given an exponential function, such as
y
=
b
(
e
)
mx
. This function would be displayed
as a straight line on a
semilogy
plot. This can be rewritten as
log
(
y
) =
mx
+
log
(
b
). Now, this has
the form
j
=
m
*
x
+
k
if you set
j
=
log
(
y
) and
k
=
log
(
b
). Note that you do not have to transform x.
Now, find the coefficients using
polyfit(x,log(y),1)
, and call these coeffients [m k].
Again, you’ve
already found
m
from the original equation, and you can find
b
by recalling that
i
=
log
(
b
), so
b
=
e
k
. You can now write an equation for your data as
y
=
exp
(
coeffs
(2))
*
(
e
)
coeffs
(1)
*
x
.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2
Recursion
There’s a very interesting technique in computer programming called recursion, in which you write
a
function that calls itself
. Eventually, this string of functions calling itself stops at some
base case
,
and the answers work their way back up. Of course, in order to understand recursion, you must
understand recursion. (That’s a joke).
As a first example, let’s consider factorial.
If you want to calculate
n
! (n factorial), you could
write this as
n
! =
n
*
(
n

1)!. Eventually, you need to get down to a base case. What should your
base case be? Well, 1! is 1, so make that your base case. So how do you write this as Matlab code?
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Finch
 matlab, Numerical Analysis, Derivative, Exponentiation

Click to edit the document details