How to Plot contours of function

Hi, Does anyone know how i can plot contour of this function?

Here is what i tried but it did not work.

x = -50 + 100.*rand(50,2);

function f = func(x)
D = 2;
[ps, D] = size(x);
o=-50+100*rand(1,D);
x=x-repmat(o,ps,1);
f = max(abs(x), [], 2);
end

This is how i tried doing it:

cmin = -50;
cmax =  50;
steps = 20;
c = linspace(cmin, cmax, steps); % Create the mesh
[X1, X2] = meshgrid(c, c)
contour(X1,X2,f);

Open in new window


Any idea how i could solve this.  Does any one know how to plot contour of functions which takes in one value like the above. Any time i try to plot the above function, it tells me that z must be atleast a 2x2. But i am taking in only one input and giving out a vector, how can it give out a matrix of atleast 2x2 inorder to plot the contour?
wish_CAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wish_CAuthor Commented:
Anybody have an idea on this please?
0
d-glitchCommented:
A contour plot typically shows lines of equal value over a two dimension area.
Elevation above sea level as a function of longitude and latitude is one example.

What is your two dimensional map?
What are the units of your equal value lines?

Contours are usually associated with continuous functions?  
What is the rand() function for?

At a more basic level, what are you hoping to accomplish?
0
wish_CAuthor Commented:
Thanks d-glitch. Basically i have a function name func in my code above. I generate a random number 50 x 2 matrix using rand() from here: So the rand() is to generate some random values.
x = -50 + 100.*rand(50,2);

Open in new window

This random matrix is going to be passed to the function func like this:
eva = func(x)

Open in new window

The function returns an array for vectors which are 50 x 1. I try to plot contour of the resulting eva by doing This: My 2 dimensional map is This [X1, X2] = meshgrid(c, c) as it is shown here:
cmin = -50;
cmax =  50;
steps = 20;
c = linspace(cmin, cmax, steps); % Create the mesh
[X1, X2] = meshgrid(c, c)
contour(X1,X2,eva);

Open in new window

The unite of my equal value line is:
steps = 20;

Open in new window

But any time i run this, it tell me that eva, has to be a matrix of atleast 2x2. As it is now it just give a matrix of 50 x 1. I need eva to be atleast 2x2 matrix so that i can plot the contour. I am now seeking for advice how to do this. Is there a way i can plot the contour? Any suggestion?
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

d-glitchCommented:
If you know the elevations at the corners of section of a map, you may be able to map the contour lines inside that section.

If you want to draw contours at 10 foot intervals, you interpolate along the edges to find all the interval values and then connect the points.

I have attached an example for a simple 2x2 matrix that represents one small map element.

I don't know what contours on a 1-D array could possibly mean.
Could you make a drawing showing the sort of plot you would like to see?
Contour-Plot-for-ExEx.pdf
0
wish_CAuthor Commented:
Attached is what i am trying to achieve.  I am trying to come up with a 3-D map for a 2-D function as shown in the attachment. The eva is supposed to be the third dimension which according to contour plot should be atleast a 2x2 matrix. My problem is eva is a 50x1.
SampleSchwefel.jpg
0
d-glitchCommented:
In your example, the data file is probably a 2D matrix with dimensions of 21x21.  It could have finer or coarser resolution.

But at 21x21, there would be 441 data values representing the z-axis value/height of the function at
  x = -100, -9-, -80, ...0, 10, 20, ... 100    and    y = -100, -9-, -80, ...0, 10, 20, ... 100

In your example, the contour plot is the flat 2D plot at z=-460, not the colorful squarish cone that rises over it.

There are no random variables in your example.

Your first step should probably be to make a 2D matrix that samples your function at discrete points.  Once you have a function, you can think about ways to plot it.

And I still don't know what you are trying to do or why you are trying to do it.
Are you really trying to do something with Genetic Algorithms and the Schwefel function?
0
wish_CAuthor Commented:
Here is my attempt
M = 50;   
n = 2;
x = -50 + 100.*rand(M,n);

cmin = -M;
cmax =  M;
ngrid = 5.05;
x = cmin:ngrid:cmax;
y = cmin:ngrid:cmax;

o     = -50+100*rand(1,n);
y     = y - repmat(o,1,10);
x     = x - repmat(o,1,10);

f = arrayfun(@tteee1,x);

figure;
contour(x,y,f);

figure;
surfc(x, y,f);
title(sprintf('The Surface and contour Plot'))

Open in new window

:

The tteee1 function is here:
function fit = tteee1(x)
D = 2;
[ps, D] = size(x);
o=-50+100*rand(1,D);
x=x-repmat(o,ps,1);
fit = max(abs(x), [], 2);
end

Open in new window


when i run the program, it gives me this error:
??? Error using ==> contour at 73
Z must be size 2x2 or greater.

Error in ==> tteee at 20
contour(x,y,f);
0
d-glitchCommented:
Look at your Workspace in the top left window.  
What are the dimensions of your arrays?

Try this:    

data = 20*rand([10,10])
figure;
contour(data);

Open in new window

0
wish_CAuthor Commented:
My x and y array each have 1x20 dimension. and f is also 1x20. I need a strategy that cam make f 2x2 or more. say 20x20. In this way i can plot the contour just like the example you as me to try. I am trying to plot contour of function tteee1 above.
0
wish_CAuthor Commented:
Here is what i have done so far:

cmin = -M;
cmax =  M;
steps = 20;
c = linspace(cmin, cmax, steps); % Create the mesh
[x, y] = meshgrid(c, c); % Create the grid
o=-50+100*rand(1,n);
c = c - repmat(o,1,10);


f = arrayfun(@tteee1,x);

figure,
contour(x,y,f);

figure,
surfc(x, y,f);

Open in new window


This give a results as attached which looks different from the one i attached earlier which i am trying to achieve.

Do you know why they look different? Am trying to plot contour and contour surface of This function:
function f= tteee1(x)
D = 2;
[ps, D] = size(x);
o=-50+100*rand(1,D);
x=x-repmat(o,ps,1);
f = max(abs(x), [], 2);
end

Open in new window


I am not sure if arrayfun are appropriate for this task. If there are other appropriate method to achieve my goal, they are welcome.
PlotResult.pdf
0
d-glitchCommented:
Try calculating your data matrix using nested for loops:
for x = 1:20
    for y = 1:20
        hilbert(x, y) = 1/(x+y -1);
    end
end

Open in new window

This is an example from the Matlab documentation.
0
wish_CAuthor Commented:
I actually tried the nested for loops but had the same problem. See the code maybe you will spot what i was doing wrong:

cmin = -M;
cmax =  M;
steps = 20;
c = linspace(cmin, cmax, steps); % Create the mesh
[x, y] = meshgrid(c, c); % Create the grid
%o=-50+100*rand(1,n);
%c = c - repmat(o,1,10);

for I=1:length(x)
    for J=1:length(y)
    o=-50+100*rand(1,2);
    x=x-repmat(o,20,10);
    f = max(abs(x), [], 2); 
    end
end

figure,
contour(x,y,f);

figure,
surfc(x, y,f);
                                            

Open in new window


This still gives me the error:
Z must be size 2x2 or greater

The example you give involves x and y, which will surely work in a for loop, but in my case i only have one input which is x. How can it have the function f to output atleast a 2x2 matrix when only one input is given?
0
wish_CAuthor Commented:
Are you really trying to do something with Genetic Algorithms and the Schwefel function?
Yes, i am actually try to implement Estimation of Distribution Algorithm (EDA) in Evolutionary Computation to understand it. I am trying to test it on Benchmark functions in 2d and i am having trouble with plotting contour of some of them, and Schwefel function is one of them.
0
wish_CAuthor Commented:
I have fix the error. Thanks you all.
0
wish_CAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for wish_C's comment #a39287043

for the following reason:

I solve it myself.
0
d-glitchCommented:
...
0
d-glitchCommented:
Please post your solution.  It should be part of the closed question.
0
wish_CAuthor Commented:
Here is the code that worked for me.

SWxlimits = [-100 100];
SWbias = [20 -20];
SWdx = (SWxlimits(2) - SWxlimits(1)) / 100;
SWN = floor((SWxlimits(2) - SWxlimits(1)) / SWdx) + 1;
Schwefel221objShifted = zeros(SWN, SWN);

SWi = 0;
for SWx = SWxlimits(1) : SWdx : SWxlimits(2)
    SWi = SWi + 1;
    SWk = 0;
    for SWy = SWxlimits(1) : SWdx : SWxlimits(2)
        SWk = SWk + 1;
        Schwefel221objShifted(SWi,SWk) = max(abs(SWx-SWbias(1)), abs(SWy-SWbias(2))); 
    end
end
[SWx, SWy] = meshgrid(SWxlimits(1) : SWdx : SWxlimits(2));

figure;
contour(SWx,SWy,Schwefel221objShifted);
figure;
figure;
surfc(SWx,SWy,Schwefel221objShifted);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wish_CAuthor Commented:
My code is now giving the right output as expected.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.