MATLAB Compiler    

A Simple Example - The Sierpinski Gasket

Consider an M-file function called gasket.m.

How the Function Works

This function determines the coordinates of a Sierpinski Gasket using an Iterated Function System algorithm. The function starts with three points that define a triangle, and starting at one of these points, chooses one of the remaining points at random. A dot is placed at the midpoint of these two points. From the new point, a dot is placed at the midpoint between the new point and a point randomly selected from the original points. This process continues and eventually leads to an approximation of a curve.

The curve can be graphed in many ways. Sierpinski's method is:

gasket.m is a good candidate for compilation because it contains a loop. The overhead of the for loop command is relatively high compared to the cost of the loop body. M-file programmers usually try to avoid loops containing scalar operations because loops run relatively slowly under the MATLAB interpreter.

To achieve a reasonable approximation of the Sierpinski Gasket, set the number of points to 50,000. To compute the coordinates and time the computation, you can use

To display the figure, you can use


 Getting Started with MEX-Files Invoking the M-File