表达式 | ||||||||||||||||
像大多数其他设计语言,MATLAB提供了数学表达式,但是不像大多数设计语言,那些表达式涉及全面矩阵。组件表达式的模块是: (1)变量 (2)数字 (3)操作符 (4)函数 也可查阅表达式的实例。 | ||||||||||||||||
一、变量 MATLAB不需要任何类型声明或维度声明。当MATLAB遇到一个新的变量名称,它自然而然的创建变量和分配合适的应用存储的大小。如果变量已经存在,MATLAB更改它的内容并且,如果有必要,便分配新的存储。例如,num_students = 25 创建一个1*1矩阵命名为num_students并且在它的单精度元素中存储值为25。 变量名称包括一个字母,字母的后面跟着任意数量数字或字符。MATLAB使用仅仅一个变量名字的首31个字符。它能够区分变量名称的大写和小写字母。“A”和“a”不是相同的变量。 | ||||||||||||||||
二、数值 | ||||||||||||||||
MATLAB 使用常规小数符号,作为一个自选小数点和主导正或负。科学符号使用字母e来规定一个十的平方比例因素。假设数字使用两者i或j作为一个下标。一些合法的数值例子是: 3 -99 0.0001 内部的所有数值被存储使用long 格式特定通过IEEE浮点标准。浮点数值有一个大致16的有限精度重要小数数字和一个有限的大致10-308到10+308的范围。 | ||||||||||||||||
三、操作符 | ||||||||||||||||
表达式使用友善的算术操作符和优先规则。
| ||||||||||||||||
四、函数 | ||||||||||||||||
MATLAB提供一个大型标准元素数学函数的数值,包括abs,squt,exp和sin。收入一个否定数值的平方根或对数不是一个错误,适合复合结果是被自动产生。MTLAB也提供更多高深数学函数,包括Bessel和gamma函数。这些中的多数函数接受综合。显示数学函数的列表,键入 help elfun 显示数学和矩阵函数的列表,键入 help specfun 有些函数,像sqrt和sin,被组建在Built-in函数中是MATLAB核心的一部分是非常有力的。但是计算详情不是轻易存取的。其他函数,像gamma和sinh,是在M文件中执行的。 那些不同于组建函数和其他函数。例如,组建函数,你布恩那个查看代码。其他函数,你能够查看代码并且如果你需要,甚至可以修改。 几种特殊函数提供通用常数的值。
无限是通过零划分一个非零变量被产生的,或通过估计并定义数学表达式溢出,i.e.,超过实际最大值。不是一个数是通过尽力去估计像0/0的表达式或无限-无限被产生的,其不能有和定义数值。 函数名称不是含蓄的。 它对于作为一个新值覆盖写入它们中的任何很重要,这些特殊的函数可以被赋新值使用,例如 eps = 1.e-6 然后使用在后来计算中的值。原型函数能够被恢复 clear eps | ||||||||||||||||
五、表达式的例子 | ||||||||||||||||
你已经看到几种MATLAB表达式的例子。这有一些更高级的例子,并有结果值。 rho = (1+sqrt(5))/2 a = abs(3+4i) z = sqrt(besselk(4/3,rho-i)) huge = exp(log(realmax)) toobig = pi*huge |
Expressions
Like most other programming languages, MATLAB provides mathematical expressions, but unlike most programming languages, these expressions involve entire matrices. The building blocks of expressions are
(1)Variables
(2)Numbers
(3)Operators
(4)Functions
See also Examples of Expressions.
一、Variables
MATLAB does not require any type declarations or dimension statements. When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. For example,
num_students = 25
creates a 1-by-1 matrix named num_students and stores the value 25 in its single element.
Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. To view the matrix assigned to any variable, simply enter the variable name.
二、Numbers
MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are
3 -99 0.0001
9.6397238 1.60210e-20 6.02252e23
1i -3.14159j 3e5i
All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite range of roughly 10^(-308) to 10^(+308).
三、Operators
Expressions use familiar arithmetic operators and precedence rules.
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
\ | Left division (described in "Matrices and Linear Algebra" in the MATLAB documentation) |
^ | Power |
' | Complex conjugate transpose |
( ) | Specify evaluation order |
四、Functions
MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin. Taking the square root or logarithm of a negative number is not an error; the appropriate complex result is produced automatically. MATLAB also provides many more advanced mathematical functions, including Bessel and gamma functions. Most of these functions accept complex arguments. For a list of the elementary mathematical functions, type
help elfun
For a list of more advanced mathematical and matrix functions, type
help specfun
help elmat
Some of the functions, like sqrt and sin, are built in. Built-in functions are part of the MATLAB core so they are very efficient, but the computational details are not readily accessible. Other functions, like gamma and sinh, are implemented in M-files.
There are some differences between built-in functions and other functions. For example, for built-in functions, you cannot see the code. For other functions, you can see the code and even modify it if you want.
Several special functions provide values of useful constants.
pi | 3.14159265... |
i | Imaginary unit, |
j | Same as i |
eps | Floating-point relative precision, |
realmin | Smallest floating-point number, |
realmax | Largest floating-point number, |
Inf | Infinity |
NaN | Not-a-number |
Infinity is generated by dividing a nonzero value by zero, or by evaluating well defined mathematical expressions that overflow, i.e., exceed realmax. Not-a-number is generated by trying to evaluate expressions like 0/0 or Inf-Inf that do not have well defined mathematical values.
The function names are not reserved. It is possible to overwrite any of them with a new variable, such as
eps = 1.e-6
and then use that value in subsequent calculations. The original function can be restored with
clear eps
五、Examples of Expressions
You have already seen several examples of MATLAB expressions. Here are a few more examples, and the resulting values.
rho = (1+sqrt(5))/2
rho =
1.6180
a = abs(3+4i)
a =
5
z = sqrt(besselk(4/3,rho-i))
z =
0.3730+ 0.3214i
huge = exp(log(realmax))
huge =
1.7977e+308
toobig = pi*huge
toobig =
Inf