1. Write a Matlab factorial
function that uses a for
loop to compute
the factorial n! of its argument n
function p = factorial(n) % return factoral n! p = 1; for k = 1:n p = p*k; end end
2. The factorial n! is defined for non-negative n. Revise your answer to
problem 1 to check if n is negative. If it is, print an error message of
the form error: factorial(n) is not defined for n = -6
(where -6 is
the value of the argument n) and return not-a-number.
function p = factorial(n) % return factoral n! if n<0 fprintf('error: factorial(n) is not defined for n = %d\n', n); p = NaN; return; end p = 1; for k = 1:n p = p*k; end end
3. Solve problem 1 using a while
loop instead of a for
loop.
function p = factorial(n) % return factoral n! p = 1; while n>1 p = p*n; n = n-1; end end
4. Write a Matlab myexp(x,N)
function that computes the exponential
function e^x using a for
loop to sum the first N terms of its Taylor
expansion. You can use your factorial
function from problem 1.
function rtn = myexp(x,N) % return e^x computed from 1st N terms of Taylor expansion rtn = 0; for n = 0:N-1 rtn = rtn + x^n/factorial(n); end end
Note the above code is a little wasteful in terms of floating point operations
because each call to factorial
recomputes the same series of products
n! = 1 * 2 * 3 * 4 …., and similarly x^n = x * x * x * …. You can reduce the
total number of floating point operations by storing and updating the factorial
and nth power of x, like this
function rtn = myexp(x,N) % return e^x computed from 1st N terms of Taylor expansion rtn = 0; xn = 1; % variable to store x^n fn = 1; % variable to store n! for n = 0:N-1 rtn = rtn + xn/fn; xn = xn*x; % update x^n -> x^(n+1) for next pass through loop fn = fn*(n+1); % update n! -> (n+1)! for next pass through loop end end
5. Use Matlab's anonymous function facility to define a scalar function f that evaulates f(x) = x^2 + 3x + 2.
f = @(x) x^2 + 3*x + 2
6. Use matlab's anonymous function facility to define a vector function f that evaluates f(x,y) = (2x+y, x^2+y^2).
f = @(x,y) [2*x+y, x^2 + y^2]
7. Write a Matlab abs
function that returns the absolute value |x|
of its argument x (without referring to Matlab's built-in abs
function!.
function rtn = abs(x); % return the absolute value |x| if x<0 rtn = -x; else rtn = x; end
8. What is y as a function of x?
The log10 y versus x plot is a straight line, so the form of the equation is log10 y = mx + b or y = c 10^(mx). y is exponential in x.
y drops from just above 10^5 to just above 10^-5 as x goes from -2 to 3. Equivalently, log10 y drops from just above 5 to just above -5 as x goes from -2 to 3. Therefore the slope m of the line log10 y = mx + b is around m = rise/run = -10/5 = -2. At x=0, y is about 10^1. Plugging that into y = c 10^(-2x) gives c=10. Therefore the relation between y and x is roughly y = 10 * 10^(-2x).
9. What is y as a function of x?
The log10 y versus log10 x plot is a straight line, so the form of the equation is log10 y = m log10 x + b or equivalently y = c x^m. y and x are related by a power law.
y goes from about y=10^-6 to y=10^10 as x goes from x=10^-2 to x=10^6. Equivalently log10 y goes from -6 to 10 as log10 x goes from -2 to 6. So the line log10 y = m log10 x + b has slope of roughly m = 16/8 = 2. At x=10^6, y=10^10. Plugging that into y = c x^2 gives c = 10^-2. Therefore the relation between y and x is roughly y = 0.01 x^2