MATLAB_3(运算,编程)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
EESC
Matrix Operations
Identity matrix: I
A*I=I*A=A
Examples
a = [ 1 4 2; 5 7 3; 9 1 6 ];
I = eye(3)
I= 100 010 001
a*I
ans = 142 573 916
单位矩阵乘法 不改变矩阵大小
a11 a12 a13
A a21
a22
a23
a31 a32 a33
(a11b11 a12b21 a13b31) C (a21b11 a22b21 a23b31)
(a31b11 a32b21 a33b31)
b11 b12
B b21
Scalar Operations(标量运算)
Basic expression 基本运算表达式
addition(加法)
a+b
a+b
subtraction (减法)
a-b
a–b
multiplication (乘法) a x b
a*b
division (除法)
a/b
a/b
矩阵除法(右除法) 注意与左除法区别!
Matrix right division: C = A / B Used to solve the matrix equation
X A = B where X = B A-1 In MATLAB, you can write
x = b * inv(a) x=b/a
Examples:
x = [ 3 -2 9 4 -5 6 2 ]; abs(x)
ans = 3294562
sin( [ 0 pi/6 pi/4 pi/3 pi/2 ] ) ans = 0 0.5000 0.7071 0.8660
a = 1:5; log(a)
ans = 0 0.6931 1.0986 1.3863
EESC
Matrix Operations
逆矩阵表示方法
Inverse of a matrix: A-1
A A-1 = A-1 A = I
Examples
a = [ 1 4 2; 5 7 3; 9 1 6 ];
b = inv(a)
b= -0.4382 0.2472 0.0225 0.0337 0.1348 -0.0787 0.6517 -0.3933 0.1461
exponent (指数)
ab
a^b
EESC
Scalar Operations(标量运算)
Processing order of operations计算顺序
parenthesis (starting from the innermost) 括号(从内到外)
exponentials (left to right) 指数函数(从左到右)
A is a p-by-q matrix B is a q-by-r matrix
then
C will be a p-by-r matrix where
q
C(i, j) A(i, k)B(k, j)
k 1
EESC
Matrix Operations
Matrix multiplication: C = A * B 矩阵乘法计算式(高等代数基本知识)
Useful Commands(重要命令)
help command doc command
Online help Online help
lookfor keyword Lists related commands
which
Version and location info
EESC
Matrix Operations(矩阵运算)
Scalar-matrix operations
矩阵与标量运算
Element-by-element operations
矩阵元素运算
EESC
Matrix Operations
Matrix multiplication: C = A * B 矩阵乘法表达式(注意与C = A .* B区别) If
a= 142 573 916
b = [ 6 1; 2 5; 7 3 ]
b= 61 25 73
c=a*b
c= 28 27 65 49 98 32
d=b*a
??? Error using ==> * Inner matrix dimensions must agree.
( 注意 a * b 必须保证行列匹配!)
EESC
Built-in Functions
Optional Results
Can return more than one result
Using MATLAB Function with Array Input
You can call built-in functions with array inputs
矢量元素间逐个计算
x = [ 1 2 3 4 5 ]; y = [ 2 -1 4 3 -2 ];
z=x+y
z= 31773
z = x .* y
矢量的维度 必须一致
z= 2 -2 12 12 -10
z = x ./ y
z= 0.5000 -2.0000 0.7500 1.3333 -2.5000
Examples
a = [ 1 2; 3 4 ]
a= 12 34
b = [ -1 3; 2 -1 ]
b= -1 3 2 -1
a .* b
ans = -1 6 6 -4
a*b
ans = 31 55
EESC
Matrix Operations
Examples
a = [ 1 4 2; 5 7 3; 9 1 6 ]
a*b
ans =
1.0000
0
0
0.0000 1.0000
0
0 -0.0000 1.0000
A-1在MATLAB中 表示为: inv(A)
EESC
Matrix Operations
矩阵除法(左除法) 重要! Matrix left division: C = A \ B Used to solve the matrix equation A X = B where X = A-1 B In MATLAB, you can write
4 2 6x 8
2
8
2
y
4
6 10 3 z 0
A = [ 4 -2 6; 2 8 2; 6 10 3 ];
B = [ 8 4 0 ]';
X=A\B
X= -1.8049 0.2927 2.6341
EESC
Matrix Operations
scalar multiplication
y= 2 4 6 8 10
z = x + 10
scalar addition
z= 11 12 13 14 15
EESC
Vector Operations
Use .* , ./ , .^ for element-by-element operations
% temp_f -- Temperature in degrees Fahrenheit
% temp_k -- Temperature in kelvins
% Prompt the user for the input temperature. temp_f = input('Enter the temperature in degrees Fahrenheit: ');
The function is applied to all elements of the array
The result is an array with the same size as the input array
MATLAB中,一些内建函数可以直接作用于矢量和矩阵
EESC
Built-in Functions
multiplications and divisions (left to right) 乘法除法(从左到右)
additions and subtractions (left to right) 加法减法(从左到右)
EESC
Built-in Functions(MATLAB内建函数)
result = function_name( input );
x = inv(a) * b x=a\b
(second version is recommended)
左除法主要用于解线性方程组
EESC
Matrix Operations
示例 Example: Solving a system of linear equations
4x 2y 6z 8 2x 8y 2z 4 6x 10y 3z 0
clear clc
Clears the workspace Clears the command window
diary filename
diary on/off who, whos Ctrl+c … %
Sends output to file Turns diary on/off Lists content of the workspace Aborts operation Continuation Comments
Document your program using comments that include(关于编程前的建议)
Short note about what your program does Short note about how it works Author information Date information Version information
(second version is recommended)
EESC
Matrix Operations (矩阵转置)
AT
A’ (In MATLAB)
EESC
Vector Operations(矢量运算)
Scalar-vector operations
x = 1:5
x= 12345
y=2*x
b22
b31 b32
(a11b12 a12b22 a13b32 ) (a21b12 a22b22 a23b32 ) (a31b12 a32b22 a33b32 )
EESC
Matrix Operations
( 注意 a * b 与 a .* b 区别!)
%
% Record of revisions:
%
Date
Programmer
Description of change
%
====
==========
=====================
% 12/01/97 S. J. Chapman
Original code
%
% Define variables:
abs, sign log, log10, log2 exp sqrt sin, cos, tan asin, acos, atan max, min round, floor, ceil, fix mod, rem
help function_name
Baidu Nhomakorabea
EESC
1.0000 1.6094
EESC
Program Design
EESC
Creating MATLAB Scripts(创建脚本)
Choose File>New>M-file from the menu 选择下拉菜单:文件->新建->脚本
Use the editor to write your program 利用编辑器进行编程
% Convert to kelvins. temp_k = (5/9) * (temp_f - 32) + 273.15;
EESC
Creating MATLAB Scripts
% Script file: temp_conversion.m
%
% Purpose:
% To convert an input temperature from degrees Fahrenheit to
% an output temperature in kelvins.