Newton and Lagrange interpolation algorithms

Source: Internet
Author: User
Tags count

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
typedef struct DATA
{
float x;
Float y;
Structure of}data;//variable x and function value y
Data d[20];//up to 20 sets
float f (int s,int t)//Newton interpolation to return to the plug-in
{
if (t==s+1)
Return (D[T].Y-D[S].Y)/(d[t].x-d[s].x);
Else
Return (f (s+1,t)-F (s,t-1))/(d[t].x-d[s].x);
}
Float Newton (float x,int count)
{
int n;
while (1)
{
cout<< "Please input n value (ie n time interpolation):"//Get interpolation times
cin>>n;
if (n<=count-1)//interpolation times must not be greater than count-1 times
Break
Else
System ("CLS");
}
Initialize T,Y,YT.
float t=1.0;
float y=d[0].y;
float yt=0.0;
Calculate Y-Values
for (int j=1;j<=n;j++)
{
t= (x-d[j-1].x) *t;
Yt=f (0,j) *t;
Cout<<f (0,J) <<endl;
Y=y+yt;
}
return y;
}
Float Lagrange (float x,int count)
{
float y=0.0;
for (int k=0;k<count;k++)//Here defaults to count-1 secondary interpolation
{
Float p=1.0;//Initialize P
for (int j=0;j<count;j++)
{//Calculate P's value
if (K==J) continue;//to determine whether the same number
p=p* (x-d[j].x)/(d[k].x-d[j].x);
}
y=y+p*d[k].y;//sum
}
Return y;//returns the value of Y
}
void Main ()
{
float X,y;
int count;
while (1)
{
cout<< "Please enter x[i],y[i] number of groups, no more than 20 groups:";//Require users to enter data group number
cin>>count;
if (count<=20)
break;//Check if the input is legal
System ("CLS");
}
Get each group of data
for (int i=0;i<count;i++)
{
cout<< "Please enter the value of" <<i+1<< "group x:";
cin>>d[i].x;
cout<< "Please enter the value of" <<i+1<< "group y:";
cin>>d[i].y;
System ("CLS");
}
cout<< "Please enter the value of x:";//Get the value of the variable x
cin>>x;
while (1)
{
int choice=3;
cout<< "Please choose which interpolation method to use to calculate:" <<endl;
cout<< "(0): Exit" <<endl;
cout<< "(1): Lagrange" <<endl;
cout<< "(2): Newton" <<endl;
cout<< "Enter your choice:";
cin>>choice;//gets the user's selection
if (choice==2)
{
cout<< "You chose the Newton interpolation calculation method, the result is:";
Y=newton (x,count); break;//call the corresponding handler function
}
if (choice==1)
{
cout<< "You chose the Lagrange interpolation calculation method, the result is:";
Y=lagrange (x,count); break;//call the corresponding handler function
}
if (choice==0)
Break
System ("CLS");
cout<< "INPUT error!!!!" <<endl;
}
cout<<x<< "," <<y<<endl;//output final result
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.