0

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];
}
kleineg
  • 462
  • 1
  • 6
  • 18
  • 2
    Be more specific. *What* is returning incorrect values? The loop doesn't run as long as it should? Incorrect values are being assigned to `v[n]`? What numbers do you expect to see? What did you see instead? Did you use the debugger to run through the loop step by step? – tnw Jun 30 '14 at 16:08
  • Have you tried to run this particular code snippet both in C# and Matlab with some predefined small tx and ty? – Eugene Podskal Jun 30 '14 at 16:10
  • @tnw I do not actually know what the values of each v[n] should be because I cannot run the Matlab code. I do know their weighted sum is incorrect (using the same weighting list) – kleineg Jun 30 '14 at 16:11
  • Well, you could try to interpret the Matlab code on your own and compare it with the C# code execution. Just use some tx {1,2,3,4....} and ty {101, 102, 103....}. – Eugene Podskal Jun 30 '14 at 16:15
  • So basically use a calculator to compute the Matlab code? This is the crux of the issue, I do not have Matlab, I have a application where I fit data and it generates Matlab code with a curve fitting equation (Chebyshev polynomials). Then I translate the code into C# to be used. – kleineg Jun 30 '14 at 16:17

2 Answers2

0

The only difference I can see is that matlab indices start at 1, while c# indices start at 0, so the following should be equivalent:

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

Also, the first matlab loop runs from 2 to tcnt - 1, while the C# version runs from 2 to tcnt.

Dennis_E
  • 8,751
  • 23
  • 29
0

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.

kleineg
  • 462
  • 1
  • 6
  • 18