Why won't this Calculation Work?

Hi,
I'm fairly new to programming, and this is my first piece of code beyond drawing pretty patterns in dxf files!
I'm having trouble with line 132 (and probably 134 too). Basically where it calculates F[n][m] the calculation is not executing correctly. e.g. F[1][1]=C[1][1]*K[1], as C[1][1]=0.000169 and K[1]=0.0121 I expect to get F[1][1] in the region of 2e-6 but instead Xcode outputs the result as 1.176e-313!
Can anybody see the problem?
Thanks,
Beth

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//  Created by Elizabeth Hill on 29/02/2012.
//  Copyright (c) 2012 Elizabeth Hill. All rights reserved.
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <fstream>
#include <string>
#define Q 0.0027
#define t 0.00025
#define T 60
#define lmax 0.02
#define g 9.81
using namespace std;
int Wave(double TipPen)
{
    double NewTipPen;

    // SOIL PROFILE

    int d=2;
    double Xud[d];
    double Rsud[d];
    double Rpud[d];

    Xud[1]=0;     // Depth
    Rsud[1]=120000;    // Skin Friction
    Rpud[1]=300000;    // End Bearing
    Xud[2]=3;     // Depth
    Rsud[2]=120000;    // Skin Friction
    Rpud[2]=300000;    // End Bearing

    // DAMPENING

    double J1;  // Toe
    double J2;  // Skin
    J2=0.15;
    J1=0.05;

    // DEFINE PILE SECTIONS

    int m; // Unit
    int p; // Total number of units in Analysis

    p=7;

    double L[p];
    double A[p];
    double P[p];
    double I[p];
    double E[p];
    double Rho[p];
    double W[p];
    double K[p];

    for (m=3; m<=p; m++) {
        L[m]=0.02;
        A[m]=0.01;
        P[m]=0.4;
        I[m]=0.000008333;
        E[m]=200000000000;
        Rho[m]=7750;
        W[m]=Rho[m]*A[m]*L[m]*g;
        K[m]=E[m]*A[m]/L[m];
    }

    // HAMMER PROPERTIES

    double HammerMass;
    double h;
    double eff;
    double eta;

    h=2;
    HammerMass=17.5;
    W[1]=HammerMass*g;
    eff=0.9;
    eta=eff/100;

    L[1]=0.1;
    A[1]=0.01;
    I[1]=0.000008333;
    E[1]=200000000000;

    // CAPBLOCK PROPERTIES

    double CapblockMass; 
    CapblockMass=0.2;

    W[2]=CapblockMass*g;
    L[2]=0.1;
    A[2]=0.01;
    I[2]=0.000008333;
    E[2]=13000000000;
    K[2]=(L[2]/(E[2]*A[2]))+(L[3]/(E[3]*A[3])); // Kirchhoff's law

    // COEFFICIENTS OF RESTITUTION

    double e1;
    double e2;

    e1=0.5;
    e2=0.5;

    int n;
    double X[T][p];
    double D[T][p];
    double Dsoil[T][p];
    double Ksoil[T][p];
    double C[T][p];
    double F[T][p];
    double Ru[T][p];
    double R[T][p];
    double V[T][p];

    // Accelerate the ram

    V[0][1]=eta*sqrt(2*h*g);

    for (n=1; n<=T; n++) {

        for (m=1; m<=p; m++) {

            // DEFLECTIONS - STEP I

            D[n][m]=D[n-1][m]+(V[n-1][m]*12*t);

            // Define Depth Below Mudline

            X[n][m]=D[n][m]+TipPen; 

            // Define ULTIMATE Tip Soil Resistance

            if (m==p) {

                    if (X[n][m]<Xud[2] and X[n][m]>=Xud[1]) {  

                        Ru[n][m]=((((Rpud[2]-Rpud[1])/(Xud[2]-Xud[1]))*(X[n][m]+Xud[1]))+Rpud[1])*A[m];   

                    }

            } else {

                // Define ULTIMATE Skin Resistance

                    if (X[n][m]<Xud[2] and X[n][m]>=Xud[1]) {

                        Ru[n][m]=((((Rsud[2]-Rsud[1])/(Xud[2]-Xud[1]))*(X[n][m]+Xud[1]))+Rsud[1])*L[m]*P[m];   

                    }
                }
            }

            // DEFINE SOIL DEFLECTIONS - STEP II

            if (D[n][m]>Q) {

                if (D[n][m]>D[n-1][m]) {

                    Dsoil[n][m]=D[n][m]-Q; 

                } else {

                    Dsoil[n][m]=Dsoil[n-1][m];
                    NewTipPen=Dsoil[n][m];
                    return NewTipPen;

                }

            } else {

                Dsoil[n][m]=0;   

            }

        // SOIL DEFLECTIONS - STEP III

        R[n][p]=(D[n][p]-Dsoil[n][p])*Ksoil[n][p]*(1+(J1*V[n-1][p]));

        // COMPRESSIONS - STEP IV

        for (m=1; m<p; m++) {

            C[n][m]=D[n][m]-D[n][m+1];   

        }

        // FORCE IN RAM - STEP V    

        F[n][1]=C[n][1]*K[1]; // Calculation Not Executing Properly

        // FORCE IN CAPBLOCK - STEP VI

        F[n][2]=C[n][2]*K[2];

        for (m=3; m<=p; m++) {

            // FORCES IN PILE - STEP VII

            F[n][m]=C[n][m]*K[m];

            // SKIN RESISTANCE ON PILE - STEP VIII

            R[n][m]=(D[n][m]-Dsoil[n][m])*Ksoil[n][m]*(1+(J2*V[n-1][m]));   

        }

        // VELOCITIES - STEP IX

        for (m=1; m<=p; m++) {

            V[n][m]=V[n-1][m]+((F[n][m-1]-F[n][m]-R[n][m])*(t*g/W[m]));       

        }
    }

    // Proof F[1][1] Calc isn't executing properly

    cout << "F[1][1]=" << F[1][1] << "\n";
    cout << "C[1][1]=" << C[1][1] << "\n";
    cout << "K[1]=" << K[1] << "\n";
    cout << "C[1][1]*K[1]=" << C[1][1]*K[1] << "\n\n";

    ofstream WaveCheck("/Users/Folder/Wave Equation/WaveCheck.txt");
    WaveCheck << "Iteration\t" << "Time\t";

    for (m=1; m<=p; m++) {

        WaveCheck << "Displacement[" << m << "]\t" << "Velocity[" << m << "]\t";   

    }

    WaveCheck << "Force in Ram\t" << "Force in Capblock\n" << "(-)\t" << "(s)\t";

    for (m=1; m<=p; m++) {

        WaveCheck << "(m)\t" << "(m/s)\t";   

    }

    WaveCheck << "N\t" << "N\n";

    for (n=1; n<=T; n++) {

        WaveCheck << n << "\t";
        WaveCheck << n*t << "\t";

        for (m=1; m<=p; m++) {

            WaveCheck << D[n][m] << "\t";
            WaveCheck << V[n-1][m] << "\t";

        }

        WaveCheck << F[n][1] << "\t";
        WaveCheck << F[n][2] << "\n";   

    }

    WaveCheck.close();

    return 0;
}

int main(int argc, const char * argv[])
{
    int a;        // Depth Iteration
    int b;        // Total Number of Depths
    double d;     // Size of Depth iterations

    d=0.002;
    b=2;

    double Pen[b];       // Pen Caused by one blow
    double z[b];         // Depth Below Mudline
    double Bl_Ct[b];     // Blows per m

    ofstream Results("/Users/Folder/Results.txt");

    Results << "Depth" << "\t" << "Bl_Ct" << "\n";
    Results << "(meters)" << "\t" << "(-)" << "\n";

    // Blow vs Deformation Table

    for (a=1; a<=b; a++) {

        // Depth for each blow

        z[a]=d*a;
        Pen[a]=Wave(z[a]);

        // Blows per Meter
        Bl_Ct[a]=1/Pen[a];
        Results << z[a] << "\t" << Bl_Ct[a] << "\n";
        cout << Pen[a] << "\t";
        cout << Bl_Ct[a] << "\n";
    }

    Results.close();

    return 0;
}
I'm guessing you're referring to lines 189 & 193.

The step before it (181->185) is changing C. Are you sure you're still using the correct values to check your calculation?

It might be a good idea to simply print all numbers involved right before/after the calculation.
You should be examining the values involved at the point of calculation. Looking at them much later only tells you what they are much later.

Examine the value of K[1] at line 189.
I ran this on VC++ and K[1] is -9.2559631349317831e+061 and K[2] is 7.7923076923076920e-010.

It looks like K[1] never gets initialized.
Last edited on
Thanks I have it now, it was the K[1].
Topic archived. No new replies allowed.