You are on page 1of 5

Math 307

Lecture 2: Inline Functions and Plotting

August 26, 2016

1. Plotting. Lets motivate plotting with an example. The formula for the wind chill
index is
Twc = 35.74 + 0.6215T 35.75V 0.16 + 0.3965T V 0.16 .
(a) The temperature on some cold day is 30 degrees, and the wind is varying from 5 to
30 mph. Create an array of values of Twc corresponding to 20 evenly spaced wind
velocities in this range. Plot this array.

V = linspace(5,30,20); % wind velocities


T = 30;
% temperature
Twc = 35.74 + 0.6215*T 35.75*V.0.16 + 0.3965*T*V.0.16;

Notice the dot operator is used to compute Twc at each point of V all at once. Isnt
it great?! Also notice that Matlabs variables are case-sensitive! Now that weve
created the arrays, lets plot them!
figure;
% open up a new figure/window
plot(V,Twc); % plot V vs Twc
title('Wind chill on 30 degree day');
xlabel('Wind speed');
ylabel('Wind chill');

The result is the following figure:

(b) Goodness, I can hardly read it! Lets change how it looks...
figure;
plot(V,Twc, 'Linewidth',3); % make line thicker
title('Wind chill on 30 degree day','Fontsize',16); % increase font
xlabel('Wind speed','Fontsize',16);
ylabel('Wind chill','Fontsize',16);
set(gca,'Fontsize',16); % increase font on axes (gca = get current axes)

Now the result is the following, much better!

(c) The key to plotting: When using the command plot(x,y), the variable x must
contain x-coordinates, and y must contain y-coordinates. If x and y are single
numbers, Matlab will plot one point. If x and y are vectors, Matlab will connect
all the points with a line.
2. Anonymous functions are great ways to compute simple functions like the one above. In
the previous example, we created two vectors with 20 points each. What if we wanted to
increase the resolution of the image? We need more points! Rather than creating these
vectors over again, lets make a function.
(a) Heres how you can create the function from the last example, using a single variable
V for the wind velocity. In Matlab, write:
T = 30;
chill = @(V)

35.74 + 0.6215*T 35.75*V.0.16 + 0.3965*T*V.0.16;

The little @ sign tells you what the independent variables are, and what follows is
the function itself. Note that in this example, T is not an independent variable
of the function. It is always 30 (as we declared it before). You can include as
many inputs as youd like, and call them whatever youd like. You can also call the
function whatever youd like.
(b) To compute values of the function, for example, Twc (5), simply type:
chill(5)

% evaluate chill at V = 5

and Matlab will give you the result. You can save it to a variable if youd like:
temp0 = chill(5);

% Now the result (23.5237) is stored as ``temp0''

(c) Lets plot the function again! If you give one input into an inline function, you get
one output. If you give a vector of inputs, the function will give a vector of outputs.
You just have to be careful that your function is defined with a dot operator!
V = linspace(5,30,20); % wind velocities
Temps = chill(V);
% evaluate chill at all velocities in V.
plot(V,Temps);

Result is a vector!

This produces the same plot in problem #1. Lets add more points:
V = linspace(5,30,100); % wind velocities
Temps = chill(V);
% evaluate chill at all velocities in V.
plot(V,Temps);

Result is a vector!

Easy!
(d) Lets say we forgot to use the dot operator in the last example:
T = 20;
chill = @(V)
35.74 + 0.6215*T 35.75*V0.16 + 0.3965*T*V0.16;
V = linspace(5,30,100); % wind velocities
Temps = chill(V);
% evaluate chill at all velocities in V. Result is a vector!

Matlab is trying to input a vector, and raise it to the 0.16 power... yikes!
Result:
Error using
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.) instead.
Error in @(V)35.74+0.6215*T-35.75*V0.16+0.3965*T*V0.16
3. Now lets suppose T and V are both variables! We can do this!
(a) chill = @(T,V)

35.74 + 0.6215*T 35.75*V.0.16 + 0.3965*T.*V.0.16;

Notice the extra dot operator since I will multiply a vector of temperatures T against
a vector of velocities V .
(b) To compute one output with a temperature of 30 and velocity of 0, wed type:
temp1 = chill(30,5);

% Now the result (23.5237) is stored as ``temp1''

Note that this is different than the wind chill with a temperature of 0 and a velocity
of 20:
temp2 = chill(5,30);

% Now the result (19.3412) is stored as ``temp2''

Id much rather be in 30 degrees with 5mph wind than 5 degrees with 30mph wind!
(c) Matlab can even plot in 2D. But, this is easier to do with for loops, so well skip
this for now.
4. We can simplify inline functions by using vector inputs. Suppose x = [T, V ]. Our
function which depends on T and V , needs only 1 input with this new vector x. Recall
that x(i) is the Matlab command to access the ith element of a vector.
chill = @(x)

35.74 + 0.6215*x(1) 35.75*x(2).0.16 + 0.3965*x(1).*x(2).0.16

Not much has changed, except that the input is just one variable x, which should have
2 components. Heres how we can evaluate it:

temp0 = chill([30,5]);

% Now the result (23.5237) is stored as ``temp0''

OR
x = [30,5];
temp0 = chill(x);

Now the result (23.5237) is stored as ``temp0''

Again, not much has changed. The input is a single vector this time. Think about how
this could simplify your code if you had lots and lots of inputs!
5. Note: Inline functions can have multiple inputs, but only one output! To obtain multiple
outputs, we can put them in a vector. Consider the following function that takes in three
inputs (x, y, z) as a point in 3D and transforms the point to a new point (x+1, y+2, z5).
pointshift = @(x,y,z) [x+1, y+2, z5];

% three inputs, one output

To run, type:
x1 = 3; % define original point
y1 = 5; % define original point
z1 = 0; % define original point
pt2 = pointshift(x1,y1,z1);

The result it a vector called pt2, with coordinates (4,7,-5).

We could have also accomplished this with a vector input and a vector output:
pointshift2 = @(x) [x(1)+1, x(2)+2, x(3)5];

% one input, one output

To run, type:
pt1 = [3,5,0]; % define original point
pt2 = pointshift2(pt1);

Note: There is a workaround to get multiple outputs through the command deal, but
lets not over-complicate things...
6. There are some less desirable ways to write functions. One of these ways is through
Matlabs symbolic toolkit MuPAD using the command syms.
You can define certain variables to be symbolic rather than numeric:
syms T V

Then after a few minutes of Matlab trying to do something that should be done in
Mathematica, you can finally define your function:
chill = 35.74 + 0.6215*T 35.75*V0.16 + 0.3965*T*V0.16;

If we type in values for T and V , it changes the variable type of T and V back into
numbers. How do we evaluate this thing? We have to use yet another command called
subs. How terrible! (Can you tell what we think of syms?)
T = 30;
V = 5;
temp0 = subs(chill);

Did you catch the answer? I get:


temp0 =
10877/200 (4771*5(4/25))/200

Yuck! Ok, but we got the answer. Why is this not as great? Clearly, 23.5237 is easier
to read than that mess, but we suggest the following more important reasons why we
prefer NOT to use syms for numerical work:
(a) Inline functions are easier to write and manipulate. The command sub(chill)
doesnt really tell you what youre subbing into the function. The command
chill(30,5) clearly shows the first variable is 30 and the second is 5. This makes
your code more readable.
(b) If you want to evaluate the function many different times, it will be easier to do so
without using syms. To change the variables using syms, I have to type out what
T and V are before evaluating it (3 lines of code each time). To evaluate the inline
version, I just need to type the new values in the same line. This becomes a big
deal with lots of variables and/or lots of computations.
(c) Inline functions result in are faster computations than syms. This may not be a
big deal in this short function, but the effect is more pronounced with larger (more
realistic) computations.
(d) Matlab was designed for numerical computations, not symbolic computations. Mathematica was designed for symbolic computation. Each is better at what it is designed for.

You might also like