|
|||
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 . To keep this value for later use, calling it by the name `result' (for example) type
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 , not ; you can always use round brackets to specify exactly what you mean: newnew = (3+result)/2).
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 . (You have just evaluated .) You can enter complex numbers either in terms of real and imaginary parts (z=x+iy), or in terms of modulus and argument ( ). Try typing
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 () must be specified in radians, not in degrees. You can recover the real part, imaginary part, modulus (absolute value), and argument by typing real(z) imag(z) abs(z) and angle(z) respectively. To find |z2|, for example, type abs(z^ 2).
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 | |