Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization method for finding floating status of an object

The problem to solve is finding the floating status of a floating body, given its weight and the center of gravity.

The function i use calculates the displaced volume and center of bouyance of the body given sinkage, heel and trim. Where sinkage is a length unit and heel/trim is an angle limited to a value from -90 to 90.

Illustration of the three variables

The floating status is found when displaced volum is equal to weight and the center of gravity is in a vertical line with center of bouancy.

I have this implemeted as a non-linear Newton-Raphson root finding problem with 3 variables (sinkage, trim, heel) and 3 equations. This method works, but needs good initial guesses. So I am hoping to find either a better approach for this, or a good method to find the initial values.

Below is the code for the newton and jacobian algorithm used for the Newton-Raphson iteration. The function volume takes the parameters sinkage, heel and trim. And returns volume, and the coordinates for center of bouyancy.

I also included the maxabs and GSolve2 algorithms, I belive these are taken from Numerical Recipies.

void jacobian(float x[], float weight, float vcg, float tcg, float lcg, float jac[][3], float f0[]) {
    float h = 0.0001f;
    float temp;
    float j_volume, j_vcb, j_lcb, j_tcb;
    float f1[3];

    volume(x[0], x[1], x[2], j_volume, j_lcb, j_vcb, j_tcb);
    f0[0] = j_volume-weight;
    f0[1] = j_tcb-tcg;
    f0[2] = j_lcb-lcg;

    for (int i=0;i<3;i++) {
        temp = x[i];
        x[i] = temp + h;
        volume(x[0], x[1], x[2], j_volume, j_lcb, j_vcb, j_tcb);

        f1[0] = j_volume-weight;
        f1[1] = j_tcb-tcg;
        f1[2] = j_lcb-lcg;
        x[i] = temp;

        jac[0][i] = (f1[0]-f0[0])/h;
        jac[1][i] = (f1[1]-f0[1])/h;
        jac[2][i] = (f1[2]-f0[2])/h;
    }
}


void newton(float weight, float vcg, float tcg, float lcg, float &sinkage, float &heel, float &trim) {
    float x[3] = {10,1,1};

    float accuracy = 0.000001f;
    int ntryes = 30;
    int i = 0;
    float jac[3][3];
    float max;
    float f0[3];
    float gauss_f0[3];

    while (i < ntryes) {
        jacobian(x, weight, vcg, tcg, lcg, jac, f0);

        if (sqrt((f0[0]*f0[0]+f0[1]*f0[1]+f0[2]*f0[2])/2) < accuracy) {
            break;
        }

        gauss_f0[0] = -f0[0];
        gauss_f0[1] = -f0[1];
        gauss_f0[2] = -f0[2];

        GSolve2(jac, 3, gauss_f0);

        x[0] = x[0]+gauss_f0[0];
        x[1] = x[1]+gauss_f0[1];
        x[2] = x[2]+gauss_f0[2];

        // absmax(x) - Return absolute max value from an array
        max = absmax(x);
        if (max < 1) max = 1;

        if (sqrt((gauss_f0[0]*gauss_f0[0]+gauss_f0[1]*gauss_f0[1]+gauss_f0[2]*gauss_f0[2])) < accuracy*max) {
            x[0]=x2[0];
            x[1]=x2[1];
            x[2]=x2[2];
            break;
        }

        i++;
    }

    sinkage = x[0];
    heel = x[1];
    trim = x[2];
}

int GSolve2(float a[][3],int n,float b[]) {
    float x,sum,max,temp;
    int i,j,k,p,m,pos;

    int nn = n-1; 

    for (k=0;k<=n-1;k++)
    {
        /* pivot*/
        max=fabs(a[k][k]);
        pos=k;


        for (p=k;p<n;p++){
            if (max < fabs(a[p][k])){
                max=fabs(a[p][k]);
                pos=p;
            }
        }

        if (ABS(a[k][pos]) < EPS) {
            writeLog("Matrix is singular");
            break;
        }

        if (pos != k) {
            for(m=k;m<n;m++){
                temp=a[pos][m];
                a[pos][m]=a[k][m];
                a[k][m]=temp;
            }
        }

        /*   convert to upper triangular form */
        if ( fabs(a[k][k])>=1.e-6)
        {
            for (i=k+1;i<n;i++)
            {
            x = a[i][k]/a[k][k];
            for (j=k+1;j<n;j++) a[i][j] = a[i][j] -a[k][j]*x;
            b[i] = b[i] - b[k]*x;
            }
        }
        else
        {
            writeLog("zero pivot found in line:%d",k);
            return 0;
       }
     }

/*   back substitution */
     b[nn] = b[nn] / a[nn][nn];
     for (i=n-2;i>=0;i--)
     {
        sum = b[i];
         for (j=i+1;j<n;j++) 
           sum = sum - a[i][j]*b[j];
        b[i] = sum/a[i][i];
     }
     return 0;
}

float absmax(float x[]) {
    int i = 1;
    int n = sizeof(x);
    float max = x[0];
    while (i < n) {
        if (max < x[i]) {
            max = x[i];
        }
        i++;
    }
    return max;
}
like image 886
user978281 Avatar asked Oct 06 '22 05:10

user978281


1 Answers

Have you considered some stochastic search methods to find the initial value and then fine-tuning with Newton Raphson? One possibility is evolutionary computation, you can use the Inspyred package. For a physical problem similar in many ways to the one you describe, look at this example: http://inspyred.github.com/tutorial.html#lunar-explorer

like image 142
Mike Vella Avatar answered Oct 07 '22 17:10

Mike Vella