MATLAB: Help generating plot of Zakharov’s function

contourgenetic algorithmmathematicsmatlab functionmatrix manipulationoptimizationsurf

Hi guys,
I have a problem that is probably quite simple but I have been pulling my hair out for a while. I looking for a method of reliably plotting the zakharov function with x constrained to -5 >= x <= 5. Here's what I have so far:
[x,y] = meshgrid(-5:0.1:5,-5:0.1:5);
z = @zakharov;
figure
[C,h] = contour(x,y,z);
I'm having difficulty translating what is in the zakharov.m file to a vectorized form, that produces a grid of the correct size for surf and contour to plot. If possible I would really appreciate a method that I could generalize to multiple dimensions, and use on other built-in optimization functions like schaffers or rastrigins, as I want to try to use them to plot the results from genetic algorithms using these functions as the search space.
Any help on this would be very much appreciated!! Thanks in advance!
Jay

Best Answer

x = -5:0.1:5; y = x;
for i = 1:numel(x), for j=1:numel(y), Z(j,i) = zakh([x(i) y(j)]); end; end
[X Y] = meshgrid(x, y);
surfl(X,Y,Z)