Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Novice programming

0 views
Skip to first unread message

David.R.Gilson

unread,
Feb 14, 2003, 8:26:52 AM2/14/03
to
Hi,
The last few posts I have made was towards making a graphic for a quantum
mechanics lecture.

This is what I have so far...

%This M script generates a Gaussian ellipse with fading red contours

%Generate the colour map
redmap=zeros(64,3);
%Want red to remain constant
redmap(:,1)=1;
%Ensures that green and blue fade away equally.
for i=1:64
redmap(i+64)=1-(i/64);
redmap(i+128)=1-(i/64);
end
%Generate x and y coordinates
[x y]=meshgrid(-5:0.01:5, -5:0.01:5);
%Create Gaussian data, scew the x data to provide elliptical distrobution.
z=exp(-((x.^2)./7)-((y.^2)./2));
%Make a filled contour. Many contour lines so that the colour change is
%almost continuous. Use the set command to remove the patch outlines and
%then apply the colour map we generated at the start of the script.
[C h CF]=contourf(x,y,z,150);...
set(h,'LineStyle','None'); colormap(redmap);

Is there anyway to optimise this further?

Thanks in advance.

--
----------------------------------------
David R Gilson, MPhys AMInstP
Email: da...@davidgilson.co.uk / D.R.G...@maths.hull.ac.uk
Web: http://davidgilson.co.uk
<Hu | MP>, Hull Mathematical Physics Group, University of Hull
"...to boldly think what no one has thought before!"


Brett Shoelson

unread,
Feb 14, 2003, 9:23:57 AM2/14/03
to

"David.R.Gilson" <da...@davidgilson.co.uk> wrote in message
news:v4prl1d...@corp.supernews.com...

> Hi,
> The last few posts I have made was towards making a graphic for a quantum
> mechanics lecture.
>
> This is what I have so far...
>
> %This M script generates a Gaussian ellipse with fading red contours
>
> %Generate the colour map
> redmap=zeros(64,3);
> %Want red to remain constant
> redmap(:,1)=1;
> %Ensures that green and blue fade away equally.
> for i=1:64
> redmap(i+64)=1-(i/64);
> redmap(i+128)=1-(i/64);
> end
> %Generate x and y coordinates
> [x y]=meshgrid(-5:0.01:5, -5:0.01:5);
> %Create Gaussian data, scew the x data to provide elliptical distrobution.
> z=exp(-((x.^2)./7)-((y.^2)./2));
> %Make a filled contour. Many contour lines so that the colour change is
> %almost continuous. Use the set command to remove the patch outlines and
> %then apply the colour map we generated at the start of the script.
> [C h CF]=contourf(x,y,z,150);...
> set(h,'LineStyle','None'); colormap(redmap);
>
> Is there anyway to optimise this further?
>


Hi David,
You can define your redmap in a single line; no need for the loop:

redmap = [ones(64,1), 1-[1:64]'/64, 1-[1:64]'/64]; % OR redmap =
[ones(64,1) repmat(1-[1:64]'/64,1,2)];

The rest looks fine. The only thing I would add is that your contourf
command will take a moment to run. You create a pretty fine mesh of x, y
(and thus z) (1001 x 1001), and then generate only150 contours. If a coarser
mesh of values would suffice, you will get the same figure, faster, by
changing your stepsize in meshgrid to, say, 0.1.

Cheers,
Brett


David.R.Gilson

unread,
Feb 14, 2003, 1:28:21 PM2/14/03
to
Did as you suggested and it flies!

Thanks :)

David.


0 new messages