Laboratory Assignment 1: Introduction to the Symbolic Matlab

We begin by finding the Matlab package either on the desktop (with an icon that looks like some sort of surface) or under Start and programs. Of course, in either case we double click on the icon. The package opens to the Matlab Command Window, suggesting that you might want to look at helpwin, helpdesk or demo. We suggest that you type helpwin. Typing helpwin gives a window that lists HELP topics. Near the end of the list of topics is the topic toolbox/symbolic. Double clicking on toolbox/symbolic gives a window listed Symbolic Math Toolbox.

The Symbolic Math Toolbox window lists the functions available in symbolic Matlab. These are the commands that will be used the most in the M160 laboratory projects. For example if we scroll down a bit, we find under the subtitle Pedagogical and Graphical Applications the function ezplot. This is the function that we will use to produce routine x-y plots. If you double click on ezplot, you get an explanation of the ezplot function that emphasizes examples (usually the easiest way to figure out how to use these functions). We see that ezplot can be used to plot a function of the form y=f(x), an implicitly defined curve f(x,y)=0 and a parametrically defined curve x=x(t), y=y(t); either on a default domain or on a given domain. You should try the first example by typing

Ezplot(‘cos(x)’)

in the Matlab Command Window. This command should produce a nice plot of cos(x) in a figure window.

We should note that more is happening than just plotting the function cos(x). If you return to the description of the ezplot function, there is no mention of the ‘s appearing around the cos(x) function. As we mentioned earlier Matlab is a multipurpose package. Since the package is also a numerical package, we have both numerical variables (really a vector) and symbolic variables (that we usually consider in an algebra or calculus class); in addition to matrix variables that we will not discuss at all. Since we want to use the symbolic capabilities of the Matlab package, we want symbolic variables. The apostrophes around the cos(x) function tell ezplot that we want the symbolic function cos(x). The approach that we will use is to define our symbolic variables before we start working. If we return to the help window for the Symbolic Math Toolbox, under the subtitle Basic Operations we find the topic

sym – Create symbolic object.

If we double click on this topic, we find that we can create symbolic variables by the command (in the Command Window, of course)

x=sym(‘x’)

If we then write

ezplot(cos(x))

we get the same plot that we got before. If you want to make several symbols symbolic variables at the same time, we can type

syms a t x y

in which case a, t, x and y would all be symbolic variables. If we wanted to define a function to use it more than once, we could define

f=4+sin(3*(x-2))

and plot the function f by the command

ezplot(f)

When we look at these different plots, we see that the default domain is the interval [-6,6]. To plot the function f on the interval [-1,3], we write

ezplot(f,[-1,3])

If you are getting tired of the machine repeating everything that you give it, you can follow the command by a semicolon to eliminate the response. For example, the command

g=4*x+atan(2*x);

will define the function g but will not repeat the statement on the screen. One last piece of information before we become serious. Often we will want to plot more than one function on the same screen. If you define

f=sin(x);

and

g=sin(x+pi);

and want to plot them in the same figure, you type

ezplot(f)

hold on

ezplot(g)

The hold on command will plot g in the same figure window as f. The the plot can be saved an imported into a word document as described earlier. If you then want to make another plot in a window that does not contain f and g, you simply type

hold off

If you care, we note that when you used pi to represent p above you used the numerical, not symbolic version of p . Most of the time it will not make a difference to us, but if you ask the machine to evaluate

sin(pi/3)

we see that the result is approximately Ö 3/2 not equal to Ö 3/2. If you want the symbolic p , you must set

pi=sym('pi');

Then when you compute

sin(pi/3)

you get Ö 3/2.

In Section 1.3 we learned how to make new functions from old functions, i.e. that many functions that we study and use are the same except for a rather slight obvious change. For the function f(x)=x3-x2-9x+9 plot f(x), f(x)+1 and f(x)-1 on the same figure, label the graphs (by hand is ok) and describe the differences. Likewise, plot f(x), f(x+1) and f(x-1) on the same figure, label the graphs and describe the differences. We might note that at least one way to define f(x)+1 after f has already been defined is to type

fp1=f+1

Also one way to define f(x+1) after f has already been defined is to type

h=subs(f,x,x+1)

(define h by substituting x+1 for x in f(x)).

Repeat the above assignment with sin(x), sin(x+2p ), sin(x-2p ), 1+sin(x) and -1+sin(x).