1 / 28

There are many matlab tutorials you can download on the internet,

Matlab – short for matrix library (matrices are the building blocks of Matlab. Matrices (or arrays) can be of all dimensions: Simple elements – 1x1 matrices, vectors – 1xN matrices, mulitple dimensional matrices (MxNx…).

Télécharger la présentation

There are many matlab tutorials you can download on the internet,

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matlab – short for matrix library (matrices are the building blocks of Matlab. Matrices (or arrays) can be of all dimensions: Simple elements – 1x1 matrices, vectors – 1xN matrices, mulitple dimensional matrices (MxNx…). Matlab has many built in functions and hundreds of other functions in ‘toolboxes’. For example – the statistics toolbox, containing functions for calculating correlations, mean values, variances…….. There are many matlab tutorials you can download on the internet, This is a combination of ones written by Tore Furevik and Seb Swart and Bjorn Backenberg

  2. Change visible details Current directory Command window

  3. Arrows on key board are useful shortcuts in Matlab. They are useful for flicking through your recent commands. The up arrow displays the last command – sin(1). Pressing it twice will bring up the ‘why’ command. The command can be edited by pressing the left arrow once and deleting 1 and putting 2. Can also put the first few letters of a command and press the tab button Or a few letters of a previous command and the up arrow will fill it in. Matlab is CASE SENSITIVE….try SIN(2) If you get into a struggle – ‘ctrl c’

  4. Matlab files are saved as .m files

  5. Matlab file formats: .m (programs) .mat (saved data) .fig (saved figures Can specify to save figs as anything else, eg .jpeg, .tiff…… Can import and export data in any file (eg netcdf)

  6. At the >> prompt, one can directly enter commands. As an example, this is one way to compute 2+2: >> a = 2 a = 2 >> b = 2 b = 2 >> c = a + b c = 4 It is often annoying to have Matlab always print out the value of each variable. To avoid this, put a semicolon after the commands: >> a = 2; >> b = 2; >> c = a + b; >> c c = 4 Only the final line produces output. Semicolons can also be used to string together more than one command on the same line: >> a = 2; b = 2; c = a + b; c c = 4

  7. Of course Matlab will also allow more complicated operations: >> a = 2; >> b = -12; >> c = 16; >> quad1 = (-b + sqrt(b^2 - 4*a*c)) / (2*a) quad1 = 4

  8. The lookfor command is also very useful. It searches through matlab functions to look for whether matlab has a function for the desired purpose. Try >> lookfor mean It only looks through the first line, to search the entire function: >>lookfor mean –all Or if you have an idea already… >>which mean

  9. >>gheko=[1,2,3;4,5,6;7,8,9] >>whos >>whos dog

  10. Make the following matrix: X=[1 0 4 9 2; 3 9 4 2 5; 3 4 2 6 4; 2 3 8 4 3; 2 9 3 2 11]; What is the mean value of each column and row? What is the minimum value of each column What is the mean, minimum and standard value and variance of the entire matrix Type mean=2 and try and calculate the mean again? How can you get rid of this? In Matlab there are several predefined constants (as there are functions). These include pi, i and j. The first being the ratio between the circumference and diameter of a circle and the latter 2 are both the imaginary unit vector What is the area and volume of a sphere with radius 6371km What is exp(i*pi)? Calculate (1+i)*4 and sin(2)+i*sinh(2i)

  11. >> mean(X,1) ans = 2.2000 5.0000 4.2000 4.6000 5.0000 >> mean(X,2) ans = 3.2000 4.6000 3.8000 4.0000 5.4000 >> min(X,[],2) >>mean(X(:)) • Circle Area   =       π • r²     =     ¼ • π • d² • Sphere Volume   =   4/3 • π • r³     =     ( π •d³)/6 • Euler's formula: e^(i pi) = -1

  12. What about nansum and nanmean

  13. Creating matrices: >>A=3.14 >>B=[1 9 3 5 2 4 6 8 3 9] 1x10 To make a more general matrix, separate by; >>C=[1 2 3 4; 3 4 5 6; 8 6 4 2] To extract elements: C(1,1) C(2,3) Matlab interprets a single index number as a row number. If the index number is larger than the number of rows, it will start over in the next column. C(5) becomes 4 and C(8) becomes 5. Rand(2,3) Ones(2,5) Magic(5) – what is special about this?

  14. Colons!!!! >a=1:10 >>a=1:2:10; >>b=9:-3:-9; >>c=0:0.2:2; >>d=0:pi/6:2*pi; >>C=[1:4;3:6;8:-2:2]; >>C(1:2,3) >>C(3,2:4) >>C(:,3:4) Change sequence: >>D=C(:,[2 1 3 4]) interchanges the first and second column. Or to remove 3rd column >>C1=C(:,[1:2 4]);

  15. Or join matrices: >>C2=[C C C1;C1 C C]; Or else C(3:end); get all elements from 3rd to last (if we don’t know length) Matrix dimensions >>x=rand(2,3,7,5); >>size(x) >>prod(size(x)); >>length(x) >>max(size(x)); Change the matrix dimensions without altering the sequence: >>y=reshape(x,2,3,35); size(y); >>z=reshape(x,prod(size(x)),1); size(z)

  16. Reshape doesn’t change elements. Useful if eg have a matrix with values for every month over a period of years and we want the mean value for just January >>[val,ind]=max(x(:)) %find the max value of the matrix and it’s position in the sequence (between 1 and 210). Although it is usually more helpful to know the position in the matrix >>[a,b,c,d]=ind2sub(ind,size(x)) %find the max value in x(a,b,c,d) we need 4 indices since we have a 4D matrix Check with help function >>[a,b,c,d]=ind2sub(size(x),ind) Testran program!!!!! Linefit program!!!!

  17. >>x=[1:5:30 NaN 20:-2:4 Inf]; >>I=isfinite(x) %returns a vector where the elements of x are finite numbers and 0 where they are not….find the indices to all elements of value 1 for I >>J=find(I) %elements 7 and 17 are omitted to remove them >>x=x(J) Alternately >>x=x(isfinite(x)) Replace all numbers >15 with 0 >> i=find(x>15); >> x(i)=0 >>x(find(x>15))=0; OR >>x(isprime(x))=x(isprime(x)).^2 %squares all prime numbers >>y=x(find(x>=10) %create a new vector with all numbers >10 isnumeric and ischar checks if a variable is a number or character A=‘word’ and B=100; isnumeric(A)………

  18. Generate a sequence of numbers from 0 to 10 with step length 0.01 Calculate the sin and cosine to all the numbers and visualize with plot >>x=0:0.01:10; >>y1=sin(x); >>y2=cos(x); plot(x,y1,x,y2,’--’) %%%%%%%plot different lines plot(x,y1,’r’,x,y2,’k’) %%%%%change colour of line; %%%%%%%%%%%%%plot lines with different colours and styles plot(x,y1,'w') hold on plot(x,y2,'r','linestyle','--') get(gcf) %%gives you all of the figure ‘handles’

  19. Calculate the square root of x >>y1=x^0.5 or >>y1=sqrt(x) Have to include a dot in order to multiply or divide element by element, x.^0.5 >>y1=x.^0.5; y2=x.^1.5; y3=x.^2; >>plot(x,y1,x,y2,’—’,x,y3,’:’)

More Related