Symbols, strings and numbers

>> syms x >> y = 2*x*exp(-x)-sin(x)Now the variables

>> ezplot(y,[0,pi])From the graph you can immediately see that

`fminbnd`

and `fzero`

operate on strings, i.e., expressions between quotes ('). The expressions can be expressions in the variable `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.7923The 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.3384It 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 = charSuppose that you want to make an array of function values of

>> 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.1057To check the result you might do the following

>> syms x >> subs(y,x,0.6) ans = 0.0939The 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+xThe 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