11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

These are not allowable:Net-Cost, 2pay, %x, @signUse names that reflect the values they represent.Special names: you should avoid usingeps (which has the value 2.2204e-16= 2 54which is the largest number such that 1 + epsis indistinguishable from 1) andpi = 3.14159... = ⇡.If you wish to do arithmetic with complex numbers,bothi and j have the value p 1 unlessyou change them>> i,j, i=3ans = 0 + 1.0000ians = 0 + 1.0000ii = 3See Section 8.4 for more on complex numbers.6 Suppressing outputOne often does not want to see the result of intermediatecalculations—terminate the assignmentstatement or expression with semi–colon>> x=-13; y = 5*x, z = x^2+yy =-65z =104>>the value of x is hidden. Note that we canplace several statements on one line, separatedby commas or semi–colons.Exercise 6.1 In each case find the value of theexpression in Matlab and explain precisely theorder in which the calculation was performed.i) -2^3+9 ii) 2/3*3iii) 3*2/3 iv) 3*4-5^2*2-3v) (2/3^2*5)*(3-4^3)^2 vi) 3*(3*4-2*5^2-3)7 Built–In Functions7.1 Trigonometric FunctionsThose known to Matlab aresin, cos, tanand their arguments should be in radians.e.g. to work out the coordinates of a point ona circle of radius 5 centred at the origin andhaving an elevation 30 o = ⇡/6 radians:>> x = 5*cos(pi/6), y = 5*sin(pi/6)x =4.3301y =2.5000To work in degrees, use sind, cosd and tand.The inverse trig functions are called asin, acos,atan (as opposed to the usual arcsin or sin 1etc.). The result is in radians.>> acos(x/5), asin(y/5)ans = 0.5236ans = 0.5236>> pi/6ans = 0.52367.2 Other Elementary FunctionsThese include sqrt, exp, log, log10>> x = 9;>> sqrt(x),exp(x),log(sqrt(x)),log10(x^2+6)ans =3ans =8.1031e+03ans =1.0986ans =1.9395exp(x) denotes the exponential function exp(x) =e x and the inverse function is log:>> format long e, exp(log(9)), log(exp(9))ans = 9.000000000000002e+00ans = 9>> format shortand we see a tiny rounding error in the firstcalculation. log10 gives logs to the base 10.A more complete list of elementary functions isgiven in Table 2 on page 42.4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!