15,907,906 members
1.00/5 (1 vote)
See more:
I am working on a project where an error file for circular motion will come from some 3rd party source at 0.1-degree division.
I want to use a numerical technique to interpolate and get the points at 0.09-degree division. (or at any user-defined degree)

Can someone tell me the issue with current code

What I have tried:

I have tried the below-using c#, but not getting the desired result.
C#
```class Program
{
static float proterm(int i, float value, float[] x)
{
float pro = 1;
for (int j = 0; j < i; j++)
{
pro = pro * (value - x[j]);
}
return pro;
}

static void dividedDiffTable(float[] x, float[,] y, int n)
{
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
y[j, i] = (y[j, i - 1] - y[j + 1, i - 1]) / (x[j] - x[i + j]);
}
}
}
static float applyFormula(float value, float[] x, float[,] y, int n)
{
float sum = y[0, 0];
for (int i = 1; i < n; i++)
{
sum = sum + (proterm(i, value, x) * y[0, i]);
}
return sum;
}

// Driver Function

public static void Main()
{
List<float> errorValues = new List<float>();
foreach (string error in pin1)
{
}

// number of inputs givenp
int n = 4;
float value;
float[,] y = new float[3601,3601];
float[] x = new float[3600];
for (int p = 0; p < 3600; p++)
{
x[p] = p/(float)10;
y[p, 0] = errorValues[p];
}

// y[][] is used for divided difference
// table where y[][0] is used for input
// y[0, 0] = 12;
// y[1, 0] = 13;
// y[2, 0] = 14;
// y[3, 0] = 16;
// float[] x = { 1, 2, 3, 4 };

dividedDiffTable(x, y, n);
int index = 0;
for (value = 0; value<360; value=value + (float)0.09)
{
//Console.WriteLine("\nValue at " + Math.Round(value,2) + " at " + (index) + " is " + Math.Round(applyFormula(value, x, y, n), 5));
Console.WriteLine(Math.Round(applyFormula(value, x, y, n), 5));
index++;
}
}
}```
Posted
Updated 22-Mar-23 15:18pm
v2
Graeme_Grant 21-Mar-23 0:17am
Please just don't dump code and expect us to debug and fix it for you.

What is wrong? Please click on "Improve question" and add more detailed information. IS there an error thrown? If so post that error with all information. Are the results not expected? What results are you getting versus what it should be? etc...

## Solution 1

We have no data to check against, no expectation of what the results should be - so we can't run your code either mentally or in a computer to find out what it does!

So it's going to be up to you ... compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```private int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

## Solution 2

If you are still having issues, try dropping this question into ChatGPT[^] and see if it helps:
`write a c# method to calculate newton's divided difference interpolation. The error data for circular motion will come from some 3rd party source at 0.1-degree division. I want to use a numerical technique to interpolate and get the points at 0.09-degree division.`

Warning: ChatGPT is not always accurate, so you may need to ask more questions or change the question asked. Also, give it some (not all) sample data for it to work with. You can ask it for results to see if the code it generates is accurate or not. But you will need to know what the correct data is to validate.

The other option is to use Google Search: newton's divided difference interpolation - Google Search[^], there is plenty of information out there, like this: Newton's Divided Difference Interpolation Formula - GeeksforGeeks[^] from the Google Search mentioned.

v2

## Solution 3

I can almost guarantee that you do not want to use Newtons divided difference interpolation. Often the issue is that interpolation using high degree polynomial fit, which Newton divided difference will do, will cause large error in relation to a natural fit. Runge's phenomenon - Wikipedia[^]

Usually its better to use spline, nurbs, b-splines etc instead.