Optimization Techniques in MATLAB
MATLAB has an optimization toolbox that boasts of a collection of functions that extend the capability of MATLAB’s numeric computing environment. This toolbox has routines for several types of optimization, including:
- Unconstrained non-linear minimization
- Constrained non-linear minimization includes goal attainment problems, semi-infinite minimization problems, and minimax problems.
- Linear and quadratic programming
- Curve fitting and non-linear least squares
- Non-linear system of equation solving
- Constrained linear least squares
You can save optimization functions in the MATLAB command line or written as interactive functions.
Standard Algorithms
The current state of the art optimization algorithms are implemented in the optimization toolbox. The BFGS quasi-Newton method and the Nelder-Mea direct search method are the main non-limited minimization algorithms. The Gauss-Newton or Levenberg-Marquardt methods are used to solve non-linear least-squares problems. An active set method combined with imaging techniques is used with routines to solve linear and quadratic programming problems. These routines offer linear research strategies and a range of algorithms. The protected methods of quadratic and cubic interpolation and extrapolation are linear research strategies.
In this article, we are going to discuss the various techniques for optimizing a MATLAB code. You can hire our optimization techniques in MATLAB online experts if you need professional assistance with your homework.
Initialization
We can get a significant performance increase before arrays are filled with data in a loop. However, this is only possible if they are initialized. A chunk of memory is usually allocated before data is filled into it when an array is initialized. A smaller portion that can accommodate data filled in until that point of time is allocated if the array is not initialized.
The previously created chunk needs to be reallocated to a bigger chunk as more data come in. This should happen until it is filled in. So what if you do not know how much space to allocate? This common question has the following solutions:
- Overestimation – Overestimating the size of your matrix and allocating much space is better than under-allocating space
- Deal with it and fix it later – A lot of developers put up with the population time, and then copy the matrix into a new pre-allocated space. This is usually saved as a .mat file. Similarly, it could be read quickly at a later date.
Vectorization
A vector, sometimes called a matrix, is the fundamental data type in MATLAB. Highly optimized vector manipulation libraries like LAPACK and BLAS are often used in MATLAB. Performing operations using vectors is extremely fast compared to using loops to iterate along with the elements of the vectors while performing the same operations.
Looping
It is faster to traverse the matrix column-wise instead of row-wise in cases where it is not possible to vectorize the code. Since MATLAB is column-major, each column is saved in a contiguous block of memory. As a result, traversing through elements column-wise if faster than row-wise.
Parallelization
Most of the basic MATLAB functions are already parallelized. Although there is a lot of scope parallelization, more functions are being added with each release. This mostly happens when vectorization is not possible. We can get a considerable performance increase on multi-core machines if the loops are parallelized. However, the loops in MATLAB are executed in a single thread because this environment is inherently single-threaded.
Direct calls to LAPACK
We already mentioned that MATLAB boast of highly optimized vector manipulation libraries like LAPACK and BLAS. To perform computations, MATLAB functions call methods from these libraries. Accessing these BLAS and LAPACK functions directly can sometimes be useful. Suppose we want to compute the maximum eigenvalue of a large matrix. Since we are only interested in the maximum eigenvalue, the other eigenvalues are of no use to us. However, when we compute the MATLAB code shown below, all the eigenvalues are computed before the maximum is returned. This is simply a waste of resources:
Clear all;
A = randn (5000);
Tic;maxev = max(eig(A)); toc
We can stop the computation once we find our maximum eigenvalue by calling methods from the LAPACK library directly. This can be done by using the method shown below:
Mex –largeArrayDimsmaxeigval.c -lmwlapack
Clear all;
A = randn (5000);
Tic;maxev = max(eig(A)); toc
The code above is specific to the computation of maximum eigenvalues of a positive definite matrix. The result will be incorrect for any other matrices.
Avail of our optimization techniques in MATLAB assignment help if you are struggling with your assignment.