Reading float type from a file and using them in formulas

Pages: 12
I am trying to read numbers in two columns that are separated by a space from a .txt file and use them in formulas within my code and output the results into another .txt file so I can use them easier. My problem is reading the 3000 lines of numbers and assigning them the variable to use in the formulas. The first column I need to the variable dt and the second column I would like it to be the variable i. For whatever reason, I can't get them to read correctly. Here is my code.

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
51
52
53
  #include <stdlib.h>
#include <stdio.h>

void GetAcc(const float Acc[], const float Vel[], float Pos[], float dt);
void GetVel(const float Acc[], float Vel[], float dt);

int main()

{
    float Position[3000]={0};
    float Velocity[3000]={0};
    float Acceleration[3000]={0};
    float i;
    float dt;

    FILE *fp;

    fp=fopen("M3 Array File.txt", "r+");
     fscanf(fp,"%f",&dt);
     fscanf(fp,"%f",&i);


    GetVel(Acceleration,Velocity,dt);
    GetAcc(Acceleration,Velocity,Position,dt);

    for(i=0;i<3000;i++)  fprintf(fp,"%3.3f\t%3.3f\t%3.3f\t%3.3f\n",dt*(float)i,Acceleration[i],Velocity[i],Position[i]);
    fclose(fp);

    return  0;
}

void GetVel(const float Acc[], float Vel[], float dt)
{
    float i;

    for(i=1;i<3000;i++)
    {
        Vel[i]=Vel[i-1]+Acc[i]*dt;
        return Vel;
    }
}

void GetAcc(const float Acc[], const float Vel[], float Pos[], float dt)
{
    float i;

    for(i=1;i<3000;i++)
    {
        Acc[i]=2*Pos[i]-Pos[i-1]-Vel[i-1];
        return Acc;
    }
}

There's a lot of problems with the code. And your description is pretty vague, too.
Can you describe exactly what you are trying to calculate?
And can you show the first 10 lines of your input file?
Last edited on
Here are the first 10 lines of my input file. What I'm trying to do is take the first column and assign it the variable "dt". I'm then trying to take the second column and assign it to another variable. I then need to use these variables in the formulas GetVel and GetAcc. I am then trying to output what would then be 4 columns of information into a .txt file. I hope this makes sense. I am completely lost on how to do this.

0.000 0.000
0.001 0.000
0.002 0.000
0.003 0.000
0.004 0.000
0.005 0.000
0.006 0.000
0.007 0.000
0.008 0.000
0.009 0.000

But what exactly are you trying to calculate?
'dt', the first column, indicates a time step.
What does the second column represent? Is it always zeroes?
I have two different formulas that I'm trying to use: Vel[i]=Vel[i-1]+Acc[i]*dt and Acc[i]=2*Pos[i]-Pos[i-1]-Vel[i-1]. The second column is not always zeros there are just 3000 lines of text from the input file. Basically I'm trying to take the time and position data and derive the velocity and acceleration from that information and output all of that data into either a different file or the same file.
So the second column represents position?
And the first column never decreases?
Can you upload the input file somewhere and post a link here?

The simplest might be pastebin.com.
Just paste the text into the New Paste box.
Then click the Create New Paste button near the bottom.
Then copy the address in your browser's address bar and paste it here.
Last edited on
You would be correct that the first column never decreases and the second column represents position data.

Here is the raw data
https://pastebin.com/dquQrMR1
Acc[i]=2*Pos[i]-Pos[i-1]-Vel[i-1]

No way!
Now we've got the man for the job!
Why no way? I'm just curious as to what I did wrong because I've been working this issue for a little over a week and am still no closer to an answer.
Dimensional analysis. The units don't add up. Assuming meters and seconds, you can't meaningfully subtract meters per second from meters. And you certainly wouldn't end up with meters per second per second.
Dimensional analysis

Absolutely! "Dimensional homogeneity" is required to add or subtract physical quantities.
But don't confuse "units" (like metres, seconds) with "dimensions" (like length, time). Units are means of giving distinct numerical values to a quantity (e.g. 1000 mm = 1 m), but a dimension is an intrinsic property (e.g. length).


I've been working this issue for a little over a week

That's worrying. Check your equations with your teacher, or you may end up wasting your time.


The first column is more likely to be "time" than "timestep" - I suspect the timestep dt is 0.001 throughout.
0.000 0.000
0.001 0.000
0.002 0.000
0.003 0.000
0.004 0.000
0.005 0.000

The times always increase by 0.001 per line.
So the dt (or Δt) is always 0.001.
And the positions are non-decreasing.
Here's the first interesting sequence (where position changes from 0.000):

0.029	0.000
0.030	0.002
0.031	0.003
0.032	0.005
0.033	0.005

Assuming meters and seconds (just to give simple names to things):
From time step 0.029s to 0.030s, the position changes from 0.000m to 0.002m.
So the average velocity was 0.002m per 0.001s, or 2 m/s.
So the velocity increased from 0 m/s to 4 m/s (for an average of 2 m/s).
Therefore the acceleration was 4000 m/s/s (since it occurred in 0.001s).

Is that right? I only know the basic physics equations, as shown here:
http://hyperphysics.phy-astr.gsu.edu/hbase/mot.html
Last edited on
I suspect the second column has been rounded to 3 decimal places ... which will cause havoc with derivatives like velocity because the relative error is so large when the quantity is small.
That is correct. The equations that I was given are as follows:
v ( k ) = v ( k − 1 ) + a ( k − 1 ) Δ t

x ( k ) = x ( k − 1 ) + v ( k − 1 ) Δ t + 1 2 a ( k − 1 ) Δ t ^2
Is it possible that the second column is acceleration?
not according to my instructor
But your formulas are for calculating velocity (v) and position (x).
Also, the order of your output statement is t, a, v, x.
The first two are t, a, which (weakly) implies that those are your inputs.
Can you double-check with your instructor? Or the printed instructions?

If the second column is acceleration, then it could be as simple as:

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
#include <stdio.h>
#include <stdlib.h>

#define SIZE 3000

int main()
{
    FILE *fin = fopen("M3 Array File.txt", "r");
    if (!fin)
    {
        perror("fopen: input");
        exit(EXIT_FAILURE);
    }

    FILE *fout = fopen("M3 Array File Out.txt", "w");
    if (!fout)
    {
        fclose(fin);
        perror("fopen: output");
        exit(EXIT_FAILURE);
    }

    double prev_t = 0, prev_a = 0, prev_v = 0, prev_x = 0;

    // Initialize with the first line.
    fscanf(fin, "%lf %lf", &prev_t, &prev_a);

    for (int i = 1; i < SIZE; ++i)
    {
        double t, a;
        fscanf(fin, "%lf %lf", &t, &a);

        double dt = t - prev_t;
        double v = prev_v + prev_a * dt;
        double x = prev_x + prev_v * dt + prev_a * dt * dt / 2;

        fprintf(fout, "%8.3f %8.3f %14.9f %14.9f\n", t, a, v, x);

        prev_t = t;
        prev_a = a;
        prev_v = v;
        prev_x = x;
    }

    fclose(fin);
    fclose(fout);

    return 0;
}

Last edited on
I rewrote the code but it's not returning the correct information. Is there something I'm missing?

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
#include <stdlib.h>
#include <stdio.h>


int main()
{
    static double time[3000];
    static double posit[3000];
    static double vel[3000];
    static double acc[3000];

    float i;
    float dt;
    int x;

    FILE *fp;

    fp=fopen("M3 Array File.txt", "r");
    for(x=0;x<=3000;x++)
    {
        fscanf(fp,"%f",&dt);
        time[x]=dt;
        fscanf(fp,"%f",&i);
        posit[x]=i;
    }
    fclose(fp);
    for(x=0;x<100;x++)
    {
        vel[x]=(posit[x]-posit[x-1])/(time[x]-time[x-1]);
        acc[x]=(2*(posit[x]-posit[x-1]-vel[x-1]));
    }


        fp=fopen("results.txt","w");
        for (int y=0;y<=3000;y++)
            {
    fprintf(fp,"%.4f\t%.4f\t%.4f\t%.4f\n", time[x],posit[x],vel[x],acc[x]);
        }
        fclose(fp);
    return 0;
}
Last edited on
Did you see my last post?
Pages: 12