MATLAB Community

MATLAB, community & more

Twenty: A Birthday, a Contest, an Explosion of Images

As I said in my last post, here at the MathWorks Community blog we are currently celebrating an important milestone: MATLAB Central is 20 years old!
It's a fine number, 20. Certain numbers just come across as friendly, and I've always been fond of 20. It's hanging out at the corner of Binary and Decimal, smiling pleasantly to everyone who walks by. What can we do to honor it and MATLAB Central? What are some of the factors that make 20 so admirable? I can think of a few.
factor(20)
ans = 1×3
2 2 5
Its literal factors make it clear that 20 is a so-called pronic number. That just means it takes the form n*(n+1).
4*5
ans = 20
That's pronicity for you. I love how you can take an uninteresting fact about a number, give it a fancy name, and suddenly it becomes more interesting [strokes goatee, saying "Yes, pronic. Yes."].
Now, what can we do with those factors?
d = divisors(20)
d = 1×6
1 2 4 5 10 20
sum(d)
ans = 42
This is fun. The answer to life, the universe, and everything may be 42, but what is the question? Now you know: it's "What is the sum of the divisors of 20?" (Note for those of you following along at home: divisors is from the Symbolic Math Toolbox.)
I wrote a short poem of admiration for 20. It goes like this.
Two, four, six, eight!
Who do we appreciate?
sum([2 4 6 8])
ans = 20
Yes, it's our friend 20. We can refactor our code and our poem like so.
Twice the sum of one through four!
What's the number we adore?
2*sum(1:4)
ans = 20
The sum of 1 through n is the nth triangular number. So we notice that 20 is twice the fourth triangular number. And 20 is itself the fourth tetrahedral number. Watch how these "figurate" numbers accumulate.
unit = [1 1 1 1]
unit = 1×4
1 1 1 1
linear = cumsum(unit)
linear = 1×4
1 2 3 4
triangular = cumsum(linear)
triangular = 1×4
1 3 6 10
tetrahedral = cumsum(triangular)
tetrahedral = 1×4
1 4 10 20
Two, four, six, eight!
How do we accumulate?
Okay, I'll lay off the poetry. But all this accumulation reminds me that this same set of numbers can be found in Blaise Pascal's favorite triangle. And there, surveying all, seated serenely in the bottom right corner, is our guest of honor.
pascal(4)
ans = 4×4
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20

Twenty Characters

Those are some things we can do with the number 20. But enough number theory! What can we do with 20 characters of code? Here's a nifty plot that uses only 15 characters. The default colors make for a nice Mexican blanket effect. Call it an area rug. Heh.
% 20 char. limit =>|
area(magic(20))
It looks better without the axis on, but that would put me over the character limit.
Here's one in honor of Cleve Moler, the father of MATLAB. Behold, 20 characters that reveal the Moler matrix of order 20.
% 20 char. limit =>|
gallery("moler", 20)
ans = 20×20
1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-1 0 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
-1 0 1 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
-1 0 1 2 5 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
-1 0 1 2 3 6 4 4 4 4 4 4 4 4 4 4 4 4 4 4
-1 0 1 2 3 4 7 5 5 5 5 5 5 5 5 5 5 5 5 5
-1 0 1 2 3 4 5 8 6 6 6 6 6 6 6 6 6 6 6 6
-1 0 1 2 3 4 5 6 9 7 7 7 7 7 7 7 7 7 7 7
-1 0 1 2 3 4 5 6 7 10 8 8 8 8 8 8 8 8 8 8
I need another two lines to show you what it looks like. Pretty easy on the eyes, eh? But beware! It has one very small eigenvalue that can easily upset naive elimination methods. Or so I'm told.
imagesc(ans)
axis square

Twenty Lines

What can we do with 20 lines of code? This little Mandelbrot set just fits. All without even cheating by cramming multiple statements onto one line of code.
p = 500;
x0 = -0.6;
r = 1.5;
k = 50;
x = linspace(x0-r, x0+r, p);
y = linspace(-r, r, p);
[X, Y] = meshgrid(x, y);
Z = X + 1i*Y;
m = k*ones(size(Z));
z = Z;
for n = 0:k
z = z.*z+Z;
h = abs(z)>2;
z(h) = NaN;
m(h) = n;
end
imagesc(m)
colormap([parula(k-1);0 0 0])
axis equal
axis off

A Contest of Characters: the MATLAB Mini Hack

Playing around with numbers, code, and images is fun, but there is a point to my meandering. I want to introduce an exciting anniversary celebration contest that we're going to launch next week. We call it the MATLAB Mini Hack. It's a constrained character-count coding contest. And as long as I'm alliterating, I can tell you that it is also collaborative and colorful.
The contest centers around this question: what fun image can you make in MATLAB using no more than 280 characters of code? I'll give you an example. Weighing in at only 278 characters, the Mandelbrot code shown above fits the bill.
We're calling the Mini Hack a contest, but it's really meant to be mostly about the fun. With that in mind, we encourage you not only to contribute your original ideas, but also to steal, or rather "remix", other people's code. Build on top of their ideas, and make even more colorful mischief. And if you see someone remixing your code, remember: that's a good thing! Be proud.
I'm so excited about this contest, I'll be doing a live Instagram event this Friday (1 October) at 10am EST. I'll be talking about the history of MATLAB Central, how we are celebrating the 20th Anniversary, and everything you need to know about this crazy, kaleidoscopic contest.
Please join me at http://instagram.com/matlab.
Speaking of kaleidoscopes, I'll close with 280 characters of rotationally symmetric hgtransformed joy.
x = linspace(0, 4*pi);
y = 0.5*x.*sin(x);
h1 = plot(x, y, LineWidth=6);
k = 20;
c = hsv(k);
for n = 1:k
h2 = copyobj(h1, gca);
t = hgtransform;
set(h2,Parent=t,Color=c(n,:))
theta = n*2*pi/k;
rot = makehgtform(ZRotate=theta);
set(t, Matrix=rot);
end
axis equal
axis off

|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。