Expressions with variables

In MATLAB, you can work with variables that have been given a value. In principle, in MATLAB you get an error message when you work with a variable that has not been given a value.
>> clear x
>> exp(-x)+sin(x)
??? Undefined function or variable 'x'.
The `value' given to a variable does not need to be a numerical value. For example, the `value' of a variable could also be a `string'. This is an expression between quotes ('). In fact, we have already been using this with ezplot, fzero en fminbnd. Consider the result of
>> f='exp(-x)+sin(x)'

f =

>> ezplot(f,[0 2*pi])
The variable $ f$ has now been introduced, and has been given a string as its value. However, the variable $ x$ is still unknown to MATLAB, since it has not been given a value.

The function $ f$ has a zero in the interval [3,4] and a minimum in the interval [4,5].

>> fzero(f,[3,4])
Zero found in the interval: [3, 4].

ans =

>> fminbnd(f,4,5)

ans =
These commands do not give an error message, because of the fact that by specifying the search interval we have temporarily given $ x$ a value (or rather, a range of values). Another possibility is to give a variable a symbolic `value'. This goes as follows.
>> x = sym('x')

x =
>> syms x
which gives the same result. You can give a symbolic value to more than one variable at the same time in the following way:
syms x y z
You can now manipulate with these variables to your heart's desire.
>> v = x^3 + y^2 + 1


>> w = sin(z)

w =

>> v*w

ans =

Previous      Next      Up      Contents

Esteur 2010-03-22