Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建一个在C中计算线性插值的方法#_C#_Linear Interpolation - Fatal编程技术网

C# 创建一个在C中计算线性插值的方法#

C# 创建一个在C中计算线性插值的方法#,c#,linear-interpolation,C#,Linear Interpolation,我的任务是创建一种计算线性插值的方法,其中Y是日期时间值,X是整数值。例如,查看以下值,如何找到7、8和9的值 Date: Value: 05/01/2013 5 06/01/2013 7 10/01/2013 9 11/01/2013 1 15/01/2013 7 17/01/2013 2 02/02/2013 8 EDIT: int interpMethod(DateTime x0, int y0, DateTime x1, int y1, int x) {

我的任务是创建一种计算线性插值的方法,其中Y是日期时间值,X是整数值。例如,查看以下值,如何找到7、8和9的值

Date:       Value:
05/01/2013  5
06/01/2013  7
10/01/2013  9
11/01/2013  1
15/01/2013  7
17/01/2013  2
02/02/2013  8

EDIT: 
int interpMethod(DateTime x0, int y0, DateTime x1, int y1, int x)
{
    return y0 * (x - x1) / (x0 - x1) + y1 * (x - x0) / (x1 - x0);
}

为了在其他两个点之间插值点,需要计算变化率,然后将其应用于两个点之间的距离

此外,确保数据类型保持一致。显示的数据具有双精度,但您的方法仅处理整数。另外,您在问题中要求输入一个double并查找DateTime,但随后返回的是一个整数

public static DateTime Interpolate(DateTime x0, double y0, DateTime x1, double y1, double target)
{
  //this will be your seconds per y
  double rate = (x1 - x0).TotalSeconds / (y1 - y0);
  //next you have to compute the distance between one of your points and the target point on the known axis
  double yDistance = target - y0;
  //and then return the datetime that goes along with that point
  return x0.AddSeconds(rate * yDistance);
}

为了在其他两个点之间插值点,需要计算变化率,然后将其应用于两个点之间的距离

此外,确保数据类型保持一致。显示的数据具有双精度,但您的方法仅处理整数。另外,您在问题中要求输入一个double并查找DateTime,但随后返回的是一个整数

public static DateTime Interpolate(DateTime x0, double y0, DateTime x1, double y1, double target)
{
  //this will be your seconds per y
  double rate = (x1 - x0).TotalSeconds / (y1 - y0);
  //next you have to compute the distance between one of your points and the target point on the known axis
  double yDistance = target - y0;
  //and then return the datetime that goes along with that point
  return x0.AddSeconds(rate * yDistance);
}

你自己努力解决这个问题了吗?-2但是没有人能提供任何有用的答案你自己努力解决这个问题了吗?-2但是没有人能提供任何有用的答案谢谢你的有用的答案。这是一个错误;我刚刚添加了一个函数,因为您要求我提供一些代码。不管怎样,如果我不得不用它来迭代一个日期列表,该怎么办;如何以编程方式输入这两个日期?等等,你想知道给定值的日期,还是给定日期的值?不,我只显示了3个日期,但你能建议我如何循环浏览日期列表吗?是的,但你需要提前知道其中一列或另一列的值,这才有意义。哪一列是已知列?你是从5个日期和3个值开始的吗?或者5个值和3个日期?3个只是一个例子,请看我的更新。假设我有两列,但大约有100行。我可以简单地做3行,但是如果行数很多,比如100行,那么算法会是什么呢?谢谢你的回答。这是一个错误;我刚刚添加了一个函数,因为您要求我提供一些代码。不管怎样,如果我不得不用它来迭代一个日期列表,该怎么办;如何以编程方式输入这两个日期?等等,你想知道给定值的日期,还是给定日期的值?不,我只显示了3个日期,但你能建议我如何循环浏览日期列表吗?是的,但你需要提前知道其中一列或另一列的值,这才有意义。哪一列是已知列?你是从5个日期和3个值开始的吗?或者5个值和3个日期?3个只是一个例子,请看我的更新。假设我有两列,但大约有100行。我可以简单地做3行,但是如果有很多行,比如100行,算法会是什么呢?