Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
static void Main()
{
    double [][] pointset = new double[][]{
        new double[] {10,0.45},
        new double[] {3.091,12.9},
        new double[] {8.12,13.01},
        new double[] {6.23,8.731},
        new double[] {12.12,2.568},
        new double[] {9.51,5.89},
        new double[] {7.67,11.901}
    };
    double[] y = { 0, 0 };
    for (int i = 0; i < 20; i++) {
        y = algorithm(pointset, y);
        Console.WriteLine("({0:0.000},{1:0.000})",y[0],y[1]);
    }
    Console.ReadKey();
    }

static double[] algorithm(double[][] pointset, double[] y)
{
    double[] s1 = {0,0};
    double s2 = 0;
    foreach (double[] point in pointset) {
        double norm = Eucnorm(new double[] { point[0] - y[0], point[1] - y[1] });
        s1[0] += point[0] / norm;
        s1[1] += point[1] / norm;
        s2 += 1 / norm;
    }
    return new double[] {s1[0]/s2, s1[1]/s2};
}

static double Eucnorm(double[] v)
{
    return Math.Sqrt((v[0] * v[0] + v[1] * v[1]));
}
Posted
Updated 3-Dec-13 22:55pm
v2
Comments
Richard MacCutchan 4-Dec-13 4:55am    
I would start by learning PHP.
CPallini 4-Dec-13 5:00am    
You should try hard, however, since the OP cannot wait too long.
Richard MacCutchan 4-Dec-13 5:21am    
Well, I can spell it.

1 solution

PHP
$pointset = array(
    array(10, 0.45),
    array(10, 0.45),
    array(3.091,12.9),
    array(8.12,13.01),
    array(6.23,8.731),
    array(12.12,2.568),
    array(9.51,5.89),
    array(7.67,11.901)
);

$y = array(0,0);

for($i = 0; $i < 20; $i++){
  $y = algorithm($pointset, $y);
  echo $y[0] . ',' . $y[1] . '<br />';
}

function algorithm($pointset, $y){
    $s1 = array(0,0);
    $s2 = 0;
    foreach ($pointset as $point) {
        $norm = Eucnorm(array($point[0] - $y[0], $point[1] - $y[1] ));
        $s1[0] += $point[0] / $norm;
        $s1[1] += $point[1] / $norm;
        $s2 += 1 / $norm;
    }
    return array($s1[0]/ $s2, $s1[1] / $s2);
}
 
function Eucnorm($v){
    return sqrt(($v[0] * $v[0] + $v[1] * $v[1]));
}


And the output is:

8.4694229958382,6.3750562923086
8.7391111370073,6.5636031962359
8.7786455894498,6.5618090919414
8.7943588659231,6.5449477143646
8.8070652809877,6.5284004270569
8.8185637197715,6.5132713529887
8.8290814816784,6.4995149942758
8.8387123815852,6.4870066624231
8.8475329199354,6.4756275801103
8.8556122056453,6.4652704538268
8.8630132953696,6.4558387585837
8.8697937889668,6.4472456393321
8.8760063177511,6.4394128933972
8.8816989826087,6.4322700597592
8.8869157513509,6.4257536067247
8.8916968199918,6.4198062077897
8.8960789416409,6.4143760962772
8.9000957261516,6.4094164902423
8.9037779132619,6.4048850799758
8.9071536216511,6.4007435712189
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900