11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

a.*b -24, ans./cans =-18 -10 0 12 26Warning: Divide by zeroans =9 10 NaN 12 13Here we are warned about 0/0—giving a NaN(Not aNumber).Example 12.2 Estimate the limitsin ⇡xlimx!0 x .The idea is to observe the behaviour of the ratiosin ⇡xxfor a sequence of values of x that approachzero. Suppose that we choose the sequencedefined by the column vector>> x = [0.1; 0.01; 0.001; 0.0001]then>> sin(pi*x)./xans =3.09023.14113.14163.1416which suggests that the values approach ⇡. Toget a better impression, we subtract the value of⇡ from each entry in the output and, to displaymore decimal places, we change the format>> format long>> ans -pians =-0.05142270984032-0.00051674577696-0.00000516771023-0.00000005167713Can you explain the pattern revealed in thesenumbers?We also need to use ./ to compute a scalardivided by a vector:>> 1/x??? Error using ==> /Matrix dimensions must agree.>> 1./xans =10 100 1000 10000so 1./x works, but 1/x does not.12.4 Elementwise Powers (.^)The square of each element of a vector could becomputed with the command u.*u. However,a neater way is to use the .^ operator:>> u = [10, 11, 12]; u.^2ans =100 121 144>> u.*uans =100 121 144>> ans.^(1/2)ans =10 11 12>> u.^4ans =10000 14641 20736>> v.^2ans =400441484>> u.*w.^(-2)ans =2.5000 -11.0000 1.3333Recall that powers (.^ in this case) are donefirst, before any other arithmetic operation. Fractionaland decimal powers are allowed.When the base is a scalar and the power is avector we get>> n = 0:4n =0 1 2 3 4>> 2.^nans =1 2 4 8 16and, when both are vectors of the same dimension,>> x = 1:3:15x =1 4 7 10 13>> x.^nans =1 4 49 1000 2856111

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

Saved successfully!

Ooh no, something went wrong!