How can I calculate index of error between two curves?

How can I calculate the index of error between two curves?
I want to make a program to calculate index of error between two Curves (one measured and the other calculed) ym=f(xm) et yc=f(xc). This index is known as: gamma=minimum{((xm^2-xc^2)^2/4)+(ym^2-yc^2)^2/4)}. I have data with 4 columns xm,ym,xc and yc("file.dat")with 163 lignes. Can someone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include"fstream"
#include <math.h>
using namespace std;
int main()  
{double xm1,ym1,xc1,yc1,xm2,ym2,xc2,yc2,min[163],a,xc,yc; 
   ifstream input1("file.dat");
   ifstream input2("file.dat"); 
   ofstream output("error.dat"); 
for(int i=0;i<163;i++)
   {input1>>xm1>>ym1>>xc1>>yc1;
     min[i]=pow(((pow(ym1-yc1,2)/4)+(pow(xm1-xc1,2)/4)),0.5);
     while (!input2.eof()){ input2>>xm2>>ym2>>xc2>>yc2; 
		           a=pow(((pow(ym1-yc2,2)/4)+(pow(xm1-xc2,2)/4)),0.5);
			   if (a<min[i]) min[i]=a,xc=xc2,yc=yc2;
                          }
     output<<xm1<<" "<<ym1<<" "<<xc<<" "<<yc<<" "<<min[i]<<endl;        
     input2.close();     		 
    }            					       
}
Use the formula? :

((MeasuredValue - CalculatedValue)/MeasuredValue)*100


You already seem to have the values calculated.
Can you explain more please. Where should I use this formula?
Last edited on
looks like you read the file variables, compute the equation you gave, and take the smallest result from those computations.

you appear to be opening the same file twice in this code. do you have 2 data files?

while !eof does not work like you want it to. do while file>> variables instead, or as you did the first time, read your know number of lines is OK here.

1/2 power is sqrt()

you only need 1 min, not 163 of them. you can do it that way, but you don't need it. just set min = first result, and after that, if new result < min update min.

I think you are close, without going over in much more detail; I suspect the filename is your problem if its not working right..

integer powers with pow are sluggish (by a full order of magnitude on my machine). If you had to do a lot of them (billions), you would want to use x*x instead of pow(x,2).

Last edited on
Thanks for your feedback,
I have only one file. I have used two loops because I want to calculate this index for each point (xm, ym), the first loop is to fix the point (xm, ym) and the second loop to change xc and yc until the minimum gamma value is reached for this measured point , The index will be recalculated for each measured point because of the first loop (minimum for each measured point=>163 min).
Last edited on
Are you trying to do a kind of double loop through the same file? If so, it's probably better to read it into a vector and loop through that.

Also, it looks like your formula is coded wrong. You need to square the variables before you subtract them (and after, as you are doing). You should make the calculation a function.
Can someone help me?

If you could describe your problem more precisely it would be easier.
As I understand your program, you first calculate the half distance between measured points and corresponding calculatet points. Then, in an inner loop you scan through the complete file again and look for an other calculated point that is closer to the current measured point, right? Alas, for the second measured poin you did not "rewind" the reading position within the file, and results for point 2 to n are wrong. Right?

I suggest -- 163 rows with four colums are close to nothing -- read the file to an array or vector or what offers access to single values by index and do your computing with it. "Rewinding" will then be just to restart the inner loop from zero.

I apologize to everyone. I did not explain the problem well because of my weakness in English.

for dutch: the formula in the script is correct I've been wrong in the problem description. This formula is called "gamma index"if you've heard anything around it before.

for MikeStgt: yes, you are right. As for your suggestion, I have problems with arrays because I have not worked on c++ for a long time. Due to lack of time I do not have time to review.

I would appreciate any help provided to resolve this problem
As I said I do not remember how to work with arrays. I have tried to write this code now, how it looks to you??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include"fstream"
#include <math.h>
using namespace std;
int main()  
{double tab[162][3],min[162],a[162],b[162],c[162];
//fill arrays
   ifstream input1("file.dat");
for(int i=0;i<163;i++)  {for(int j=0;i<4;i++){input1>>tab[i][j];}}
//min calcul
    for(int k=0;k<163;k++)
           { min[k]=sqrt(((pow(tab[k][1]-tab[k][3],2)/4)+(pow(tab[k][0]-tab[k][2],2)/4)));//initialized
                 for(int m=0;m<163;m++)
                                       {  a[m]=sqrt(((pow(tab[k][1]-tab[m][3],2)/4)+(pow(tab[k][0]-tab[m][2],2)/4)));
                                          if (a[m]<min[k]) min[k]=a[m],b[k]=tab[m][2],c[k]=tab[m][3];
                                       }
           }
 //output      
   ofstream output("error.dat"); 
    for(int n=0;n<163;n++) output<<tab[n][0]<<" "<<tab[n][1]<<" "<<tab[n][2]<<" "<<tab[n][3]<<" "<<min[n]<<endl;                    					       
}
Last edited on
lack of time

The week has seven days, and a day has 24 to 25 hrs. That is the time you have.
Could you give us what's written in file.dat ? If the file contains a duplicate of the numbers then you don't need to reset the position, but it all depends on the file.

Also, use code tags. You can edit your post, highlight your code, and then press on the <> on the side.

What's the output of your code? Is it what you want? And use the formula I showed you. Put it where you need it, probably at the very end.
I tried to make your code more readable while adding the least extra syntax. The structs allow better naming.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

const int Size = 163;

struct Point { double x, y; };
struct Data  { Point  m, c; };

double calc(Point& m, Point& c) {
    return sqrt( (m.y - c.y) * (m.y - c.y) / 4
               + (m.x - c.x) * (m.x - c.x) / 4);
}

int main() {
    Data tab[Size];

    ifstream in("file.dat");
    for (int i = 0; i < Size; ++i)
        in >> tab[i].m.x >> tab[i].m.y
           >> tab[i].c.x >> tab[i].c.y;

    ofstream out("error.dat"); 
    for (int i = 0; i < Size; ++i) {
        Point c = tab[i].c;
        double min = calc(tab[i].m, tab[i].c);
        for (int j = 0; j < Size; ++j) {
            double a = calc(tab[i].m, tab[j].c);
            if (a < min) {
                min = a;
                c = tab[j].c;
            }
        }
        out << tab[i].m.x << " "
            << tab[i].m.y << " "
            << c.x        << " "
            << c.y        << " "
            << min        << endl;
    }
}

Last edited on
This part of the file.data with the script I sent earlier results are not good. But when I tried the code sent by dutch, the results looked good
Thanks to all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-0.1 0.009482 -0.0995097 0.161745
-0.0975 0.00923542 -0.096981 0.170202
-0.095 0.0110617 -0.0944523 0.179338
-0.0925 0.01057 -0.0919236 0.189228
-0.09 0.0114892 -0.0893948 0.199956
-0.0875 0.0117732 -0.0868661 0.211621
-0.085 0.0129619 -0.0843374 0.224333
-0.0825 0.0140061 -0.0818087 0.238222
-0.08 0.014044 -0.0792799 0.253437
-0.0775 0.0159117 -0.0767512 0.270153
-0.075 0.0175585 -0.0742225 0.28857
-0.0725 0.0163601 -0.0716938 0.308928
-0.07 0.0175872 -0.0691651 0.331506
-0.0675 0.0182034 -0.0666363 0.356637
-0.065 0.0209153 -0.0641076 0.384718
-0.0625 0.0222173 -0.0615789 0.416228
-0.06 0.0236476 -0.0590502 0.451741
-0.0575 0.0278926 -0.0565214 0.491959
-0.055 0.0293662 -0.0539927 0.537744
-0.0525 0.0305306 -0.051464 0.590162
-0.05 0.0338266 -0.0489353 0.650543
-0.0475 0.0358168 -0.0464065 0.720564
-0.045 0.0427173 -0.0438778 0.802362
-0.0425 0.0464377 -0.0413491 0.898691
-0.04 0.0541169 -0.0388204 1.01315
-0.0375 0.057463 -0.0362917 1.15049
-0.035 0.0678275 -0.0337629 1.31713
-0.0325 0.0756967 -0.0312342 1.52183
-0.03 0.0863331 -0.0287055 1.77685
-0.0275 0.100277 -0.0261768 2.09966
-0.025 0.118287 -0.023648 2.51591
-0.0225 0.13975 -0.0211193 3.06438
-0.02 0.168703 -0.0185906 3.80602
-0.0175 0.212913 -0.0160619 4.84125
-0.015 0.268167 -0.0135332 6.34637
-0.0125 0.354543 -0.0110044 8.65905
-0.01 0.497201 -0.0084757 12.5138
-0.0075 0.76183 -0.005947 19.8569
-0.005 1.28033 -0.0034183 37.8462
-0.0025 99.8686 -0.0008895 100
0 96.8627 0.0016392 56.4928
0.0025 94.7472 0.0041679 25.6482
0.005 100 0.0066966 15.1498
0.0075 1.27382 0.0092253 10.1185
0.01 0.758472 0.0117541 7.249
0.0125 0.492924 0.0142828 5.44034
0.015 0.354822 0.0168115 4.22385
0.0175 0.271275 0.0193402 3.3669
0.02 0.20854 0.021869 2.74152
0.0225 0.171942 0.0243977 2.27207
Topic archived. No new replies allowed.