Matlab journey (full version)

catalogue

0 main content

1. Development of MATLAB

2. Advantages of MATLAB

3. Working environment of MATLAB

3.1 command window

3.2 figure window

3.3 file editor

4 help system

5 code system summary

0 main content

Development of MATLAB
Advantages of MATLAB
Working environment of MATLAB
help system
Code system summary

1. Development of MATLAB

 from U.S.A Mathworks company Officially launched in 1984, Then upgrade step by step. At present, it is usually updated twice a year, for example
Such as 2018a,2018b. be used for Numerical calculation and graphic processing System environment.
 MATLAB ( MATrix LABoratory )It's a kind of An interactive programming language based on matrix operation.
 There is only one data type, one standard input and output Statement without compiling.

2. Advantages of MATLAB

In addition to excellent numerical calculation ability, it also provides professional water Flat Symbol computing, word processing, visual modeling and simulation
real-time control And other functions. each Variables represent a matrix ; each individual All elements are considered plural ; All operations are on matrices and complex numbers
Effective; Instruction expression and common forms in mathematics and Engineering Very similar.
 Friendly working platform and editing environment Many tools are used: Graphical user interface, including MATLAB Desktop and some windows Interface, such as command window, editor and debugger; Cheng The program can run directly without compiling and can be updated in time Report the error and analyze the cause of the error.
 Easy to use programming language : MATLAB Is a high Level matrix / Array language. Grammatical features are more consistent with science and technology Personnel pair Writing format of mathematical expression , ideal for non Used by computer professionals.
 Strong scientific computing and data processing capabilities : including 600 A variety of mathematical operation functions can be easily realized by users Various calculation functions required. for example Matrix, eigenvector Fourier transform, linear equation solving, differential equation solving Complex number, trigonometric function, multidimensional array operation Wait. Matrix The number of rows and columns need not be defined in advance.
 Powerful and simple drawing function From the input data: Dynamic coordinate drawing; Can specify multiple coordinate systems; Can draw
Making curves and surfaces in three-dimensional coordinates; Different settings are available Color, linetype, angle of view, etc.
 Rich functions and strong scalability: Rich internal functions And toolbox. The toolbox includes signal processing, image processing
Control system, neural network, wavelet analysis, finance, etc, Almost all aspects are involved.

3. Working environment of MATLAB

Enter the working environment : double click MATLAB Icon.
Exit the work environment : exit , quit Or close directly.
MATLAB There are many windows for different functions, for example Command window , history command window, current directory window, work window
Space management window Graphics window and Text editing window Wait.
 
  1. Is the path folder selection
  2. Displays the current folder
  3. Click a file in the folder to display it
  4. Command line window
  5. Displays the variables and values of the named row window
  6. It is hidden in this figure, and the historical code is displayed

 

3.1 command window

It is the main carrier to operate MATLAB. By default, the command window opens when you start MATLAB.

MATLAB All functions and commands of can be found in the command Window execution. The running results of the program (except graphics) are displayed in the command Window.

3.2 figure window

A window that displays the drawing. Automatically pop up after executing the drawing command.

3.3 file editor

A window for writing and modifying file programs.

4 help system

Command window help system.
 help : find the usage of all commands or functions.
Form: help +Function name
 look for : when you don't know the exact name of a command or function
Find its function when.
Form: look for +Keywords

 

5 code system summary

%%==============I. Clear environment variables and commands==================
clear all   % eliminate Workspace All variables in
clc         % eliminate Command Window All commands in

%%==============II. Variable command rule=======================
%%
%====1. Variable names are case sensitive====
A = 2
a = 3

%%
%====2. The length of variable name shall not exceed 63 bits====
 ABCDEFGHIJKLMNOPQRSTUVWXYZ123456ABCDEFGHIJKLMNOPQRSTUVWXYZ123456 = 3

%%
%=====3. Variable names start with a letter and can consist of letters, numbers, and underscores, but punctuation is not allowed====
% 3A = 4
% .a = 5
% /b = 5
a_2 = 3
% a.2 = 4

%%
%====4. The variable name should be concise and clear. The physical meaning of the variable can be seen directly through the variable name====
A = rand(3,5)
rows = size(A, 1)
cols = size(A, 2)

%%==============III. MATLAB data type===========================
%%
%====1. number=====
2 + 4

10 - 7

3 * 5

8 / 2

%%
%====2. Characters and strings====
s = 'a'
abs(s)
char(65)
num2str(65)

str = 'I Love MATLAB & Machine Learning.'

length(str)

doc num2str

%%
%====3. matrix=====
A = [1 2 3; 4 5 2; 3 2 7]
B = A'%Transpose
C = A(:)%Column vector
D = inv(A)%inverse
A * D

E = zeros(10,5,3)
E(:,:,1) = rand(10,5)
E(:,:,2) = randi(5, 10,5)
E(:,:,3) = randn(10,5)

%%
%====4. Cell array====
A = cell(1, 6)
A{2} = eye(3)
A{5} = magic(5)
B = A{5}

%%
%====5. structural morphology====
books = struct('name',{{'Machine Learning','Data Mining'}},'price',[30 40])
books.name
books.name(1)
books.name{1}

%%============IV. MATLAB Matrix operation=============================
%%
%=====1. Definition and construction of matrix====
A = [1 2 3 5 8 5 4 6]
B = 1:2:9
C = repmat(B, 3, 1)%Copy, will B Copy into 3 rows and 1 column
D = ones(2, 4)

%%
%====2. Four operations of matrix=====
A = [1 2 3 4; 5 6 7 8]
B = [1 1 2 2; 2 2 1 1]
C = A + B
D = A - B
E = A * B'%Matrix calculation
F = A .* B Calculate the specific value
G = A / B     % G * B = A     G * B * pinv(B) = A * pinv(B)    G = A * pinv(B)
H = A ./ B

%% 
%====3. Subscript of matrix=====
A = magic(5)
B = A(2,3)
C = A(3,:)
D = A(:,4)
[m, n] = find(A > 20)

%%===============V. MATLAB Logic and process control===============
%%
%====1. if ... else ... end====
A = rand(1,10)
limit = 0.75;

B = (A > limit);   % B is a vector of logical values
if any(B)
  fprintf('Indices of values > %4.2f: \n', limit);
  disp(find(B))%dis output
else
  disp('All values are below the limit.')
end

%%
%====2. for ... end====
k = 10;
hilbert = zeros(k,k);      % Preallocate matrix

for m = 1:k
    for n = 1:k
        hilbert(m,n) = 1/(m+n -1);
    end
end
hilbert

%% 
%====3. while ... end====
n = 1;
nFactorial = 1;
while nFactorial < 1e100
    n = n + 1;
    nFactorial = nFactorial * n;
end
n

factorial(69)
factorial(70)

prod(1:69)
prod(1:70)

%%
%====4. switch ... case ... end====
mynumber = input('Enter a number:');

switch mynumber
    case -1
        disp('negative one');
    case 0
        disp('zero');
    case 1
        disp('positive one');
    otherwise
        disp('other value');
end

%%=============VI. MATLAB Script and function files===============
%%
%=====1. Script file====
myScript

%%
%====2. Function file=====
mynumber = input('Enter a number:');
output = myFunction(mynumber)

%%===========VII. MATLAB Basic drawing operations==============
%%
%======1. 2D plane drawing==========
x = 0:0.01:2*pi;
y = sin(x);
figure
plot(x, y)
title('y = sin(x)')
xlabel('x')
ylabel('sin(x)')
xlim([0 2*pi])

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
figure
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');%Two ordinates
set(get(AX(1),'Ylabel'),'String','Slow Decay') 
set(get(AX(2),'Ylabel'),'String','Fast Decay') 
xlabel('Time (\musec)') 
title('Multiple Decay Rates') 
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')

%%
%======2. Three dimensional drawing=============
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
xlabel('sin(t)')
ylabel('cos(t)')
zlabel('t')
grid on
axis square

%%
%======3. Saving and exporting drawings======

% (1) Edit → Copy Figure
% (2) Toolbar → Save
% (3) print('-depsc','-tiff','-r300','picture1')
% (4) File → Export Setup

%%================VIII. MATLAB File import================
%%
%====1. mat format====
save data.mat x y1 y2
clear all
load data.mat

%%
%====2. txt format====
M = importdata('myfile.txt');

S = M.data;
save 'data.txt' S -ascii
T = load('data.txt');

isequal(S, T)

%%
%====3. xls format====
xlswrite('data.xls',S)
W = xlsread('data.xls');
isequal(S, W)

xlswrite('data.xlsx',S)
U = xlsread('data.xlsx');
isequal(S, U)

%% 
%====4. csv format=====
csvwrite('data.csv',S)
V = csvread('data.csv');
isequal(S, V)


 

 

Keywords: MATLAB linear algebra

Added by tomhilton on Sun, 23 Jan 2022 10:43:37 +0200