Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting 'Hi' in MATLAB

Tags:

matlab

I wished to plot this 'Hi', which may be 'Hello World' graphical equivalent in MATLAB. Haven't been able to do it. Any suggestions are welcome.

like image 997
Arkapravo Avatar asked Jun 11 '10 04:06

Arkapravo


2 Answers

Here is a code for the plot using the formula on the linked page and specified axis limits. You can play with colormap, view direction and other properties to get closer.

x = linspace(-3,3,50);
y = linspace(-5,5,50);
[X Y]=meshgrid(x,y);
Z = exp(-X.^2-Y.^2/2).*cos(4*X) + exp(-3*((X+0.5).^2+Y.^2/2));
Z(Z>0.001)=0.001;
Z(Z<-0.001)=-0.001;
surf(X,Y,Z);
colormap(flipud(cool))
view([1 -1.5 2])

cool MATLAB screenshot

like image 112
yuk Avatar answered Nov 11 '22 05:11

yuk


It seems @yuk beat me to it, still this is my version:

[x y] = meshgrid( linspace(-3,3,50), linspace(-5,5,50) );
z = exp(-x.^2-0.5*y.^2).*cos(4*x) + exp(-3*((x+0.5).^2+0.5*y.^2));
idx = ( abs(z)>0.001 );
z(idx) = 0.001 * sign(z(idx));

figure('renderer','opengl')
patch(surf2patch(surf(x,y,z)), 'FaceColor','interp');
set(gca, 'Box','on', ...
    'XColor',[.3 .3 .3], 'YColor',[.3 .3 .3], 'ZColor',[.3 .3 .3], 'FontSize',8)
title('$e^{-x^2 - \frac{y^2}{2}}\cos(4x) + e^{-3((x+0.5)^2+\frac{y^2}{2})}$', ...
    'Interpreter','latex', 'FontSize',12)

view(35,65)
colormap( [flipud(cool);cool] )
camlight headlight, lighting phong

screenshot

like image 37
Amro Avatar answered Nov 11 '22 07:11

Amro