2
votes

I am trying to use octave to do a data approximation project, but I am running into some trouble testing some basic things. I'm not very experienced with octave, so I was hoping someone could point me in the right direction.

I'm attempting to generate a list of x,y points using the Halton sequence. I want to plug those x,y points into the peaks function to get the z peaks values for those coordinates. Next, I want to plot those x,y,z points to be sure that my plot resembles the peak plot, but my plot looks wrong.

Here is the code:

%13 for this example to compare to meshgrid
N = 13;

%creates a halton sequence for N and scales it between -3 to 3
seq = haltonseq(N, 2) * 6 – 3;

%save the x,y coordinate from the halton sequence
X = seq(:,1);
Y = seq(:,2);

%convert the x,y vector in matrixes
X = repmat(X, 1, N)';
Y = repmat(Y,1,N);

%get the z values for x,y
Z = peaks(X, Y);

%plot my x,y,z values
surf(X,Y,Z);

%plot meshgrid to compare to
[mX mY] = meshgrid(-3:0.5:3);
mZ = peaks(mX, mY);
surf(mX, mY, mZ);

The Halton sequence is correct, but I will include it for verification. This is after it has been scaled to be between -3 and 3:

seq =

   0.00000  -1.00000
  -1.50000   1.00000
   1.50000  -2.33333
  -2.25000  -0.33333
   0.75000   1.66667
  -0.75000  -1.66667
   2.25000   0.33333
  -2.62500   2.33333
   0.37500  -2.77778
  -1.12500  -0.77778
   1.87500   1.22222
  -1.87500  -2.11111
   1.12500  -0.11111

Mesh Graph:

Mesh Graph

Halton Graph N = 13:

enter image description here

Halton Graph N = 300:

enter image description here

Now, to the best of my understanding of this code, the Halton sequence plot should not look like it does, and as I increase N, it should resemble the meshgrid graph more, but it keeps its same deformed shape. I'm not sure where I am going wrong, can someone point me in the right direction?

Note: I am using this code with Octave now, but I will also be using it with Matlab later, so I tagged it as both.

1

1 Answers

4
votes

A quick look at your X and Y reveals the problem:

octave:33> X
X =

   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500
   0.00000  -1.50000   1.50000  -2.25000   0.75000  -0.75000   2.25000  -2.62500   0.37500  -1.12500   1.87500  -1.87500   1.12500

octave:34> Y
Y =

  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000  -1.00000
   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000
  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333  -2.33333
  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333  -0.33333
   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667   1.66667
  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667  -1.66667
   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333   0.33333
   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333   2.33333
  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778  -2.77778
  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778  -0.77778
   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222   1.22222
  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111  -2.11111
  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111  -0.11111

As you can see, they are not sorted properly. When plotting, order (usually) matters; see below for an explanation. Adding the following two lines after the repmat fixes it:

X = sort(X,2);
Y = sort(Y,1);

Here's the result:

enter image description here


Explanation

The problem is that, when plotting graphs where lines have to be drawn between points, ordering matters. To illustrate, consider this small example:

x1 = [1 2 3 4];
x2 = [3 1 4 2];
plot(x1,x1.^2);
plot(x2,x2.^2);

These two will plot the exact same points, but in a different order. The lines joining them will hence look different:

Plot for x1:

enter image description here

Plot for x2:

enter image description here