1 Replies - 819 Views - Last Post: 27 November 2015 - 08:24 AM Rate Topic: -----

#1 nct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-November 15

linera interpolation

Posted 27 November 2015 - 08:09 AM

i have a text file having two columns heading "z","p","v","w","av","aw" containing numeric values.So i want a program to store these values in two different
arrays on which i can operate.
here is my text file:
   z		  p	   v		   w		   av		  aw
-0.67001	6.54438	-0.01078	 0.00001	-0.00987	0.00000
-0.65043	6.35235	-0.01081	-0.00014	-0.00981	0.00329
-0.63086	6.16032	-0.01083	-0.00028	-0.00976	0.00657
-0.61129	5.96829	-0.01086	-0.00034	-0.00969	0.00967
-0.59172	5.77623	-0.01089	-0.00038	-0.00961	0.01154
-0.57215	5.58411	-0.01096	-0.00035	-0.00945	0.01071
-0.55258	5.39199	-0.01103	-0.00034	-0.00929	0.00992
-0.53301	5.19987	-0.01109	-0.00033	-0.00911	0.00915
-0.51344	5.00767	-0.01119	-0.00038	-0.00887	0.01036
-0.49387	4.81547	-0.01131	-0.00042	-0.00862	0.01172
-0.47431	4.62327	-0.01141	-0.00044	-0.00832	0.01302
-0.45473	4.431	-0.01154	-0.00047	-0.00799	0.01445
-0.43516	4.23871	-0.01169	-0.00051	-0.00762	0.01589
-0.41559	4.04644	-0.01183	-0.00051	-0.00721	0.01725
-0.39602	3.85407	-0.01201	-0.00053	-0.00675	0.01879
-0.37645	3.66171	-0.01221	-0.00053	-0.00625	0.02029


Next i want to give a Z value as input and want my program to check if the given value of is present in my "z" list or not...if yes then produce
the corresponding "v" and "w"value respectively.
If the given Z value is not in the "z" list then find the linear interpolation between the values nearest(nearest above and below)
the given z value from the "z" list,to produce u and w value.
Thanks in advance ...i need it urgently.



#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;
double interpolation (double x1, double x2, double y1, double y2, double xInt)
    {

        double yInt = y1 + (y2-y1)/(x2-x1)*(xInt-x1);

        return yInt;
    }
int main()
    {
        ifstream theFile("velocity.txt");
        double depth;
        double z;
        cin>>z;
        int n=0;
        double y[]={0};
        double v[]={0};
        double w[]={0};
        double ax[]={0};
        double ay[]={0};
        double velx;
        double vely;
        double pressure;
        double velocityx;
        double velocityy;
        double acclx;
        double accly;
        while (theFile>>depth>>pressure>>velocityx>>velocityy>>acclx>>accly)
        {
            cout<<depth<<"  "<<pressure<<"  "<<velocityx<<"  "<<velocityy<<"  "<<acclx<<"  "<<accly<<endl;
            y[n]=depth;
            v[n]=velocityx;
            w[n]=velocityy;
            ax[n]=acclx;
            ay[n]=accly;
            n=n+1;
            
            }
        cout<<n;
        for(int j=0;j<n;j++)
        {
        	cout<<y[j]<<"  "<<v[j]<<"  "<<w[j]<<"  "<<ax[j]<<"  "<<ay[j]<<endl;
		}
        for(int i=0;i<n;i++)
        {   int f=0;
            if(y[i]==z)
            {
                velx=v[i];
                vely=w[i];
            }
            else if(y[i]<z)
                    {
                      f=f+1;
                    }
            else
            {
                       velx=interpolation (y[f-1], y[f], v[f-1], v[f], z);
                       vely=interpolation (y[f-1], y[f], w[f-1], w[f], z);
                       break;
            }
    }
       cout<<velx<<endl<<vely<<endl;


    }


i dont know why it is not working.....pls help me guys ...i need it urgently

Attached File(s)


This post has been edited by Skydiver: 27 November 2015 - 08:21 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: linera interpolation

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: linera interpolation

Posted 27 November 2015 - 08:24 AM

"not working" is kind of broad. Can you be more descriptive about what is not working. Are you getting compiler errors/warnings? Are you getting crashes?

It's like going to the doctor and saying "I don't feel well." Do you expect her to just cure you with that bit of information?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1