Professional Documents
Culture Documents
1. Display formats: If you want to use format command to control the format of results represented, you can do as follows: With short format:
>> format short >> 5/3 ans = 1.6667
We also have some other format like: short e, long e, short g, long g, bank, compact, loose. 2. Elementary math functions: sqrt(x): Square root exp(x): Exponential abs(x): Absolute value log(x): Natural logarithm (Base e logarithm) log10(x): Base 10 logarithm factorial(x): x!, x must be a integer 3. Trigonometric math functions sin(x): sine of angle x
cos(x): cosine of angle x tan(x): tangent of angle x cot(x): cotangent of angle x Note that: x in the functions above is in radians Some other trigonometric functions: asin(x), acos(x), atan(x), acot(x): inverse trigonometric of sin(x), cos(x), tan(x) and cot(x) sinh(x), cosh(x), tanh(x), coth(x): hyperbolic trigonometric of x
Note that:
sinh( x)
cosh( x) tanh( x)
coth( x)
5. One dimensional array: Many ways to declare a one dimensional array: The first way: % Row vector >> a = [1 2 3 4 5] % Column vector: >> b = a >> b = [1;2;3;4;5;6] The second way: % Declare a one dimensional array through the first value, step and the last value >> c = [1:2:10] % The first value is 1, the last value is 10 and the step is 2 The third way: use linspace function
% Declare a one dimensional array through the first value, the last value and the number of elements >> d = linspace(1, 100, 10) % the first value is 1, the last value is 100 and the number of elements is 10 6. Two dimensional array: Note that: Elements in the same row are seperated by a space or a comma Rows in a matrix are seperated by a semicolon
>> A = [1 2 3; 4 5 6; 7 8 9] >> A = [1,2,3; 4,5,6; 7, 8, 9] Because each row or column of a matrix is a vector (row vector or column vector), we can use the knowledge about how to create a new one dimensional array to create columns and rows of a matrix The index addressing of a array starts at 1. Its different from the index addressing in C, Java, which are from 0
Some predefined matrices in matlab: zeros(m,n): a m by n matrix with all elements are zero ones(m,n): a m by n matrix with all elements are one eye(n): a square matrix with elements on the main diagonal are one
Some predefined functions in matlab for array: length(a): returns the length of an array size(a): returns the size of an array diag(a): construct a matrix having elements on the main diagonal is vector a diag(A): construct a vector having elements is elements on the main diagonal of matrix A
mean(A): If A is a vector, this function will returns the mean value of the elements of this vector. Meanwhile, if A is a matrix, this function will return the mean value of each comlumn:
max(A): If A is a vector, this function will returns the maximum value among all elements. Meanwhile, if A is a matrix, this function will return the row vector that contains the maximun value
Note that: with [d,n] = max(A) if A is a vector, this function will return the largest element of vector A and the position of the largest element in this vector min(A): similar to max(A) sum(A): if A is a vector, this function will return the sum of elements of this vector, but if A is a matrix, this function will return the sum of each column of this matrix sort(A): If A is a vector, this function will arrange the elements of this vector in the ascending order (default mode). We alsocan indicate the mode of order: ascend or descend with the following function: sort(A, mode) in which mode is ascend or descend median(A): this function will return the median of the elements of a vector std(A): returns the standard deviation of the elements of a vector det(A): returns the determinant of a square matrix dot(a,b): calculates the dot product of two vectors a and b. Two vectors must have the same dimension
cross(a,b): calculates the cross product of two vectors a and b. Two vectors must have the same dimension inv(A): find the inverse matrix of matrix A
8. Generation of random vectors applying to generation of random noise rand(0,n): the uniform distribution in the range [0,1] randn(0,n): the standard Gaussian distribution with mean zero and variation one 9. Input from keyboard, output in screen and command windows
name = input('Please insert your name: ', 's'); disp(name) disp('Have a nice day') fprintf('Hello %s\n',name);
fprintf('x2 = %f\n',x2); elseif(delta == 0) fprintf('This equation has one solution x = %f\n', -b/(2*a)); else fprintf('This equation does not have any solution\n'); end end fprintf('Do you want to continue (Y/N)? \n'); ans = input('','s'); if(ans == 'Y') hope = 'Y'; else hope = 'N'; end end
10. Open a new file, write something and close this file:
fid = fopen('new file.txt', 'a'); fprintf(fid,'Hello boss, how are you today ?\n'); ans = input('','s'); fprintf(fid,'%s', ans); fclose(fid);
Note that: The permission for this file: o r (read): read only o w (write): Write something to this file. If this file already exists, this mode will delete all and write something new o a (append): Write something to this file. If this file already exist, this mode will append the text from the end of existed file
11. Import and export data from and into a excel file
>> import = xlsread('example')
import =
1.0e+03 *
1.8750 1.8750
0.0047 0.0045
0.0003 0.0003
0.0004 0.0004
0.0116 0.0113
0 0
0 0
0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
>> xlswrite('example_1',import)
Note that: We also can indicate some other options like sheet name, range, in the command xlsread and xlswrite if the excel file has more than one sheet or just want to get a specific area in the excel file 12. Programming in Matlab: a. Relational Operators: The result will be 1 if the comparison is true (logical true) and it will be zero if the comparison is false (logical false) If two scalar are compared, the results will be 0 or 1. If two matrices or two vectors are compared, the comparison is done element by element, the result is a logical array of the same size with 0 and 1 in the same dimensional matrix or vector If a scalar is compared to a matrix or vector, this scalar will be compared with each element of the matrix or vector. The result will be a matrix or vector of 0 and 1 Some built-in relational operators: o and(A,B) or A&B o or(A,B) or A|B o xor(A,B) o not(A) or ~A o all(A): returns 1 if all elements of vector A is nonzero and 0 if one of them is zero o any(A): returns 0 if all elements are zero and 1 if one of them is nonzero o find(A): returns the indies of the nonzero elements o find(A>d): returns the indies of the elements which are greater than d