Click here to Skip to main content
15,884,872 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
//C# for solving inverse interpolation
using System;
class GFG

{

// Consider a structure to keep

// each pair of x and y together

class Data

{

public double x, y;

public Data(double x, double y)

{

this.x = x;

this.y = y;

}

};

// Function to calculate the

// inverse interpolation

static double inv_interpolate(Data []d,

int n, double y)

{

// Initialize readonly x

double x = 0;

int i, j;

for (i = 0; i < n; i++)

{

// Calculate each term

// of the given formula

double xi = d[i].x;

for (j = 0; j < n; j++)

{

if (j != i)

{

xi = xi * (y - d[j].y) /

(d[i].y - d[j].y);

}

}

// Add term to readonly result

x += xi;

}

return x;

}

// Driver Code

public static void Main(string[] args)

{

// Sample dataset of 4 points

// Here we find the value

// of x when y = 4.5

Data []d = {new Data(1.27, 2.3),

new Data(2.25, 2.95),

new Data(2.5, 3.5),

new Data(3.6, 5.1)};

// Size of dataset

int n = 4;

// Sample y value

double y = 4.5;

// Using the Inverse Interpolation

// function to find the

// value of x when y = 4.5

Console.Write("Value of x at y = 4.5 : {0:f5}");
Console.Write(inv_interpolate(d, n, y));

}

}


What I have tried:

//C# for solving inverse interpolation
using System;
class GFG

{

// Consider a structure to keep

// each pair of x and y together

class Data

{

public double x, y;

public Data(double x, double y)

{

this.x = x;

this.y = y;

}

};

// Function to calculate the

// inverse interpolation

static double inv_interpolate(Data []d,

int n, double y)

{

// Initialize readonly x

double x = 0;

int i, j;

for (i = 0; i < n; i++)

{

// Calculate each term

// of the given formula

double xi = d[i].x;

for (j = 0; j < n; j++)

{

if (j != i)

{

xi = xi * (y - d[j].y) /

(d[i].y - d[j].y);

}

}

// Add term to readonly result

x += xi;

}

return x;

}

// Driver Code

public static void Main(string[] args)

{

// Sample dataset of 4 points

// Here we find the value

// of x when y = 4.5

Data []d = {new Data(1.27, 2.3),

new Data(2.25, 2.95),

new Data(2.5, 3.5),

new Data(3.6, 5.1)};

// Size of dataset

int n = 4;

// Sample y value

double y = 4.5;

// Using the Inverse Interpolation

// function to find the

// value of x when y = 4.5

Console.Write("Value of x at y = 4.5 : {0:f5}");
Console.Write(inv_interpolate(d, n, y));

}

}
Posted
Updated 26-Mar-23 14:38pm

You tried nothing.
A good starting point could be: Python Classes[^].
 
Share this answer
 
Comments
Maciej Los 3-Mar-22 7:57am    
5ed!
CPallini 3-Mar-22 8:35am    
Thank you, Maciej!
There is an online tool available, you can try yourself
Convert C# to Python - A free code conversion tool - developer Fusion[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900