# Lagrange Function C + +

Source: Internet
Author: User

```

`#include <iostream>`

`using namespace std;`

`//拉格朗日插值求解方程`

`double * xs; //all input x`

`double * ys; //all input y`

`int n; //size`

`//1.1275 1.1503 1.1735 1.1972`

`//0.1191 0.13954 0.15932 0.17903`

`//1.130`

`void init()`

`{`

` cout << "please input n " << endl;`

` cin >> n;`

` xs = new double[n];`

` ys = new double[n];`

`//input x`

` cout << "please input the x‘s value !" << endl;`

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

` {`

` cin >> xs[i];`

` }`

`//input y`

` cout << "please input the y‘s value !" << endl;`

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

` {`

` cin >> ys[i];`

` }`

`}`

`double everyItem(int num, double x)`

`{`

` double y = ys[num];`

` double up = 1;`

` double down = 1;`

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

` {`

` if(i != num)`

` {`

` up *= (x - xs[i]);`

` down *= (xs[num] - xs[i]);`

` }`

` }`

` return y*up/down;`

`}`

`double lagrange(double x)`

`{`

` double total = 0;`

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

` {`

` total = total + everyItem(i , x);`

` }`

` return total;`

`}`

`int main()`

`{`

` init();`

` double input;`

` cout << "please input you x !" <<endl;`

` cin >> input;`

` double result = lagrange(input);`

` cout << "the result is " << result << endl;`

` return 0;`

`}`

```

From for notes (Wiz)

List of attachments

Lagrange Function C + +

Related Keywords:

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.