matlab homework solution

matlab homework solution

Optimizing MATLAB Code: Techniques and Best Practices

1. Introduction to MATLAB Optimization

Before we embark on this adventure, let’s briefly define optimization. In the MATLAB context, optimization can be several things. It can be explicitly passing a parameter to mlint telling it what style checks to apply or not apply. Optimization can be downloading a JIT accelerator to apply to your code. Optimization can be selecting a MATLAB coder project type. Optimizing your code can mean vectorizing code instead of using procedural code. It can be writing your m-tree nodes to enable parfor loops to employ multiple workers. It can be verifying the default true value for the MEX file and forcing immediate computation during the code generation stage. In other words, there are many specific techniques, best practices, modifications, choices, etc., of what to use and how to do it, in order to optimize your MATLAB programs. Be assured, we will cover the vast majority of these procedures and practices in this short document. The fundamental goals, of course, are to make the code faster, smaller (better memory use), and at the same time more robust (maintainable). But the techniques to accomplish this are a large factor galore, as our map of the process will demonstrate.

2. Techniques for Improving Code Efficiency

This chapter describes techniques to improve the execution speed and memory usage of your code. Using MATLAB’s JIT accelerates your code, while considering data types and array size reduces memory usage. You can achieve further improvements using other advanced optimizations including multi-threading, vectorization, memory efficient coding, and profile guided optimizations (PGO). Some techniques are applicable only to certain data types or computation types.

This chapter describes general and system-specific techniques for creating efficient MATLAB code. It is divided into six sections. The first section describes the Just-In-Time (JIT) acceleration that MATLAB performs, providing you with a useful understanding about what has already been taken care of behind the scenes by MATLAB. System-Specific Techniques at the end of the chapter provides an overview of some Apple and Linux OS features that influence optimal execution speed.

3. Utilizing Built-in Functions and Vectorization

Efficient computation is complex and embodies trade-offs that are rewarded with high performance. Therefore, knowledge of strengths and weaknesses of tools and solutions is essential. Using built-in functions makes the code more clear, and they can often provide superior performance. This is because they are written in the optimized language, or have optimized algorithms, appropriate scaling, and protection from common issues resulting in human errors. Vectorization—operations or sets of operations applied to entire data sets—is similar. It is a novel technique, specific for array-based languages such as MATLAB, which allow calculations to be expressed relatively easily in the form of matrix or array operations.

While the loops are the most common language feature in the code, excessive use of loops in MATLAB leads to code that is not efficient. Many MATLAB users assume that operations in MATLAB would be executed in C or Fortran under the hood. In reality, MATLAB is its own language, no matter what engine implements the operation. This language is excellent in vector operations and matrix manipulations, and if you adhere to good practices and use it properly, you will observe that it is a high-performance language. The analysis made in this paper, back-to-back comparison of FORTRAN, Python loop, and Python (Numpy or list comprehension) vectorized algorithms, shows that out-of-the-box MATLAB is not always slower than FORTRAN or Python loops, and that MATLAB code often becomes superior if the built-in functions are used.

4. Memory Management and Preallocation

Use data types that take up less space. If you know that the elements of an array you create will always be 1 through 32, you can save memory by using 5 bits per element instead of the usual 32 bits. By specifying ‘uint32’ when you preallocate, you cast the type of the input to the function, constraining the array elements to the allowable range of 0 to 31. To effectively do this, every element in the array must be checked for this range. If there are out-of-range indices in the array, the preallocation is not only ineffective at saving space, but it also makes the function slower because it has to check every element twice.

Reserve memory-efficient data types such as ‘uint8’ instead of ‘double’ when you preallocate. Other memory-efficient data types that are available to use with your inputs include ‘uint32’ for positive integers and ‘logical’ for binary values.

Consider the space dedicated to avoiding the use of complex numbers. Instead of performing arithmetic operations on complex numbers, another approach is to break the complex numbers into their real and imaginary parts, named r and i, respectively. Then you can loop over the matrices that contain r and i, making the operations you want to do in parallel on the real and imaginary parts.

When preallocating, use simple data types for ranges (i.e., ‘uint32’) if you know that the numbers that will be stored are non-negative integers of values less than 264. For example, if you have preallocated a logical number of rows or columns or slices, you can use it to choose more memory-efficient loops after that factor comes into play.

5. Profiling and Benchmarking for Performance Analysis

If you are dealing with performance issues in MATLAB, or any programming language for that matter, it can be difficult to know where the problem lies. You might suspect that a function is running slowly, but to be sure, you can use a profiler. A profiler is a tool that measures how much time a running program spends executing components of a code base. There are two ways that profiling can be performed, off-line and on-line. On-line profiling refers to the use of a profiler which is integrated into an application such that performance analysis can be done on the fly. Off-line profiling refers to the use of a tool which implements a profiling method that was not built into a program during its design and development. Usually off-line profiling tools use debug information and do not run a program, and upon completion the results of the profiling are visible in the source code.

In MATLAB, the primary tool for performing off-line profiling is the profile viewer. You can create a profile report containing detailed performance characteristics about the execution of a function, and then visualize the results using the profile viewer. The profile report provides statistics about the function and time and the number of calls to functions, and indicates where time is spent. The third-party off-line profiling tool that applies compression or translational lookaside buffer can be utilized to give an impressive speedup. Profile viewers are useful not only for ranking the computational cost of the instructions of the program but also for understanding the other behavior of the program like thread CPU time, number of calls attributed, inclusive time, own time, etc. This tool also provides the facility of displaying the histogram (cumulative time consumed) of any CG-Threads, and histograms of all basic block frequencies, executes block frequency and so on.

Place Your Order
(275 Words)

Approximate Price: $26

Calculate the price of your order

275 Words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total Price:
$31
The price is based on these factors:
Academic Level
Number of Pages
Urgency
Principle features
  • Free cover page and Reference List
  • Plagiarism-free Work
  • 24/7 support
  • Affordable Prices
  • Unlimited Editing
Upon-Request options
  • List of used sources
  • Anytime delivery
  • Part-by-part delivery
  • Writer’s sample papers
  • Professional guidance
Paper formatting
  • Double spaced paging
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)
  • 275 words/page
  • Font 12 Arial/Times New Roman

•Unique Samples

We offer essay help by crafting highly customized papers for our customers. Our expert essay writers do not take content from their previous work and always strive to guarantee 100% original texts. Furthermore, they carry out extensive investigations and research on the topic. We never craft two identical papers as all our work is unique.

•All Types of Paper

Our capable essay writers can help you rewrite, update, proofread, and write any academic paper. Whether you need help writing a speech, research paper, thesis paper, personal statement, case study, or term paper, Homework-aider.com essay writing service is ready to help you.

•Strict Deadlines

You can order custom essay writing with the confidence that we will work round the clock to deliver your paper as soon as possible. If you have an urgent order, our custom essay writing company finishes them within a few hours (1 page) to ease your anxiety. Do not be anxious about short deadlines; remember to indicate your deadline when placing your order for a custom essay.

•Free Revisions and Preview

To establish that your online custom essay writer possesses the skill and style you require, ask them to give you a short preview of their work. When the writing expert begins writing your essay, you can use our chat feature to ask for an update or give an opinion on specific text sections.

A Remarkable Student Essay Writing Service

Our essay writing service is designed for students at all academic levels. Whether high school, undergraduate or graduate, or studying for your doctoral qualification or master’s degree, we make it a reality.