Anda di halaman 1dari 2

Department of Mathematics

MATHS 108 Matlab Worksheet 1

Family Name: ID Number:

First Names:

You may want to read the Matlab help pages at http://www.math.auckland.ac.nz/Matlab


for more information about Matlab commands. The tutors in the Mathematics and Statistics
Computer Labs in the Basement of Building 303 can help you with Matlab.

Matlab commands in this worksheet follow a >>. Enter them exactly as they are shown.

Please printout this sheet and write your answers in the gaps provided. No cover sheet is
required. Hand in to the correct box on the Ground floor of Building 303. Late assignments or
assignments placed into incorrect boxes will not be marked.

1. Matlab can work symbolically with algebraic expressions; the variable names have to be declared
as symbolic:
>> syms x y z
We can now define y and z in terms of x.
>> y = x∧4 - 3*x∧2 - 2*x
This defines y as y = x4 − 3x2 − 2x. Note that we need to use * to tell Matlab to multiply by
the constants.
What does the Matlab operator ∧ do?

2. Matlab can solve an expression equal to zero. To solve x4 − 3x2 − 2x = 0, we use the command
>> solve(y)
Matlab gives the output as a temporary variable called ans. This will be overwritten by any
subsequent output. If we want to keep this output, we give it a name:
>> zero = solve(y)

What values of x make y = 0?

3. If we were solving y = 0 by hand, we would factorise the expression. Matlab can do this for us:
>> factor(y)
Matlab can also expand an expression, which can also be useful:
>> z = (x-1)*(x-2)*(x-3)*(x-4)*(x-5)
>> expand(z)
Write the expanded polynomial:

z=

MATHS 108 Matlab Worksheet 1 Page 1 of 2


4. Before entering the following command, predict its output:
>> solve(z)

5. Matlab can draw a graph of a function.


>> ezplot(z)
By default, Matlab plots a function between −2π and 2π. Since all of the interesting parts of
this function are in the domain [0, 6], we want to be able to tell Matlab to change the domain
it sketches the function on. Try the following commands.
>> ezplot(z,[0,6])
>> grid on

How many times does the graph of z cross the x-axis in the interval [0, 6]?

6. We know that z = 0 at several points. We might want to know the value of z another point.
>> p = subs(z,x,1.5)
This command substitutes the value of x = 1.5 into the expression for z, and calls this value p.
>> hold on; plot(1.5,p,’o’); hold off
The plot command can be used to draw other features on a graph; this command puts a circle
at the point (1.5, p). The hold on command keeps the graph from being redrawn, allowing
more graphs to be drawn over the top. The command to stop this is hold off.
The semicolons (;) in the above commands allow multiple commands on a single line.
We can “zoom in” on a function by changing the x- and y-axis bounds.
>> axis([1,2,0,4])
Is (1.5, p) at the peak of z? YES/NO

7. Enter the following commands, and sketch the 4

output graph on the axes on the right.


3

>> ezplot(2∧x)
2
>> hold on
>> ezplot(log2(x)) 1

>> ezplot(x) 0

>> axis([-4 4 -4 4])


−1
>> title(’’)
>> axis square −2

>> grid on −3

>> hold off


−4
−4 −3 −2 −1 0 1 2 3 4
x

Expect to see more Matlab commands and output in the assignments, test and exam.

MATHS 108 Matlab Worksheet 1 Page 2 of 2

Anda mungkin juga menyukai