# Getting started with Matlab Numerical Methods Appendix B help/techdoc/learn_matlab/learn_matlab.html.

## Presentation on theme: "Getting started with Matlab Numerical Methods Appendix B help/techdoc/learn_matlab/learn_matlab.html."— Presentation transcript:

Getting started with Matlab Numerical Methods Appendix B http://www.mathworks.com/access/helpdesk/ help/techdoc/learn_matlab/learn_matlab.html

What Is MATLAB? Math and computation Math and computation Algorithm development Algorithm development Data acquisition Data acquisition Modeling, simulation, and prototyping Modeling, simulation, and prototyping Data analysis, exploration, and visualization Data analysis, exploration, and visualization Scientific and engineering graphics Scientific and engineering graphics Application development, including graphical user interface building Application development, including graphical user interface building

The MATLAB System Development Environment. Development Environment. The MATLAB Mathematical Function Library. The MATLAB Mathematical Function Library. The MATLAB Language. The MATLAB Language. a high-level matrix/array language a high-level matrix/array language Graphics. Graphics. The MATLAB External Interfaces (API). The MATLAB External Interfaces (API).

MATLAB Online Help Desktop Tools and Development Environment Desktop Tools and Development Environment Mathematics Mathematics Programming Programming Graphics Graphics 3-D Visualization 3-D Visualization Creating Graphical User Interfaces Creating Graphical User Interfaces External Interfaces/API External Interfaces/API

Matrices and Arrays To enter D ü rer's matrix, simply type in the Command Window To enter D ü rer's matrix, simply type in the Command Window >>A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] >>A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] A = A = 16 3 2 13 16 3 2 13 5 10 11 8 5 10 11 8 9 6 7 12 9 6 7 12 4 15 14 1 4 15 14 1

sum, transpose, and diag sums of the columns of A sums of the columns of A >>sum(A) >>sum(A) ans = 34 34 34 34 ans = 34 34 34 34 >>sum(A')' >>sum(A')' ans = ans = 34 34 sum(diag(A)) sum(diag(A)) ans = 34 ans = 34 The transpose operation is denoted by an apostrophe or single quote, '.

Subscripts >>A(1,4) + A(2,4) + A(3,4) + A(4,4) >>A(1,4) + A(2,4) + A(3,4) + A(4,4) ans = 34 ans = 34 >>X = A; >>X = A; >>X(4,5) = 17 >>X(4,5) = 17 X = X = 16 3 2 13 0 16 3 2 13 0 5 10 11 8 0 5 10 11 8 0 9 6 7 12 0 9 6 7 12 0 4 15 14 1 17 4 15 14 1 17

The Colon Operator >> 1:10 >> 1:10 ans = 1 2 3 4 5 6 7 8 9 10 ans = 1 2 3 4 5 6 7 8 9 10 >> 100:-7:50 >> 100:-7:50 ans = 100 93 86 79 72 65 58 51 ans = 100 93 86 79 72 65 58 51 >> 0:pi/4:pi >> 0:pi/4:pi ans = 0 0.7854 1.5708 2.3562 3.1416 ans = 0 0.7854 1.5708 2.3562 3.1416 In subscript In subscript >> A(1,1:4) >> A(1,1:4) ans = 16 3 2 13 ans = 16 3 2 13

The magic Function >> B = magic(4) >> B = magic(4) B = B = 16 2 3 13 16 2 3 13 5 11 10 8 5 11 10 8 9 7 6 12 9 7 6 12 4 14 15 1 4 14 15 1 To make this B into D ü rer's A, swap the two middle columns: To make this B into D ü rer's A, swap the two middle columns: A = B(:,[1 3 2 4]) A = B(:,[1 3 2 4])

Expressions Variables Variables num_students = 25 num_students = 25 Numbers Numbers 3 -99 0.0001 9.6397238 1.60210e-20 6.02252e23 1i -3.14159j 3e5i 3 -99 0.0001 9.6397238 1.60210e-20 6.02252e23 1i -3.14159j 3e5i Operators Operators +-*/^ +-*/^ \ Left division \ Left division Functions Functions help elfun help elfun help specfun help specfun help elmat help elmat

Working with Matrices

Generating Matrices zeros All zeros zeros All zeros ones All ones ones All ones rand Uniformly distributed random elements rand Uniformly distributed random elements randn Normally distributed random elements randn Normally distributed random elements

ones(n,m) >> ones(3,4) >> ones(3,4) ans = ans = 1 1 1 1 1 1 1 1

zeros(n,m) >> zeros(3,4) >> zeros(3,4) ans = ans = 0 0 0 0 0 0 0 0

rand(n,m) >> rand(3,4) >> rand(3,4) ans = ans = 0.9501 0.4860 0.4565 0.4447 0.9501 0.4860 0.4565 0.4447 0.2311 0.8913 0.0185 0.6154 0.2311 0.8913 0.0185 0.6154 0.6068 0.7621 0.8214 0.7919 0.6068 0.7621 0.8214 0.7919

randn(3,4) >> randn(3,4) >> randn(3,4) ans = ans = -0.4326 0.2877 1.1892 0.1746 -0.4326 0.2877 1.1892 0.1746 -1.6656 -1.1465 -0.0376 -0.1867 -1.6656 -1.1465 -0.0376 -0.1867 0.1253 1.1909 0.3273 0.7258 0.1253 1.1909 0.3273 0.7258

Load and Save.mat >> A = [1 2 3] >> A = [1 2 3] >> B = [4 5 6] >> B = [4 5 6] >> save mydata.mat >> save mydata.mat >> clear >> clear >> load mydata.mat >> load mydata.mat

eye(n), eye(size(A)) >> eye(3) >> eye(3) ans = ans = 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 >> eye(size(A)) >> eye(size(A)) ans = ans = 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0

Load and save ASCII file >> a = magic(4); b = ones(2, 4) * -5.7; c = [8 6 4 2]; >> a = magic(4); b = ones(2, 4) * -5.7; c = [8 6 4 2]; >> save -ascii mydata.dat >> save -ascii mydata.dat >> clear >> clear >> load mydata.dat >> load mydata.dat >> mydata >> mydata mydata = mydata = 16.0000 2.0000 3.0000 13.0000 16.0000 2.0000 3.0000 13.0000 5.0000 11.0000 10.0000 8.0000 5.0000 11.0000 10.0000 8.0000 9.0000 7.0000 6.0000 12.0000 9.0000 7.0000 6.0000 12.0000 4.0000 14.0000 15.0000 1.0000 4.0000 14.0000 15.0000 1.0000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 8.0000 6.0000 4.0000 2.0000 8.0000 6.0000 4.0000 2.0000

Building Tables >> n = (0:9)'; >> n = (0:9)'; pows = [n n.^2 2.^n] pows = [n n.^2 2.^n] pows = pows = 0 0 1 0 0 1 1 1 2 1 1 2 2 4 4 2 4 4 3 9 8 3 9 8 4 16 16 4 16 16 5 25 32 5 25 32 6 36 64 6 36 64 7 49 128 7 49 128 8 64 256 8 64 256 9 81 512 9 81 512 MATLAB uses a dot, or decimal point, as part of the notation for multiplicative array operations.

Multivariate Data >>D = [ >>D = [ 72 134 3.2 72 134 3.2 81 201 3.5 81 201 3.5 69 156 7.1 69 156 7.1 82 148 2.4 82 148 2.4 75 170 1.2 ] 75 170 1.2 ] to obtain the mean and standard deviation of each column, use to obtain the mean and standard deviation of each column, use >>mu = mean(D), sigma = std(D) >>mu = mean(D), sigma = std(D) mu = 75.8 161.8 3.48 mu = 75.8 161.8 3.48 sigma = 5.6303 25.499 2.2107 sigma = 5.6303 25.499 2.2107 >>help datafun >>help datafun >>help stats >>help stats

Matlab Graphics

plot(x,y) t=[0:5:100] t=[0:5:100] y=t.^0.34-log10(t)+1./t y=t.^0.34-log10(t)+1./t plot(t,y) plot(t,y) title( ‘ Plot of y versus t ’ ) title( ‘ Plot of y versus t ’ ) grid grid

3D graphics [x,y]=meshgrid(-4.0:0.2:4.0,-4.0:0.2:4.0); [x,y]=meshgrid(-4.0:0.2:4.0,-4.0:0.2:4.0); z=(-20*x.^2+x)+(-15*y.^2+5.*y); z=(-20*x.^2+x)+(-15*y.^2+5.*y); surfl(x,y,z); surfl(x,y,z); axis([-4 4 -4 4 -800 0]) axis([-4 4 -4 4 -800 0]) xlabel('x-axis'); xlabel('x-axis'); ylabel('y-axis'); ylabel('y-axis'); zlabel('z-axis'); zlabel('z-axis');

Try yourself, and have fun!

Similar presentations