Advertisement
TheMagnusRex

6.3

Jan 15th, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.     int n; //
  7.     double xp; //
  8.     double a,b;
  9.     int i;
  10.     cout<<"n=";
  11.     cin>>n;
  12.     double x[n];
  13.     double y[n];
  14.     for (int l=0;l<n;l++){
  15.         cout<<"x_"<<l<<" || y_"<<l<<endl;
  16.         cin>>x[l]>>y[l];
  17.     }
  18.     cout<<"xp=";
  19.     cin>>xp;
  20.     if(xp<x[0]){
  21.         cout<<"|backward extrapolation|"<<endl;
  22.         i=1;
  23.     }
  24.     else if(xp>x[n-1]){
  25.         cout<<"|forward extrapoaltion|"<<endl;
  26.         i=n-1;
  27.     }
  28.     else{
  29.         int j = 0;
  30.         while (j<n-1){
  31.             j+=1;
  32.             if(xp>=x[j-1]&&xp<=x[j]){
  33.                 i=j;
  34.                 j=n-1;
  35.             }
  36.         }
  37.     }
  38.     a = (y[i]-y[i-1])/(x[i]-x[i-1]);
  39.     b = y[i-1] - a*x[i-1];
  40.     double yp = (a*xp + b);
  41.     cout<<"f(xp)="<<yp<<endl;
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement