# Symbols, strings and numbers

We will investigate the function  ' on the interval [0, ] with MATLAB. Execute the following commands.
>> syms x
>> y = 2*x*exp(-x)-sin(x)

Now the variables en are defined as symbols in MATLAB. You can draw the function in MATLAB with the command
>> ezplot(y,[0,pi])

From the graph you can immediately see that f has a maximum, a minimum and two zeroes on the interval [0, ]. The locations of the extrema and zeroes cannot be determined exactly, but have to be approximated numerically. However, the MATLAB functions fminbnd and fzero operate on strings, i.e., expressions between quotes ('). The expressions can be expressions in the variable x. You can simply convert a symbolic expression into a string with the function char.
>> fzero(char(y),[0.5,1])
Zero found in the interval: [0.5, 1].

ans =
0.8030

>> fminbnd(char(y),1.5,2)

ans =
1.8409

>> fzero(char(y),[2.5,3])
Zero found in the interval: [2.5, 3].

ans =
2.7923

The locations of the minimum and the zeroes have been found. Also the location of the maximum can easily be found.
>> fminbnd(char(-y),0,0.5)

ans =
0.3384

It will often occur that you have to convert a symbolic expression into a string or vice versa. With class you can always find out what type of expression you are working with.
>> class(y)

ans =
sym

>> z = char(y);
>> class(z)

ans =
char

Suppose that you want to make an array of function values of f without first defining your own function. Let us say that we want to make the array [(-1), (-0.6), (-0.2), ..., (1)]. This proceeds as follows.
>> x = -1:0.4:1;
>> z = vectorize(y)

z =
2.*x.*exp(-x)-sin(x)

>> eval(z)

ans =
-4.5951   -1.6219   -0.2899    0.1288    0.0939   -0.1057

To check the result you might do the following
>> syms x
>> subs(y,x,0.6)

ans =
0.0939

The interrelationship of symbols, strings and numbers may make using MATLAB difficult. You can only get a feeling for this by practising a lot. We give one more example.
>> syms x
>> 2.3456 + x

ans =
1466/625+x

The fact that the numerical value 2.3456 has been converted into the exact fraction 1466/625, raises the suspicion that the last result has become a symbolic value, because in MATLAB itself every exact fraction is immediately converted into a numerical value. A check with class` confirms this suspicion.

Esteur 2010-03-22