Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

MATLAB Programming/Vectoring Mathematics

From Wikibooks, open books for an open world
<MATLAB Programming
Thelatest reviewed version waschecked on16 April 2020. There aretemplate/file changes awaiting review.

[MATLAB Programming\|/MATLAB Programming]m]

Chapter 1: MATLAB ._.

Introductions .
Fundamentals of MATLAB
MATLAB Workspace
MATLAB Variables
*.mat files

Chapter 2: MATLAB Concepts

MATLAB operator
Data File I/O

Chapter 3: Variable Manipulation

Numbers and Booleans
Strings
Portable Functions
Complex Numbers

Chapter 4: Vector and matrices

Vector and Matrices
Special Matrices
Operation on Vectors
Operation on Matrices
Sparse Matrices

Chapter 5: Array

Arrays
Introduction to array operations
Vectors and Basic Vector Operations
Mathematics with Vectors and Matrices
Struct Arrays
Cell Arrays

Chapter 6: Graphical Plotting

Basic Graphics Commands
Plot
Polar Plot
Semilogx or Semilogy
Loglog
Bode Plot
Nichols Plot
Nyquist Plot

Chapter 7: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
Debugging M Files

Chapter 8: Advanced Topics

Numerical Manipulation
Advanced File I/O
Object Oriented Programming
Applications and Examples
Toolboxes and Extensions

Chapter 9: Bonus chapters

MATLAB Benefits and Caveats
Alternatives to MATLAB
[MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
Octave/MATLAB differences

edit this box


MATLAB is a vector programming language. The most efficient use of MATLAB will involve taking advantage of the built-in capabilities for manipulating data instead of using constructs such as loops.

Basic Math

[edit |edit source]

Most arithmetic operators will work as expected on vectors:

>>a=[24394378];>>5*aans=102154715390>>a/2ans=1.000021.5000471.500039.0000>>0.2+aans=2.200043.2000943.200078.2000

Likewise, all of these operations can be done with matrices for the expected results.

Most MATLAB functions, such assin orlog, will return a vector or matrix of the same dimensions as its input. So to compute the sine of all the integers between 0 and 10, it suffices to run

>>sin(0:10)

and the returned vector will contains ten values.

Per Element Operations

[edit |edit source]

Operators such as the carrot (^) or multiplication between vectors may not work as expected because MATLAB sees vectors the same as any other matrix, and so performs matrix power and matrix multiplication. All operators can be prefixed by a. to make it explicit that an operation should be performed on each element of the matrix. For example, to compute the differences of the sine and cosine function squared for all integers between 1 and 4, one can use:

>>(sin(1:4)-cos(1:4)).^2ans=0.09071.75681.27940.0106

as opposed to

>>(sin(1:4)-cos(1:4))^2???Errorusing==>mpowerMatrixmustbesquare.

which results from MATLAB's attempt to square a 1x4 vector using matrix multiplication.

Using.* or./ allows one to divide each element of a matrix or vector by the elements of another matrix or vector. To do this, both vectors must be of the same size.

Converting Loops to Vector-based mathematics

[edit |edit source]

Since MATLAB is a vector language, an artificial algorithm such as

x=[];v=[5,2,4,6];fori=1:4x(i)=v(i)*((i+32)/2-sin(pi/2*i));if(x(i)<0)x(i)=x(i)+3;endend

can be done far more efficiently in MATLAB by working with vectors instead of loops:

i=1:4;v=[5,2,4,6];x=v.*((i+32)/2-sin(pi/2*i));x(x<0)=x(x<0)+3;

Internally, MATLAB is of course looping through the vectors, but it is at a lower level then possible in the MATLAB programming language.

Retrieved from "https://en.wikibooks.org/w/index.php?title=MATLAB_Programming/Vectoring_Mathematics&oldid=3676747"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp