Linear Interpolation - C

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 42798 Views - Last Post: 01 April 2014 - 08:17 PM Rate Topic: -----

#1 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Linear Interpolation - C

Posted 01 April 2014 - 03:29 PM

I'm a problem developing an algorithm in order to calculate the linear interpolation of a time series in a C program.
I was given the formula as follows: Posted Image but I'm not sure how to incorporate it into the program. I am also wondering if this formula would also allow me to calculate the values for distance when given a time not in the array. Say, I wanted to enter in the time "11.8", would the formula allow me to calculate the distance for that time?

#include <stdio.h>
#include <stdlib.h>
#define size 8

double getDistance(int time[], double distance[], double test);

int main(void)
{
	int time[size] = {0,3,5,8,10,12,15,18};
	double distance[size] = {2, 5.2, 6, 10, 10.4, 13.4, 14.8, 18};
	float test;
	char choice;

	do
	{
		printf("Please enter a time between 0 and 18: ");
		scanf("%f", &test);

		while(test < 0)
		{
			printf("The number you have entered is invalid, please enter another number. ");
			scanf("%f", &test);
		}

		while(test > 18)
		{
			printf("The number you have entered is invalid, please enter another number. ");
			scanf("%f", &test);
		}

		getDistance(time, distance, test); 

		printf("Do you want to enter another number? (Y/N) ");
		while(getchar() != '\n') continue;
		scanf("%c", &choice);

	}while(choice != 'N');

	system("pause");

	return 0;
}

double getDistance(int time[], int distance[], int test)
{

}



Is This A Good Question/Topic? 0
  • +

Replies To: Linear Interpolation - C

#2 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 03:38 PM

View PostAugust_26th, on 01 April 2014 - 03:29 PM, said:

I am also wondering if this formula would also allow me to calculate the values for distance when given a time not in the array.


First this is sort of a silly question, since you can program it to mostly anything.
But, I understand, but it would depend on the values of x and f(x) you are feeding it. If x is time, then yes you could solve the approximation of the distance.
Was This Post Helpful? 0
  • +
  • -

#3 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 04:31 PM

View Postinfernorthor, on 01 April 2014 - 03:38 PM, said:

First this is sort of a silly question, since you can program it to mostly anything.
But, I understand, but it would depend on the values of x and f(x) you are feeding it. If x is time, then yes you could solve the approximation of the distance.


I suppose that was a rather silly question, but I was just asking to do away with my doubts. Well the time and distance arrays have already been initialized. And I am rather certain that the time that the user is inputting will be x. I just don't know how to work in to the whole of the program.
Was This Post Helpful? 0
  • +
  • -

#4 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Linear Interpolation - C

Posted 01 April 2014 - 04:44 PM

Hi,

So what is the formula for f(x) ?
Was This Post Helpful? 0
  • +
  • -

#5 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 05:22 PM

View Posttarmizi_adam2005, on 01 April 2014 - 04:44 PM, said:

Hi,

So what is the formula for f(x) ?


I posted it up above, but it'd be Posted Image. f(x) would be the y's obviously.
Was This Post Helpful? 0
  • +
  • -

#6 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Linear Interpolation - C

Posted 01 April 2014 - 05:52 PM

Ah, ok.... I thought that f(x) was defined as some other function you forgot to mention. Well, My calculus knowledge are quite rusty but, i would start implementing the calculation simple. Say, if you were given the values for x, x1, x2, y2 and y1 how would you get the value for y on paper ? Then try implementing that in code first. Then you can modify it for time series using arrays and loops...
Was This Post Helpful? 0
  • +
  • -

#7 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 06:20 PM

View Posttarmizi_adam2005, on 01 April 2014 - 05:52 PM, said:

Ah, ok.... I thought that f(x) was defined as some other function you forgot to mention. Well, My calculus knowledge are quite rusty but, i would start implementing the calculation simple. Say, if you were given the values for x, x1, x2, y2 and y1 how would you get the value for y on paper ? Then try implementing that in code first. Then you can modify it for time series using arrays and loops...


I'm guessing I'd do something along these lines?
double f = (y1 - y2)/(x1 - x2)
double g = (x-x1)
double y = y1 + h * i


or would this not be what you're asking? If so, I suppose I understood that relatively simply. But it's the incorporation of the arrays that's boggling me.
Was This Post Helpful? 0
  • +
  • -

#8 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 06:27 PM

I don't know what you are doing with the f,g,y

but since you seem utterly confused

int time[size] = {x0,x1,x2,x3,...};
double distance[size] = {y0,y1,y2,y3,...};


Was This Post Helpful? 0
  • +
  • -

#9 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 06:36 PM

View Postinfernorthor, on 01 April 2014 - 06:27 PM, said:

I don't know what you are doing with the f,g,y

but since you seem utterly confused

int time[size] = {x0,x1,x2,x3,...};
double distance[size] = {y0,y1,y2,y3,...};



I decided to break it up to make it simpler on the eyes, but wouldn't the interpolation formula just be:
y = y0 + ((y1-y0)/(x1-x0))(x-x0)


Given that the user inputs a number, how would I determine where it would be on the time scale? For example, the user inputs an 11 which isn't in the defined array, but the times 10 and 12 are. Then I'd want to pull those values for time 10 and 12 and use them to calculate the value for time 11. I suppose that's where I'm mainly confused.
Was This Post Helpful? 0
  • +
  • -

#10 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 06:47 PM

for that
x0 = 10 , x1= 12 , and x=11
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Linear Interpolation - C

Posted 01 April 2014 - 06:50 PM

Yes.
Let (x0, y0) = (10, 10.4)
Let (x1, y1) = (12, 13.4)



Since you were given x, you can compute for y given your equation above.
Was This Post Helpful? 0
  • +
  • -

#12 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 07:00 PM

View PostSkydiver, on 01 April 2014 - 06:50 PM, said:

Yes.
Let (x0, y0) = (10, 10.4)
Let (x1, y1) = (12, 13.4)



Since you were given x, you can compute for y given your equation above.


I suppose I have my main function down so I don't need to touch that any longer. But would this work for the calculations function?

double getDistance(int time[], int distance[], float test)
{
	int k = arraycalc(time, test);
	double x0 = time[k], x1 = time[k++], y0 = distance[k], y1 = distance[k++];

	double y = y0 + ((y1 - y0) / (x1 - x0)) * (test - x0);

	return y;
}

double arraycalc(int time[], float test)
{
	int i;

	for(i = 0; i < size; i++)
	{
		if(test < time[i] && test > time[i++])
		{
			return i;
		}
	}
}


I tried plugging it in to my program and it built successfully but once it got down to calculating an answer, it output a "-1.#IND00".
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Linear Interpolation - C

Posted 01 April 2014 - 07:08 PM

Post your current complete code. We are not clairvoyant and can read what is on your monitor. Just basing things off the code you posted on post #1 and post #12, there is nothing there that prints out an answer. How are we supposed to know what you are printing out as the answer? Without seeing your code, you could be printing out an uninitialized variable.

Anyway, with the code you posted in post #12, look closely at your arraycalc() function. Not only are you double incrementing, you are also doing a buffer overrun past the end of your array. Additionally, if you don't find your value, the function will return some random value because you aren't returning a value for the "not found" case.
Was This Post Helpful? 0
  • +
  • -

#14 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 07:10 PM

careful with

test < time[i] && test > time[i++]
both time[i] time[i++] will be the same thing
i++ assigns number before adds
++i adds before assigns

size should also be passed variable
and make sure not call time[] out of bounds
Was This Post Helpful? 0
  • +
  • -

#15 August_26th   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 14

Re: Linear Interpolation - C

Posted 01 April 2014 - 07:12 PM

View PostSkydiver, on 01 April 2014 - 07:08 PM, said:

Post your current complete code. We are not clairvoyant and can read what is on your monitor. Just basing things off the code you posted on post #1 and post #12, there is nothing there that prints out an answer. How are we supposed to know what you are printing out as the answer? Without seeing your code, you could be printing out an uninitialized variable.

Anyway, with the code you posted in post #12, look closely at your arraycalc() function. Not only are you double incrementing, you are also doing a buffer overrun past the end of your array. Additionally, if you don't find your value, the function will return some random value because you aren't returning a value for the "not found" case.


Apologies. I updated my own code without updating the post here. You mentioned that there's obviously something wrong with arraycalc, so I guess I'll have to tinker with that. But beyond that, was there anything wrong with the getDistance function?
#include <stdio.h>
#include <stdlib.h>
#define size 8

double getDistance(int time[], double distance[], float test);
double arraycalc(int time[], float test);

int main(void)
{
	int time[size] = {0,3,5,8,10,12,15,18};
	double distance[size] = {2, 5.2, 6, 10, 10.4, 13.4, 14.8, 18};
	float test;
	char choice;

	do
	{
		printf("Please enter a time between 0 and 18: ");
		scanf("%f", &test);

		while(test < 0)
		{
			printf("The number you have entered is invalid, please enter another number. ");
			scanf("%f", &test);
		}

		while(test > 18)
		{
			printf("The number you have entered is invalid, please enter another number. ");
			scanf("%f", &test);
		}

		printf("The distance for the time given would be: %f\n", getDistance(time, distance, test)); 

		printf("Do you want to enter another number? (Y/N) ");
		while(getchar() != '\n') continue;
		scanf("%c", &choice);

	}while(choice != 'N');

	system("pause");

	return 0;
}

double getDistance(int time[], int distance[], float test)
{
	int k = arraycalc(time, test);
	double x0 = time[k], x1 = time[k++], y0 = distance[k], y1 = distance[k++];

	double y = y0 + ((y1 - y0) / (x1 - x0)) * (test - x0);

	return y;
}

double arraycalc(int time[], float test)
{
	int i;

	for(i = 0; i < size; i++)
	{
		if(test < time[i] && test > time[i++])
		{
			return i;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2