Obtain an incorrect mathematical value after the Matlab code converted to C #

advertisements

I am using a method which was generated using the TableCurve program and written in Matlab. I am trying to convert the method into C# to use in my project but it is returning incorrect values. I think I have narrowed down the problem to a particular spot, the original code is

iv=1;
for j=1:1:tcnt
    for m=j:-1:1
      v(iv)=tx(m)*ty(j-m+1);
      iv=iv+1;
    end
end

where tx, ty, and v are lists of numbers and iv is a counter. and I have translated it into

n = 0;
for (int j = 1; j <= tcnt; j++)
{
    for (int m = j; m >= 1; m--)
    {
        v[n] = tx[m - 1] * ty[j - m];
        n++;
    }
}

Does anyone see a problem with my indices? because I do not. If there is no problem with my indices I will edit this question to try and locate the actual problem area.

Edit

This is the entire working Matlab code (after variables are set up)

 tx(1)=1.0;
 ty(1)=1.0;
 tx(2)=x;
 ty(2)=y;
 for j=2:1:tcnt-1
     tx(j+1)=2*x*tx(j)-tx(j-1);
     ty(j+1)=2*y*ty(j)-ty(j-1);
 end
 iv=1;
 for j=1:1:tcnt
     for m=j:-1:1
         v(iv)=tx(m)*ty(j-m+1);
         iv=iv+1;
     end
  end
  z=0.0;
  for j=1:1:order+1
      z = z + p(j)*v(j);
  end

And this is my C# code

tx[0] = 1.0;
ty[0] = 1.0;
tx[1] = x;
ty[1] = y;

for (int j = 2; j <= tcnt; j++)
{
    tx[j] = 2 * x * tx[j - 1] - tx[j - 2];
    ty[j] = 2 * y * ty[j - 1] - ty[j - 2];
}

n = 0;

for (int j = 1; j < tcnt; ++j)
{
    for (int m = j; m >= 1; --m)
    {
        v[n] = tx[m] * ty[j - m + 1];
        n++;
    }
}

z = 0.0;

for (int j = 0; j <= order; j++)
{
    z += constantList[j] * v[j];
}


What the bleep are you doing fitting data with a Chebyshev polynomial? Use a Fourier series, you already have the equation written out and tested to everyone's satisfaction. And the equation is bounded to the curve so you don't have to worry about discontinuities outside the range you have test data for. Hey, I have a good idea... I will do that.