1
votes

I am trying to plot contour of the Shifted Schwefel Problem function but keep having this error: Z must be size 2x2 or greater. I have searched on this forum and the information i have helped a little, but could not fix the above error. The information i got from this forum lead me to trying this code:

min = -50;
max = 50;
steps = 20;
c = linspace(min, max, steps); % Create the mesh
[x, y] = meshgrid(c, c); % Create the grid
%o=-50+100*rand(1,2);
%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);

Now i have error that z, which the the value of f most be atleast 2x2 or greater. I know my f is taking only one input and therefore will output only one. I tried having it in a nested for loops, but still giving me a array of vectors not matrix of atleast 2x2. if the input was two, then the problem will be fine, but the problem is, it is one input. Does anyone know how i can make this "f" output a matrix of atleast 2x2 so that i can plot the z of the contour?

1
The first thing I notice when I run your code is an error in your assignment to f (??? Subscript indices must either be real positive integers or logicals.) You can fix this by renaming your max variable to something else (I used maxi). As for the dimensions of f, it isn't exactly clear what you're trying to do. Could you expand your question to give a description what what you need your plot to include? The max function just returns the maximum value along a given dimension, so it will always output vectors - Jacob Robbins

1 Answers

0
votes

There are a few things to note:

1.) As Jacob Robbins pointed out correctly in his comment, you should avoid using names from Matlab functions as variable names (in your case min and max). One very easy way to do this, is to use only upper case letters for variable names.

2.) You are correct in saying that your fis only one output (though one output in this case is not a single number, but a vector). That is, because you don't assign any indexing to it within the loop.

3.) Yes, both contour and surfc need at least 2x2 - because they plot information on a grid, which is itself at least 2x2 in nature.

4.) In your particular case, two loops may not be necessary. You seem to only be manipulating the x-vector and your grid is regular. Hence you might want to try this loop:

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

Now, f will be of size 20x20, which corresponds to your x- and y-grid. Also, now your contour and surfc command will produce plots.

5.) One last comment: The output of your function and the results of a web-search for "Shifted Schwefel function" are very different. But the question if your implementation of the Shifted Schwefel function is correct, should be asked as a new question.