Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Include Page
SIMULATION: MATLAB Google AnalyticsSIMULATION:
MATLAB Google Analytics

The Need for Array Pre-Allocation

Include Page
Spring-Mass System - Panel
Spring-Mass System - Panel

Step 3: Plotting

Let's visualize our results by making a plot of the mass position x vs. time using the steps shown The MATLAB code analyzer produces two warnings about our x and t arrays changing size on every iteration as demonstrated in the video below.

HTML
 <iframe width="600" height="338" src="//www.youtube.com/embed/N9JEnT76BKYixSPeMxjODw?rel=0" frameborder="0" allowfullscreen></iframe>

The following video briefly explains why array pre-allocation leads to faster code. See this blog-post for more information about pre-allocation.

 


Create Professional-Looking Plot

We'll make a few tweaks to the plot to prettify it and make it look more professional. We'll do this using the following template.

figure(1); %Create figure #1
clf; %Clear current figure
h=plot( , ,' ');
set(h,'LineWidth',2); %Set linewidth for curve
set(gca,'Box','on','LineWidth',2,...
   'FontName','Helvetica',...
   'FontSize',14); %Set axis properties
xlabel('');
ylabel('');
title('');
axis square; %Make axis box square


The following video shows you how to implement this template.

HTML
 
HTML
<iframe width="600" height="450338" src="//www.youtube.com/embed/iSVEN_feRKA6i0-X9h9bjM?rel=0" frameborder="0" allowfullscreen></iframe>

Pre-Allocation Procedure

 


Note that we use the variable h for the plot handle in the above video. But we have also used h for the time step. Oops! In this case, this doesn't cause an error since plotting occurs at the end. But it'd be better to call the plot handle, say, hplot in your code.

We next change the axis limits from the default using the axis function so that our curve fills up more of the plot areaWe pre-allocate the x and t arrays using the zeros function as shown below.

HTML
 <iframe width="600" height="338" src="//www.youtube.com/embed/BFR8WjtYwNgE77XSRcjfcE?rel=0" frameborder="0" allowfullscreen></iframe> 

Change in Run-Time due to Pre-Allocation


Add Analytical Solution to Plot

First, we form the necessary arrays from the analytical solution.

HTML
 <iframe width="600" height="338" src="//www.youtube.com/embed/DAlTSfTfJl0?rel=0" frameborder="0" allowfullscreen></iframe> 


Second, we add a curve corresponding to the analytical solution to our figureWe next use the "profiler" in MATLAB to check how much the run-time decreases due to pre-allocation.

HTML
 <iframe width="600" height="338" src="//www.youtube.com/embed/bfqQTGe_pxUk9cojj_oETU?rel=0" frameborder="0" allowfullscreen></iframe> 


Lastly, we add a legend to label the curves.

HTML
 <iframe width="600" height="338" src="//www.youtube.com/embed/trTvzt3cVIo?rel=0" frameborder="0" allowfullscreen></iframe> 



Go to Step 4: Function Creation

Go to all MATLAB Learning Modules