Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Include Page
SIMULATION: ANSYS MATLAB Google AnalyticsSIMULATION: ANSYS
MATLAB Google Analytics
Widget Connector
width600
urlhttps://www.youtube.com/watch?v=uXURYBVEv-c
height370
Include Page
Spring-Mass System - Panel
Spring-Mass System - Panel

Step 2: Array Pre-Allocation

The Need for Array Pre-Allocation

The MATLAB code analyzer produces two warnings about our x and t arrays changing size on every iteration as demonstrated in the video below. These warnings can be addressed by pre-allocating the memory for these arrays.

HTML
<iframe width="600" height="338" src="

...

//www.youtube.com/embed/

...

The following video shows how to implement the Euler's integration method for one time-step.

N9JEnT76BKY?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.

HTML
<iframe width="600" height="450" src="

...

//www.youtube.com/

...

We extend our script to perform the Euler integration over multiple time-steps by looping over the appropriate statements as shown in the video below.

embed/iSVEN_feRKA?rel=0" frameborder="0" allowfullscreen></iframe>


Pre-Allocation Procedure

We 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/

...

Our script saves only the final location of the mass. We are also interested in saving the intermediate locations so that we can make a plot of position vs. time. We do this by creating arrays for the position and time as shown in the video below.

BFR8WjtYwNg?rel=0" frameborder="0" allowfullscreen></iframe> 


Change in Run-Time due to Pre-Allocation

We 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_pxU?rel=0" frameborder="0" allowfullscreen></iframe> 



Go to Step 3: Plotting

Go to all MATLAB Learning Modules

...