These days, we have just begun to learn the object-oriented programming of MATLAB. The previous thing is to use MATLAB to write some simple script or function, this aspect of MATLAB mature function and straightforward matrix operation method and syntax are easy to get started, convenient for people to focus on the algorithm itself. The first few days to write code when the thought, in the actual use of MATLAB numerical calculation, the data and functions with some methods to organize together will also bring great convenience, otherwise scattered data and functions are always looking uncomfortable. For example, I just recently wanted to write a little bit of code in MATLAB to make the calculation of stress tensor related more concise.
First, the physical background of the stress tensor example
In elasticity, the stress tensor is the stress state of a certain point in the body of the elastomer, and the stress (vector) relationship between it and the plane over which the normal vector is $\hat{n}$ is: $$\boldsymbol{t}=\boldsymbol{t}\cdot \hat{n}=\hat {N}\cdot \boldsymbol{t}$$ the $\boldsymbol{t}$ here is a stress vector; the coarse $\boldsymbol{t}$ is the stress tensor, which is a second-order tensor, which can be expressed as a three-dimensional matrix. Stress tensor is obviously not a direct observation/measurement of the volume, for general understanding, the stress vector is clearly a more intuitive, more physical meaning of the physical volume, so the known stress tensor stress vector should be a very basic operation.
Known stress tensor, the method to find the positive stress and shear stress on a certain plane is: $$\sigma_n=\hat{n}\cdot \boldsymbol{t}\cdot \hat{n},\quad \tau_n=\sqrt{|\boldsymbol{t The normal stress of}|^2-\sigma_n^2}$$ is the normal vector of stress vector and polygon, and the inner product can also be considered as the two-time product of stress tensor and normal vector. The shear stress and the normal stress direction are orthogonal, the vector and the stress vector, so the size can be obtained by using the Pythagorean theorem. In elastic materials, shear stress is often a very important physical quantity, because many materials (such as rocks, etc.) have a compressive non-shear characteristics, the size of the shear stress will directly determine whether these materials will rupture, how (in which direction) rupture occurs. So it is a basic operation to find the stress tensor to calculate the normal stress and shear stress .
In addition, the stress tensor in the elastomer can always be changed into the form of the spindle coordinate system, the physical meaning is always there are three orthogonal principal stress direction, with these three principal stress direction for the axis, the stress tensor shear component is zero, and its mathematical significance is that stress tensor can always be diagonal. In the main stress axis, there are some advantages in expressing the special brevity, so it is important to find the principal stress direction and the principal stress size of stress tensor .
Now, I have three actions I want to implement. Of course, because MATLAB intuitive human matrix operation syntax, these three operations in the script is easy to implement. For example, I now have a stress tensor T, and three operations can be implemented using the following code:
The% stress vector is required for the line vector T = (t * n). '; % if n is a column vector T = n * t; % if n is the line vector%, the positive stress sigma = N. ' * T * n; % if n is the column vector sigma = n * T * n. '; % if n is the row vector% of the shear stress tau = sqrt (norm (t) ^2-sigma^2); The main stress and its corresponding direction [V, D] = Eig (t);
It doesn't seem to be complicated, but there are four deficiencies: (a) The stress vectors and the normal stress must be judged by whether the input n is a row vector or a column vector, and if it is troublesome to write a branch statement each time, (b) to find the shear stress must be based on the positive stress, This means that each shear stress needs to be handwritten for the operation of the normal stress, which also includes the Judgment statement, (iii) the solution of the main stress is two matrices, if you want to directly get the "principal stress-principal stress direction" structure what to do? (iv) expression is not intuitive, poor readability.
These shortcomings can be solved with a custom function, write a function to put the code in it. But there is another problem: these operations are still not associated! We do not know that these operations are for the same physical quantity-stress tensor. Wouldn't it be flattered to put a class like C/c++/python to hold these functions, and then use T.x () as a way to call them? This is to learn--
Second, the definition of stress tensor matlab class
Now, using the "class" in MATLAB, several operations of stress tensor are assembled together. So first, you need to define a MATLAB class:
% stresstensor.m% keyword ClassDef, followed by a custom class name, storing properties and methods, ending with end classdef Stresstensor% Stress tensor (stresstensor) class% keyword properties: The attribute used to hold the class (member variable), End properties Tensormat End With end % keyword methods: The method used to store the class (member function), ending with end methods% keyword function: This has been seen many times, is used to define the function, the same way as the direct definition function, with end ending% as the constructor, The name and class name are the same, the return value must be the Stresstensor class, so you can write "Item.tensormat" directly, that is, to access its member variables as the Stresstensor class has the function item = stresstensor (A If Nargin = = 0 Item.tensormat = zeros (3, 3); else Item.tensormat = A; End End EndEnd
When defining a class, you must define at least one constructor (as already implemented above), or you may not be able to generate an instance at all. Here's a little trick, nargin in the function is a special keyword, its meaning can be understood as:n-arguments-input, that is, the number of parameters passed in. Using Nargin, it is possible to determine the input of several parameters within the function structure and respond separately, which is equivalent to writing a function to achieve the function overload. In addition, the function of MATLAB in defining its implementation method, can take the variable parameter list Varargin (that is, variable-Arguments-in put), This allows you to identify and manipulate the structure of a function without specifying the number of possible arguments (not to mention the type). Of course, it is certainly not common for varargin to be able to organize only floating-point data vectors; it must be capable of organizing a wide variety of data-cell arrays (cells).
The file that holds the definition of the class is generally the same name as the Class (STRESSTENSOR.M), within which the attribute (member variable) of the class needs to be specified, which is done under the "Properties" keyword (see code); Indicates the method (member function) of the class, in "Method (methods)" Keyword (see the code below). For example, to find stress vectors, to find positive stress, to find shear stress:
function stress = getstress (obj, direction) % for stress vectors % Enter a parameter: the surface method vector direction; % return: Stress vector stress on the plane (line vector) Direction = direction/norm (direction); If size (direction, 1) > 1 stress = (Obj.tensormat * direction). '; else stress = direction * OBJ.TENSORMAT; Endendfunction stressnorm = Getnormal (obj, VEC) % for positive stress % input A parameter: To find the surface method vector Vec; % return: Positive stress stressnorm (row vector) on this face C12/>normval = Obj.getnormalval (VEC); Stressnorm = Normval*vec/norm (VEC); Endfunction stressshear = Getshear (obj, VEC) % to find shear stress % Enter a parameter: Surface method vector vec;< c16/>% return: Shear stress vector stressshear (row vector) vunit = Vec/norm (VEC) on the surface; Stress = obj.getstress (VEC); Stressval = dot (stress, vunit); Stressnorm = Stressval*vunit; Stressshear = Stress-stressnorm;end
And the method of calculating the principal stress:
function Paxis = Principalaxis (obj) % to find the principal stress and its corresponding direction % null input; Returns an array of 3*2 cells, I behavior: the first principal stress-I principal stress corresponding direction Paxis = cell (3, 2); A = Obj.tensormat; [Eigvec, Eigval] = Eig (A); pstress = Diag (eigval); For i = 1:3 paxis{i, 1} = pstress (i); Paxis{i, 2} = Eigvec (:, i). '; EndEnd
We notice that there is a parameter objin the function of the class, which indicates that the function needs to invoke other functions or member variables of the class itself , and that in Python, but in general, the method that belongs to the class (member function) must be included in the process of defining the class. Self This parameter has the same effect. The usage in C + + is similar to the class itself pointer this, but it does not need to be included in the parameter. After the obj parameter is included, it is possible to invoke its own properties and methods within the function, using the syntax obj.property and Obj.method (arguments). As I know, in the definition of the class function in Matlab, obj is not forced to include, but if it is not included, the meaning of this function in the class is not clear, in addition to the overloaded functions in the class, often can be placed outside the class (compared to Python static function).
Overloading and operator overloading of class functions
Finally, when it comes to Matlab, there is no exception to numerical calculations, even for object-oriented programming, and when it comes to numerical computation, operator overloading must be addressed, because only in this way can you really incorporate your own class into a set of computational syntax for MATLAB. operator overloading in MATLAB is much more straightforward than many other languages because each of its operators corresponds to a function, which is no different from a normal function. For example, the + operator, the corresponding function file is PLUS.M, the function name is plus. So in a custom class, operator overloading and function overloading indistinguishable, just rewrite a method with the same name under this class , for example:
function result = Plus (obj1, obj2) A = Obj1.tensormat + Obj2.tensormat; result = Stresstensor (A); end
The + operator overload is implemented as long as the plus is determined from a method that belongs to Stresstensor.
There are specific operators that can be overloaded, and the corresponding function names can be found in the Operator overloading reference page. or the following large table also provides the appropriate information:
Iv. Inspection and actual invocation
Next, just put this series of functions in the class definition body (ClassDef stresstensor) method (methods) keyword can be. We can write a script call:
Import stresstensor.*; % First, import this class and its contents, you can use wildcard characters. * All content under Import class T1 = Stresstensor (); % parameterless constructor, see case Nargin==0 when the constructor definition above is a = 3*eye (3) + diag ([1, 2], 1) + diag ([1, 2], 1); T2 = Stresstensor (A); % with parameter constructor t = T1 + T2; % calls the overloaded addition disp (T.tensormat); % showing stress tensor at this time% output:% 3 1 0% 1 3 2% 0 2 3v = [1, 1, 1];stress = t.getstress ( v); % call to stress vector Method disp (stress);% output:% 2.3094 3.4641 2.8868disp (T.getshearval (v)); % call method to find shear stress% output:% -0.5774 0.5774 -0.0000
The resulting results are in line with expectations.
V. Class functions are split into file and organization mode
Finally, a split of the file.
A class might be large, containing a number of attributes and a large and large-scale approach, which would certainly cause the files we have just written to STRESSTENSOR.M to store the information of the class becoming larger and more chaotic and less manageable. For this, C + + processing method is to put the definition of the class and the declaration of the class function in the header file, the implementation of the function is placed in the source file, and can be completed through a number of source files function implementation. MATLAB is simpler, all functions can be removed intact, cut all the code into a new file is OK, the new file name is set to the name of the function. But! In order for the MATLAB editor to understand that they are all subordinate to a class of functions, all about a class of methods and the definition of the class itself needs to be placed in a folder, the name of the folder "@" + class name, @ is to let the compiler understand that this is a class folder, such as the previous stress tensor, the folder named " @stressTensor ". That's my way of organizing this stresstensor class.
Object-oriented: MATLAB's custom class [Matlab]