Documente Academic
Documente Profesional
Documente Cultură
Vectorization of code
X=[1 2 3 4 5] Y=zeros(size(X)); For I=1 : numel(X) Y(i)=X(I)^2 End
X=[1 2 3 4 5] Y=x.^2
Functions of two variables Here is how we graph the function z(x,y) = x exp( x^2 - y^2): [x,y] = meshgrid(-2:.2:2, -2:.2:2); % Generate X and Y arrays for three-dimensional plots. The first command creates a matrix whose entries are the points of a grid in the square -2 <= x <= 2, -2 <= y <= 2. The small squares which make up the grid are 0.2 units wide and 0.2 unit tall. z = x .* exp(-x.^2 - y.^2); The second command creates a matrix whose entries are the values of the function z(x,y) at the grid points mesh(z) ;% create wireframe parametric surfaces specified by X, Y, and Z
In this example the differential equation. dy/dx=x|y|, y(0)=1, is approximated using Euler's Method:
h = 0.001;x = [0:h:2]; y = 0*x;y(1) = 1; i = 1;size(x); max(size(x)) while(i<max(size(x))) y(i+1) = y(i) + h*(x(i)-abs(y(i))); i = i + 1; end plot(x,y,'go') Hold ;plot(x,y)
For loop
Ex: B = [ [1 2 3]' [3 2 1]' [2 1 3]'] for j=2:3, for i=j:3, B(i,:) = B(i,:) - B(j-1,:)*B(i,j-1)/B(j-1,j-1) end end
Eulers method-dy/dx=x^2-y^2,y(0)=1
h = 0.1; x = [0:h:2]; y = 0*x; y(1) = 1; size(x) for i=2:21, y(i) = y(i-1) + h*(x(i-1)^2 - y(i-1)^2); end plot(x,y) plot(x,y,'go') plot(x,y,'go',x,y)
Built in functions
Diag End Eye Find Istrue,Isequal Ndims,Reshape Prod Sort,Sum Tril,Triu
Matrix functions
Linspace Fliplr Flipud Repmat B = repmat(A,m,n) creates a large matrix B consisting of an m-by-n tiling of copies of A. The statement repmat(A,n) creates an n-by-n tiling. Rot90 B = rot90(A,k) rotates matrix A counterclockwise by k*90 degrees, where k is an integer. Squeeze B = squeeze(A) returns an array B with the same elements as A, but with all singleton dimensions removed. A singleton dimension is any dimension for which size(A,dim) = 1.
rotate
a=[1 2 3 ;4 5 6;] b=rot90(a,1) or b=a(:,3:-1:1).' c=rot90(a,2)
B=[1 4 7 ;9 4 3 ;2 5 6] A=B C=A*B D=A.*B X=inv(A) I=Inv(A)*A Y=eig(A) p=round(poly(ans))-roots of characteristic equation
Roots(p)is equal to Eig(A) Sqrt(-1) Log(0) Humps and peaks Fplot and Gplot TIC Start a stopwatch timer. TOC Read the stopwatch timer. ETIME Elapsed time CLOCK = [year month day hour minute seconds]
U=[10 9 8] V=[1 2 3] U+V,U-V U*V,U.*V U/V-Slash or right division. U/V is the matrix division of U into V, which is roughly the same as U*INV(V) U\V-Backslash or left division. U\V is the matrix division of U into V, which is roughly the same as INV(U)*V
v1=1:3;v2=4:6;v3=7:9; N=[v1;v2;v3] tril(N) triu(N) sum(N) diag(N) Sum(diag(N)) prod(N) fliplr(N) load m.txt
Linear equations
Now consider a linear equation
ax + by = p cx + dy = q We can write this more compactly as AX = B coefficient matrix A is ab cd If A is invertible, X = (1/A)B, or, using Matlab notation, X = A\B
clear, C = [2,-3,4; 1,1,4; 3,4,-2], B = [5; 10; 0] b is a column vector. Solve for x, y and z using A = C\B, x = A(1), y = A(2), z = A(3) Verify by calculating C*A B (Note that these are matrix operations; you MUST NOT use a dot (.) before * or \)
Polynomials
F(x)=x^3-3x-2 Entering this polynomial P=[1 0 2 5] Polyval(P,2) K=roots(P)
Fitting a curve
Data to be fit x=[1 2 3 4 5] y=[23,78.9,367 57.99 82] h=polyfit(x,y,2)(change order) x2=1:0.5:5;y2=polyval(h,x2) plot(x,y,'o',x2,y2);hold ;grid on
Applications
Data fitting Solving linear equations Visualization of rotation