Day10--MATLAB programming

MATLAB is a powerful software and is widely used in the field of automatic control. This series of blog posts will be based on control system simulation. Refer to the book MATLAB/Simulink and control system simulation. This series of blog posts and the author's automatic control theory (postgraduate entrance examination) complement each other. For detailed theoretical knowledge, please move to the automatic control theory (postgraduate entrance examination) series blog.

10.MATLAB program design

10.1 MATLAB program type

  1. Script M file executed in the command line window;
    Enter and execute on the command line. If there are no input parameters, no output parameters will be returned;
  2. M files that can be accessed, i.e. program files;
    Access in. m format, including a series of MATLAB instructions and necessary annotations. Variables need to be created and obtained in the workspace. The processed data is the data in the command line window. There are no input parameters and no parameters will be returned;
  3. Function file;
    After receiving the input parameters, the function executes and outputs the results;
    1. Function definition line: function[out1,out2,...] = filename(in1,in2,...);
    2. Function body statement: except the return and input variables, which are directly referenced in the function statement, all variables used in the function body are local variables, that is, after the function returns, these variables will be automatically cleared in the workspace;

10.2 MATLAB program flow control

% 1.Sequential program control
% Sequential program control starts from the first line of the program and is executed line by line until the last line of the program;


% 2.Branch program structure
% The program of branch program structure determines the execution direction according to whether the execution conditions are met;
% Branch structure: if-else-end Structure while Structure switch-case-otherwise Structure;

% 2.1 if,else,elseif sentence
% Format 1: if-end structure
if Logical expression
	Execute statement
end

% Format 2: if-else-end structure
if Logical expression
	Execute statement 1
else
	Execute statement 2
end

% Format 3: if-elseif-end structure
if Logical expression 1
	Execute statement 1
elseif Logical expression 2
	Execute statement 2
end
% Actual combat environment: MATLAB 2020b
% Tips: The following are made by MATLAB verification

% Actual combat 1: if Conditional statement,if_else.m
n=input('n=');
if n<0					% Judge positive and negative
    A='negative'		
elseif isempty(n)==1	% Judge whether it is empty
    A='empty'
elseif rem(n,2)==0		% Judge parity
    A='even'
else
    A='odd'
end

>> if_else
n=[]
A =
    'empty'
>> if_else
n=4
A =
    'even'
>> if_else
n=-10
A =
    'negative'
% 2.2 switch sentence
% Basic format:
switch expression		% Expressions can be scalars or strings
	case Value 1
		Statement 1
	case Value 2
		Statement 2
		...
	otherwise
		Statement 3
end
% Actual combat environment: MATLAB 2020b
% Tips: The following are made by MATLAB verification

% Actual combat 2: switch_.m
name=input('Please input your name:');
switch name
    case 'willard'
        'willard is fuxi technology CEO'
    case 'chen'
        'chen is fuxi technology CFO'
    case 'zhang'
        'zhang is fuxi technology big BOSS'
    otherwise
        'This people is not found'
end


>>Please input your name:'chen'
>>ans =
    'chen is fuxi technology CFO'
% 3.Loop program structure
% while Loop structure: the number of times the loop body is executed is uncertain;
% for Loop structure: the number of times the loop body is executed is determined;

% 3.1 for Circular statement format
for Cyclic variable=Starting value:step:Termination value
	Circulatory body
end

% 3.2 while Circular statement format
while expression
	Circulatory body
end
% Actual combat environment: MATLAB 2020b
% Tips: The following are made by MATLAB verification

% Actual combat 3: output 99 multiplication table
for i = 1:9
    for j = 1:i
        fprintf('%d*%d=%d  ', i, j, i * j)
        if i == j
            fprintf('\n')
            continue
        end
    end
end

1*1=1  
2*1=2  2*2=4  
3*1=3  3*2=6  3*3=9  
4*1=4  4*2=8  4*3=12  4*4=16  
5*1=5  5*2=10  5*3=15  5*4=20  5*5=25  
6*1=6  6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  
7*1=7  7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  
8*1=8  8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  
9*1=9  9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  

10.3 MATLAB programming principles

  1. %The following content is annotation to improve the readability of the program;
  2. Develop the habit of using the clear command to clear variables at the beginning of the main program to eliminate the impact of other variables in the working area on the operation of the program, and do not use clear in the subroutine;
  3. The parameter values are concentrated at the beginning of the program. Enter a semicolon after the statement line so that it and the intermediate results are not displayed on the screen;
  4. The input instruction is used to input some temporary data; If it is larger than a large number of parameters, a subroutine storing parameters is established and called through the subroutine name in the main program;
  5. The program shall be modular as far as possible, and the method of calling subroutines by the main program shall be adopted to merge all subroutines together to perform all operations;
  6. Use Debugger to debug the program, set breakpoints, single step execution and continuous execution;
  7. Set the working path of MATLAB;
% Here is MATLAB Basic structure of program

% explain
 Clear command: clear Workspace Variables and graphics in, clear,close
 Defining variables: including the declaration of global variables and the setting of parameter values
 Execute command line by line: refers to MATLAB Operation instructions provided or special commands provided by the toolbox

Control cycle: for,if then,switch,while Equal statement
 Execute commands line by line

end
 Drawing command: draw operation results

Keywords: MATLAB

Added by JC99 on Thu, 18 Nov 2021 17:07:57 +0200