Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main(){
- int n; //
- double xp; //
- double a,b;
- int i;
- cout<<"n=";
- cin>>n;
- double x[n];
- double y[n];
- for (int l=0;l<n;l++){
- cout<<"x_"<<l<<" || y_"<<l<<endl;
- cin>>x[l]>>y[l];
- }
- cout<<"xp=";
- cin>>xp;
- if(xp<x[0]){
- cout<<"|backward extrapolation|"<<endl;
- i=1;
- }
- else if(xp>x[n-1]){
- cout<<"|forward extrapoaltion|"<<endl;
- i=n-1;
- }
- else{
- int j = 0;
- while (j<n-1){
- j+=1;
- if(xp>=x[j-1]&&xp<=x[j]){
- i=j;
- j=n-1;
- }
- }
- }
- a = (y[i]-y[i-1])/(x[i]-x[i-1]);
- b = y[i-1] - a*x[i-1];
- double yp = (a*xp + b);
- cout<<"f(xp)="<<yp<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement