Some MATLAB tips (2)

Source: Internet
Author: User

Tips4.Efficiency of inline functions, anonymous functions, nested functions, and m-file functions in MATLAB.Basically, MATLAB functions are divided into two types: anonymous function and M-file function ). Next, we will illustrate the application and Efficiency Comparison of various functions from an example. Problem description: Now I want to draw a stable area of the numerical algorithm, that is, all the Enable equations a (u) in the rectangular area [-;-30; 30] in the complex plane) * X2 + B (U) * x + C (u) = 0 is followed by a drawing of the U value smaller than 1. Programs written using anonymous functions are as follows: Clear all
TIC
% Coefficient. After the denominator is removed, it can be considered that it is the same solution as the original equation.
A = @ (u) exp (u) * (-1 + exp (u)-2 * u + u * exp (u ));
B = @ (u)-exp (u) * (-1-2 * u + exp (2 * u ));
C = @ (u) exp (2 * u) * (-1 + exp (u)-U );
% Root formula
Root1 = @ (u)-B (u) + SQRT (B (U) ^ 2-4 * a (u) * C (u ));
Root2 = @ (u)-B (U)-SQRT (B (U) ^ 2-4 * a (u) * C (u ));
% Variable Initialization
T = 1;
Result = zeros (400*600,1 );
% Traverse the region [-;-30: 30]. If the vertex meets the condition, the value is stored.
For II =;
For JJ =-30: 0.1: 30;
If (ABS (root1 (II + I * JJ) <ABS (2 * a (II + I * JJ ))...
& ABS (root2 (II + I * JJ) <ABS (2 * a (II + I * JJ )))
Result (t) = II + I * JJ;
T = t + 1;
End
End
End
Plot (result ,'.')
TOC running result: elapsed time is 86.485000 seconds. M-file function and nested function program: function R1 = root12 (u)
% Calculate the two roots of the equation using the root equation
R1 = [(-B (u) + SQRT (B (U) ^ 2-4 * a (u) * C (u)/(2 * a (u) + EPS ),
(-B (U)-SQRT (B (U) ^ 2-4 * a (u) * C (u)/(2 * a (u) + EPS)]; function FUNA = a (u) % subfuntion
% Coefficient a (u)
FUNA = exp (u) * (-1 + exp (u)-2 * u + u * exp (u ));
End function funb = B (U) % subfunction
% Calculate coefficient B (U)
Funb =-exp (u) * (-1-2 * u + exp (2 * u ));
End function func = C (u) % subfunction
% Calculate coefficient C (u)
Func = exp (2 * u) * (-1 + exp (u)-U );
Endend clear all
TIC
% Variable Initialization
T = 1;
Result = zeros (400*600,1 );
% Traverse the region [-;-30: 30]. If the vertex meets the condition, the value is stored.
For II =;
For JJ =-30: 0.1: 30;
If (min (ABS (root12 (II + I * JJ) <1 )~ = 0)
Result (t) = II + I * JJ;
T = t + 1;
End
End
End
Plot (result ,'.')
TOC running result: elapsed time is 36.328000 seconds. You can directly write the formula in the M script without using a function: Clear all; tic % variable initialization.
T = 1;
Result = zeros (400 *); % traverses the region [-;-30: 30]. If the vertex meets the condition, the value is stored.
For II =;
For JJ =-30: 0.1: 30;
% IF (ABS (root1 (II + I * JJ) <ABS (2 * a (II + I * JJ ))...
% & ABS (root2 (II + I * JJ) <ABS (2 * a (II + I * JJ )))
If (ABS (exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ )))...
+ SQRT (-exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ) ^ 2 )...
-4 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ) * (exp (2 * (II + I * JJ )) * (-1 + exp (II + I * JJ)-(II + I * JJ )))))...
<ABS (2 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ ))))...
& ABS (exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ )))...
-SQRT (-exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ) ^ 2 )...
-4 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ) * (exp (2 * (II + I * JJ )) * (-1 + exp (II + I * JJ)-(II + I * JJ )))))...
<ABS (2 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ )))))
Result (t) = II + I * JJ;
T = t + 1;
End
End
Endplot (result, '.') TOC running result: elapsed time is 26.578000 seconds. The result is obvious. Writing the formula directly in the M file is much faster than using the function. This is mainly because the system needs to perform additional stack-to-stack and value-transfer operations when calling a function. Inline functions are not considered here, because they are calculated based on symbols and are less efficient than anonymous functions. However, the formula is directly written in M files, which greatly reduces the readability, reusability, and maintainability of the program. The compromise can only be a complex formula with many calls or an M file function. Of course, if you drop the formula with a small number of calls, it is relatively simple, it is also natural to use anonymous functions.     Tips5.Another small tip about the above Code. This was accidentally discovered. Make the following minor changes to the above Program (for more information, see); clear all; tic % variable Initialization
T = 1;
% Result = zeros (400 *); % traverse the region [-;-30: 30]. If the vertex meets the condition, the value is stored.
For II =;
For JJ =-30: 0.1: 30;
% IF (ABS (root1 (II + I * JJ) <ABS (2 * a (II + I * JJ ))...
% & ABS (root2 (II + I * JJ) <ABS (2 * a (II + I * JJ )))
If (ABS (exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ )))...
+ SQRT (-exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ) ^ 2 )...
-4 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ) * (exp (2 * (II + I * JJ )) * (-1 + exp (II + I * JJ)-(II + I * JJ )))))...
<ABS (2 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ ))))...
& ABS (exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ )))...
-SQRT (-exp (II + I * JJ) * (-1-2 * (II + I * JJ) + exp (2 * (II + I * JJ) ^ 2 )...
-4 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ) * (exp (2 * (II + I * JJ )) * (-1 + exp (II + I * JJ)-(II + I * JJ )))))...
<ABS (2 * (exp (II + I * JJ) * (-1 + exp (II + I * JJ)-2 * (II + I * JJ) + (II + I * JJ) * exp (II + I * JJ )))))
Result (t) = II + I * JJ;
T = t + 1;
End
End
Endplot (result, '.') Does TOC show it? In fact, % result = zeros (400 *, 1); is switched off; & changed &. next, let's see there is a key above your Current Directory window with a small check mark √: Click it and go back to the interface below. zmy_stablearea_2.m is the file name of the above script: see it! In fact, this is a code check tool. If it is not a good practice, it will prompt for modification. For example, in the above program, if the 31st rows are not modified, the running result is: elapsed time is 63.984000 seconds. This is much slower than the 26.578000 seconds after modification! This is easy to understand. One is to dynamically allocate memory for array elements, and the other is pre-allocate, which is obviously faster. This is a typical practice of changing the space time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.