A, MATLAB basic functions
1, MATLAB help
(1) help function
help function can be used to view the corresponding help text
(2) lookfor function
If you do not know the specific name of the function, the function of a single know that related keywords, you can use lookfor to find out.
(3) common command help
help: Getting Help |
helpwin: Run Help Window |
who: Lists the current workspace variables | whos: For more information about the current list of workspace variables |
what: M lists files in the current folder or the specified directory, MAT files, MEX file | which: Shows the path or file specified function |
Check the existence of the specified variable or file: exist |
2, a simple example
Predefined variables
variable | Explanation |
eps | The smallest positive number of computers |
pi | Pi approximation 3.14159265358979 |
inf or Inf | gigantic |
NaN | Not quantitative |
i,j | I the imaginary unit defined |
flops | The number of floating-point arithmetic to calculate the amount of statistics |
3, MATLAB interface
(1) MATLAB search path
By selecting the setting MATLAB path of the main window, enter "Set Path" dialog box, you can add or remove search path to MATLAB.
MATLAB to get the file information stored on the hard drive by setting the path.
(2) Workspace Browser
Content top left corner of the desktop MATLAB workspace window allows the user to change the content of the work area of how a variable.
4, MATLAB basic functions
(1) command line window
command | Command Description | command | Command Description |
cd | Display or change the current folder | load | Load variables specified file |
to you | Display files in the current folder or the specified folder | diary | Log File command |
clc | Clearly display the contents of all of the work window | ! | Call DOS command |
home | Move the cursor to the top left corner of the command line window | exit | Exit MATLAB |
clf | Clear graphics window | quit | Exit MATLAB |
type | The file contents | pack | Collect memory fragmentation |
clear | Clear memory variable | hold | Pattern holding switch |
echo | Switch operating window information display | path | Show search directory |
disp | Or display variable text | save | Save memory variable to the specified file |
(2) calculator function
Operation | symbol | Operation | symbol |
addition | + | division | /or\ |
Subtraction | - | power | ^ |
multiplication | * |
In most cases, the input line spaces have no effect on MATLAB operations. Further, in the priority than addition multiplication in MATLAB. If you do not specify the output name, MATLAB will default to the result of the operation named ans.
Input (3) of simple matrix
MATLAB, do not have to explain how to do the dimension of the matrix, the storage will be automatically configured. Direct input matrix, the matrix elements are separated by space or comma, with matrix rows ";" isolation. The entire matrix in "[]" in.
You can also enter the matrix Branch
Enter the matrix elements
Commonly used punctuation
name | punctuation | effect |
comma | , | Used to display the calculation result of separation between the instruction and the subsequent instruction; As an input amount and the separation between inputs; Array elements as the delimiter |
Black spots | . | Array representation, the decimal point used; Before the operation for symbols constituting "array" operator |
semicolon | ; | End for the instruction, the calculation result of the inhibition of display; As the calculation result is not displayed after the instruction command thereto partition; Between the lines of the array as a delimiter |
colon | : | It is used to generate an array; Invoked when used as single subscript, it represents a long list of all the elements that make up; When used as a multi-index quoted, it represents all elements on Weina |
No comments | % | It has all the physical line start the first part of the comment is seen as a non-executive |
apostrophe | ' ' | String descriptor |
Parentheses | ( ) | Changing the order of operations; When invoked by the array; Instruction input variables in the function list by |
Square brackets | [ ] | With the input array; Instruction input variables in the function list by |
curly braces | { } | Descriptor element array cells; Graphic charged with a special character brackets |
The digraphs | - | Used as a variable, the function of the file name hyphen; Drawing the subscript charged Preamble |
No. Life | ... | Consists of three or more consecutive black dots. It stores the physical row in the row continues as a logical |
@ | @ | Placed before the function name, function handle is formed; Preamble anonymous function; Before placed in the directory, the directory user object class is formed |
Blank | Used as a separator between the input and the input Lang; Array element delimiter |
(4) complex types
Plural general form C = a + bi, or assignment may be used to generate a plurality of complex function
function | description |
conj(c) | Computing the complex conjugate of c |
real(c) | Returns the real part of c |
imag(c) | Returns the complex imaginary part of c |
isreal(c) | If c is not an element array has an imaginary part, the function returns a isreal |
abs(c) | Returns the complex modulus c |
angle (c) | Returns the number of pieces of angle c |
(5) calculating elementary functions
Function name | Functional Description | Function name | Functional Description |
without | Sine | sec | Secant |
are | Sinusoidal input in degrees | secd | Secant input in degrees |
born | Hyperbolic election | to | 双曲正割 |
asin | 反正弦 | asec | 反正割 |
asind | 反正弦,输出以度为单位 | asced | 反正割,输出以度为单位 |
asinh | 反双曲正弦 | asech | 反双曲正割 |
cos | 余弦 | csc | 余割 |
cosd | 余弦,输入以度为单位 | cscd | 余割,输入以度为单位 |
cosh | 双曲余弦 | csch | 双曲余割 |
acos | 反余弦 | acsc | 反余割 |
acosd | 反余弦,输出以度为单位 | acscd | 反余割,输出以度为单位 |
acosh | 反双曲余弦 | acsch | 反双曲余割 |
tan | 正切 | cot | 余切 |
tand | 正切,输入以度为单位 | cotd | 余切,输入以度为单位 |
tanh | 双曲正切 | coth | 双曲余切 |
atan | 反正切 | acot | 反余切 |
atand | 反正切,输出以度为单位 | acotd | 反余切,输出以度为单位 |
atan2 | 四象限反正切 | acoth | 反双曲余切 |
如,计算0~2的正弦函数,余弦函数
>> x=0:pi/10:2*pi;
>> y1=sin(x);
>> y2=cos(x);
>> figure(1);
>> help figure
>> plot(x,y1,'b-',x,y2,'ro-');
>> xlabel('X取值');
>> ylabel('函数值');
>> legend('正弦函数','余弦函数');
(6)指数和对数函数
函数名 | 功能描述 | 函数名 | 功能描述 |
exp | 指数 | realpow | 对数,若结果是复数则报错 |
expm1 | 准确计算exp(x)-1的值 | realog | 自然对数,若输入不是正数则报错 |
log | 自然对数以e为底 | realsqrt | 开平方根,若输入不是正数则报错 |
log1p | 准确计算log(1+x)的值
|
sqrt | 开平方根 |
log10 | 常用对数(以10为底) | nthroot | 求x的n次平方根 |
log2 | 以2为底的对数 | nextpow2 | 返回满足2^p>=abs(N)的最小正整数P,其中N为输入
|
二、关系和逻辑运算
1、关系运算符
关系运算符 | 描述 | 关系运算符 | 描述 |
< | 小于 | >= | 大于等于 |
<= | 小于等于 | == | 等于(非赋值) |
> | 大于 | ~= | 不等于 |
等于运算符(==):如果两个变量值相同将会返回变量值1,如果不同将会返回0;
不等运算符(~=):如果两个变量值不同则返回1,相同则返回0.
理论上a和b应该相等,但由于为近似值,故产生了round off错误,可以检测两数之间在一定的范围内是否近似相等
2、逻辑运算符
逻辑运算符 | 描述 |
& | 在两个逻辑数组之间逐元素的与操作 |
| | 在两个逻辑数组之间逐元素的或操作 |
~ | 对一个逻辑数组进行取反操作 |
xor(x,y) | 逻辑异或操作,当x和y一个为0,一个不为0返回true,同时为0或为1,返回false |
any(x) | 如x为向量,当x的任意一个元素不为0时返回true,否则返回false;如果x是数组,对于x的每一列,如果有一个元素不为0,则返回true,否则返回false |
all(x) | 如果x都不为0时返回true,否则返回false |
三、简单数组
1、一维数组的创建
(1)直接输入:
行向量
列向量
(2)用":"生产向量
a=j:k生产的向量是a[j,j+1,..,k]
a=j:d:k生成行向量a=[j,j+d,...,j+m*d],其中m=fix((k-j)/d)
(3)函数inspace生成按等差形式排列的行向量
在X1和X2之间默认生成100个线性分布的数据,相邻两个数据的差保持不变,构成等差数列。
x=linspace(X1,X2)
x=(X1,X2,n)
在x1和x2之间生成n个线性分布的数据。相邻两个数据的差保持不变同样能构成等差数列。
2、行向量转置为列向量
(1)直接转置(')
(2)使用transpose函数
3、二维数组的创建
二维数组由实数或复数排列成矩形构成,从数据结构上看,矩阵和二维数组没有区别。
直接输入
4、数组寻址
(1)对一位数组进行寻址
下标写成一个数或者一个数组即可以进行访问
对一维数组的其中一个元素进行访问
对一维数组的多个元素进行访问
还可以使用另一种方法实现上诉功能
(2)对二维数组进行寻址
二维数组寻址与一维数组寻址一样,只不过需要考虑二维数组的行和列,二一维数组只需要考虑一个行或列,所以对二维数组有以下三种方法。
全下标法:指定行、列访问
单下标法:单下标法进行访问,数组顺序为先列后行