线性代数的Matlab口令

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
>> x = [1 8 9]; >> y = [1; 8; 9]; >> x + y ??? Error using ==> plus Matrix dimensions must agree.
OK to add/subtract a scalar to/from a vector though:
>> x = [1 8 9]; >> x + 4 ans = 5 12 13
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Vectors with Sequential Entries
Another method is to use linspace
disp(’Hello world!’) 2 + 5*cos(pi/6)ˆ2 - 3*sin(pi/8) disp(’Goodbye world...’)
At the command prompt:
>> test Hello world! ans = 4.6019 Goodbye world...
The Very Basics
Loops and Functions
Plotting and Graphics
Vectors
To create a row vector (1 × n matrix):
>> x = [1 8 9];
To create a column vector (n × 1 matrix):
>> x = [1; 8; 9];
>> x = [1:2:10] x = 1 3 5 7 9
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Vectors with Sequential Entries
Can be used in concatenation:
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Short and Long Formats
The format command changes the display mode.
>> format long >> pi ans = 3.141592653589793 >> format short >> pi ans = 3.1416
The default format is short
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
How To Get Help
The help command followed by another command name:
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
MatricesБайду номын сангаас
To create a matrix:
>> A = [1 8 9; 2 5 0] A = 1 2 >> A’ ans = 1 8 9 2 5 0 8 5 9 0
You can prepare files consisting of MATLAB commands to be executed in order. They need to have a “.m” extension. For example, let the contents of file test.m be
>>
Example:
>> 2 + 3 ans = 5
Another example:
>> pi ans = 3.1416
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
M Files
or
>> x = [1 8 9]’;
Addition/subtraction of vectors, and multiplication/division by a scalar are as usual:
>> x = [1 8 9]; >> y = [3 4 6]; >> z = 2*x + y z = 5 20 24
Lecture 1 Introduction to MATLAB
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Command Prompt
Enter commands at the prompt
>> x = [1:2:7 12:4:20] x = 1 3 5 7 12 16 20
[stepsize] can be negative:
>> x = [12:-4:0] x = 12 8 4 0
The default [stepsize] is 1:
>> x = [1:5] x = 1 2 3 4 5
Using [first]:[stepsize]:[last] syntax gives
>> 1:2:9 ans = 1 3 5 7 9
To assign to a vector:
>> x = [1:2:9] x = 1 3 5 7 9
The [last] value does not have to match the actual last entry:
That is because it is interpreted as
>> x + [4 4 4];
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Vectors with Sequential Entries
>> x = pi x = 3.1416
to assign variable x the value π. Use ; to suppress the response:
>> x = pi;
Example for using variables afterwards:
>> y = cos(x); >> z = yˆ2 z = 1
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Variables
No need to reserve space in the memory beforehand. For example, you just write
>> x = linspace(0,10,6) x = 0 2 4 6 8 10
>> x = linspace(10,0,6) x = 10 8 6 4 2 0
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
>> i ans = 0 + 1.0000i >> iˆ2 + 1
ans = 0
Never too late to reassign its original value:
>> i = sqrt(-1) ans = 0 + 1.0000i
The Very Basics
Variables, Vectors, and Matrices
The sum and prod Commands
To add or multiply all the entries of a vector:
>> sum([1 3 4]) ans = 8 >> prod([1 3 4]) ans = 12
It does not matter whether it is a row or column vector.
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
EE 20: Linear Methods for Engineering Analysis and Design Using MATLAB
>> help format FORMAT Set output format. FORMAT with no inputs sets the output format to the default appropriate for the class of the variable. For float variables, the default is FORMAT SHORT. FORMAT does not affect how MATLAB computations are done. Computations on float variables, namely single or double, are done in appropriate floating point precision, no matter how those variables are displayed. Computations on integer variables are done natively in integer. Integer variables are always displayed to the appropriate number of digits for the class, for example, 3 digits to display the INT8 range -128:127. FORMAT SHORT and LONG do not affect the display of integer variables. FORMAT may be used to switch between different output display formats of all float variables as follows: FORMAT SHORT Scaled fixed point format with 5 digits. FORMAT LONG Scaled fixed point format with 15 digits for double and 7 digits for single. FORMAT SHORT E Floating point format with 5 digits. FORMAT LONG E Floating point format with 15 digits for double and 7 digits for single. . . .
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
Vectors
Cannot add vectors with size mismatch and/or row and column vectors:
All variables stored in the MATLAB Workspace
The Very Basics
Variables, Vectors, and Matrices
Loops and Functions
Plotting and Graphics
The Complex i
Unless you assign something to i, it is reserved for the complex √ number i = −1.
相关文档
最新文档