sumitmehta
New Member
- Messages
- 215
- Reaction score
- 1
- Points
- 0
I hope this program is useful for you people.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LagrangesInterpolation
{
public partial class Form1 : Form
{
private float[] Xs;
private float[] Ys;
public Form1()
{
InitializeComponent();
Xs = new float[4];
Ys = new float[4];
Xs[0] = 5;
Xs[1] = 6;
Xs[2] = 9;
Xs[3] = 11;
Ys[0] = 12;
Ys[1] = 13;
Ys[2] = 14;
Ys[3] = 16;
}
private void button1_Click(object sender, EventArgs e)
{
float l = Lagranges(10);
MessageBox.Show(l.ToString());
}
private float Lagranges(float point) {
int omit = 0;
float lag = 0;
float numerator = 1;
float denominator = 1;
for (int i = 0; i < Xs.Length; i++)
{
for (int j = 0; j < Xs.Length; j++)
{
if (j != omit)
{
numerator *= (point - Xs[j]);
denominator *= (Xs[omit] - Xs[j]);
}
}
lag += (float) (numerator / denominator) * Ys[omit];
numerator = 1;
denominator = 1;
omit++;
}
return lag;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LagrangesInterpolation
{
public partial class Form1 : Form
{
private float[] Xs;
private float[] Ys;
public Form1()
{
InitializeComponent();
Xs = new float[4];
Ys = new float[4];
Xs[0] = 5;
Xs[1] = 6;
Xs[2] = 9;
Xs[3] = 11;
Ys[0] = 12;
Ys[1] = 13;
Ys[2] = 14;
Ys[3] = 16;
}
private void button1_Click(object sender, EventArgs e)
{
float l = Lagranges(10);
MessageBox.Show(l.ToString());
}
private float Lagranges(float point) {
int omit = 0;
float lag = 0;
float numerator = 1;
float denominator = 1;
for (int i = 0; i < Xs.Length; i++)
{
for (int j = 0; j < Xs.Length; j++)
{
if (j != omit)
{
numerator *= (point - Xs[j]);
denominator *= (Xs[omit] - Xs[j]);
}
}
lag += (float) (numerator / denominator) * Ys[omit];
numerator = 1;
denominator = 1;
omit++;
}
return lag;
}
}
}