CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

#### Hybrid View

1. Junior Member
Join Date
Feb 2014
Posts
2

## Fitting Data

I want do fit data with a sinus fit. I found an algorithm, unfortunately I do not see how to give the data to the algorithm.
Does anybody has an ide how to solve this?

Thanks for help..

float * datax = new float[numOfData]; float * datay = new float[numOfData];
float * dataz = new float[numOfData]; float phase(0); float mag(0);

for(int i=0;i // fill data

float a(0); float b(0); float c(0); float p[3] = {0}; // product of fitting equation float XiYi(0); float XiZi(0); float YiZi(0); float XiXi(0); float YiYi(0); float Xi(0); float Yi(0); float Zi(0); for(int i=0;i

float A[3][3];
float B[3][3];
float C[3][3];
float X[3][3];
int i;
int j;
float x = 0;
float n = 0; //n is the determinant of A

A[0][0] = XiXi;
A[0][1] = XiYi;
A[0][2] = Xi;
A[1][0] = XiYi;
A[1][1] = YiYi;
A[1][2] = Yi;
A[2][0] = Xi;
A[2][1] = Yi;
A[2][2] = numOfData;

for( i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
B[i][j] = 0;
C[i][j] = 0;
}
}

for( i=0, j=0;j<3;j++)
{
if(j == 2)
{
n += A[i][j] * A[i+1][0] * A[i+2][1];
}
else if(j == 1)
{
n += A[i][j] * A[i+1][j+1] * A[i+2][0];
}
else
{
n += A[i][j] * A[i+1][j+1] * A[i+2][j+2];
}
}

for( i=2, j=0;j<3;j++)
{
if(j == 2)
{
n -= A[i][j] * A[i-1][0] * A[i-2][1];
}
else if(j == 1)
{
n -= A[i][j] * A[i-1][j+1] * A[i-2][0];
}
else
{
n -= A[i][j]*A[i-1][j+1]*A[i-2][j+2];
}
}

// Check determinant n of matrix A
if (n)
{
x = 1.0/n;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
B[i][j] = A[j][i];
}
}

C[0][0] = (B[1][1] * B[2][2]) - (B[2][1] * B[1][2]);
C[0][1] = ((B[1][0] * B[2][2]) - (B[2][0] * B[1][2])) * (-1);
C[0][2] = (B[1][0] * B[2][1]) - (B[2][0] * B[1][1]);

C[1][0] = ((B[0][1] * B[2][2]) - (B[2][1] * B[0][2])) * (-1);
C[1][1] = (B[0][0] * B[2][2]) - (B[2][0] * B[0][2]);
C[1][2] = ((B[0][0] * B[2][1]) - (B[2][0] * B[0][1])) * (-1);

C[2][0] = (B[0][1] * B[1][2]) - (B[1][1] * B[0][2]);
C[2][1] = ((B[0][0] * B[1][2]) - (B[1][0] * B[0][2])) * (-1);
C[2][2] = (B[0][0] * B[1][1]) - (B[1][0] * B[0][1]);

for( i=0;i<3;i++)
{
for( j=0;j<3;j++)
{
X[i][j] = C[i][j] * x;
}
}

p[0] = XiZi;
p[1] = YiZi;
p[2] = Zi;

a = X[0][0] * p[0] + X[0][1] * p[1] + X[0][2] * p[2];
b = X[1][0] * p[0] + X[1][1] * p[1] + X[1][2] * p[2];
c = X[2][0] * p[0] + X[2][1] * p[1] + X[2][2] * p[2];
}
else // determinant=0
{
a = 1;
b = 1;
c = 0;
}
mag = sqrt(a*a + b*b);

2. ## Re: Fitting Data

1. Please, use Code tags when posting code snippets. Otherwise your code is absolutely unreadable! Have a look at the Announcements
2. Please, don't place more than one statement on a line! Write not like but like
Originally Posted by Mondain
Code:
```	float * datax = new float[numOfData]; float * datay = new float[numOfData];
float * dataz = new float[numOfData];  float phase(0); float mag(0);```
but the following way:
Code:
```	float * datax = new float[numOfData];
float * datay = new float[numOfData];
float * dataz = new float[numOfData];
float phase(0);
float mag(0);```
3. Please, don't omit very important parts in your code like you did here:
Originally Posted by Mondain
Code:
`	for(int i=0;i // fill data`

3. ## Re: Fitting Data

I found an algorithm, unfortunately I do not see how to give the data to the algorithm
The details are at http://mariotapilouw.blogspot.de/201...e-fitting.html If you don't understand this, then the issue is with the mathematics and not with c++.

You might get more help if you posted to the Algorithms forum http://forums.codeguru.com/forumdisp...ata-Structures

4. Junior Member
Join Date
Feb 2014
Posts
2

## Re: Fitting Data

There is no information how the data is filled. The code is incomplete.

Code:
`for(int i=0;i // fill data`
Does anybody see, which variables have to be filled?

PS: How can I edit previous posts?

5. ## Re: Fitting Data

Originally Posted by Mondain
PS: How can I edit previous posts?
You will be able to edit them after about five sent posts.

6. ## Re: Fitting Data

From the web site
1. Prepare the buffer for datax, datay (for cos(b), sin(b)), and dataz (for the input data) and variables for the result, phase and mag

2. Then fill the data with a sine or cosine function and the simulated input data:
The 'missing/incomplete' code is where you need to fill the variables with the required sine/cosine function values and the input data. This is mathematics related.

7. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: Fitting Data

you're doing matrix math, so it would make sense to make a matrix class (or at the very least separate out the elementary matrix primitives (adding, multiplying, ...) into sepaprate functions.

This makes it easier to see what's going on because your core 'fitting' routine won't be messed up by the details of the matrix operations.

Splitting a 'big' problem up into multiple smaller (reussable) bits is an important part of programming.
it's also easier to debug because once the basic functions have been thouroughly tested, and you get an error in the output, you can eliminate the well tested bits from the stuff you need to check.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•