|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help |
First let's use Matlab just like a calculator. Type
1.2 * sin(3.4^2 + log10(5))to see the value of
result = 1.2 * sin(3.4^2 + log10(5))Now you can use this value in a new calculation:
new = 3+result/2(Note that this gives
Matlab knows about complex numbers. You get
by typing
sqrt(-1), but since most people use i to represent
you can get it more easily by typing i. (To allow for the
peccadilloes of electrical engineers you can also get it by typing
j.) Try typing
exp(i*pi)to check that Matlab also knows about the exponential function and
z = 3 + 4*iand
z = 5 * exp(i*0.9273)Note that in both cases Matlab's response is in x+iy form, and that the argument (
Warning: If you should type something like i = 10 then the value
of i will be redefined and will no longer be
.
One of the most useful features of Matlab is that functions like
sin, log, abs and many others will work on whole
lists of numbers simultaneously. Suppose, for example, that we wanted
to evaluate the list
sin([-pi/2, 0, pi/2, pi])Notice that we used ( and ) to enclose the argument to the function sin, but [ and ] to enclose the list of numbers. If you make your list of numbers vertical instead of horizontal, then the answer comes out vertical too. Try:
vlist = [-pi/2; 0; pi/2; pi] sin(vlist)So, between [ and ] commas are used to put things beside each other, while semicolons put things on top of each other. (You can also use a space instead of a comma, and a line-break (`Return' key) instead of a semicolon -- try it.)
In fact functions like sin will work on two-dimensional arrays
of numbers, and the result will have the same shape as the original
array. To find
try1 = [1, 2, 3 ; -1, -2, -3] log(try1)
| | computing help | |