MainPage.xaml.cs
// some theory: http://people.math.gatech.edu/~carlen/2507/notes/NewtonMethod.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

// data set:                      function: w0*x0 + w1*x1 + ... + wn
//
// x0, x1, ... | y1     
// x0, x1, ... | y2
// ...
// x0, x1, ... | ym
//

namespace TrainedSumNewton
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int i, j, m, n, iter;
            int[] datax;
            int[] y;
            float[] temp;
            float[] grad;
            float[] w;
            float gradsize2;
            float E;
            float error = 0.000001f;
            int[] numbers;

            m = 7; n = 5;
            datax = new int[m * n];
            y = new int[m];
            temp = new float[m];
            grad = new float[n + 1];
            w = new float[n + 1];
            numbers = new int[n];

            // data set
            datax[0] = 1; datax[1] = 2; datax[2] = 3; datax[3] = 4; datax[4] = 5; y[0] = 15;
            datax[5] = 0; datax[6] = 3; datax[7] = 4; datax[8] = 5; datax[9] = 6; y[1] = 18;
            datax[10] = 3; datax[11] = 4; datax[12] = 5; datax[13] = 6; datax[14] = 7; y[2] = 25;
            datax[15] = 4; datax[16] = 5; datax[17] = 6; datax[18] = 7; datax[19] = 8; y[3] = 30;
            datax[20] = 0; datax[21] = 0; datax[22] = 0; datax[23] = 0; datax[24] = 10; y[4] = 10;
            datax[25] = 6; datax[26] = 7; datax[27] = 9; datax[28] = 10; datax[29] = 12; y[5] = 44;
            datax[30] = 0; datax[31] = 0; datax[32] = 0; datax[33] = 0; datax[34] = 0; y[6] = 0;

            // initial w
            w[0] = Convert.ToInt32(w0.Text);
            w[1] = Convert.ToInt32(w1.Text);
            w[2] = Convert.ToInt32(w2.Text);
            w[3] = Convert.ToInt32(w3.Text);
            w[4] = Convert.ToInt32(w4.Text);
            w[5] = Convert.ToInt32(w5.Text);

            iter = 0;
            do
            {
                for (j = 0; j < m; j++)
                {
                    temp[j] = 0;
                    for (i = 0; i < n; i++)
                    {
                        temp[j] += datax[j * n + i] * w[i];
                    }
                    temp[j] += w[n] - y[j];
                }

                for (i = 0; i < n; i++)
                {
                    grad[i] = 0;
                    for (j = 0; j < m; j++)
                    {
                        grad[i] += temp[j] * datax[j * n + i];
                    }
                }

                grad[n] = 0;
                for (j = 0; j < m; j++)
                {
                    grad[n] += temp[j];
                }

                gradsize2 = 0;
                for (i = 0; i <= n; i++)
                {
                    gradsize2 += grad[i] * grad[i];
                }
                gradsize2 = (float)Math.Abs(gradsize2);

                E = 0;
                for (j = 0; j < m; j++)
                {
                    E += 1f / 2f * temp[j] * temp[j];
                }

                for (i = 0; i <= n; i++)
                {
                    w[i] -= grad[i] / gradsize2 * E;
                }

                iter++;

            }
            while (gradsize2 > error && iter < 1000000);

            numbers[0] = Convert.ToInt32(number1.Text);
            numbers[1] = Convert.ToInt32(number2.Text);
            numbers[2] = Convert.ToInt32(number3.Text);
            numbers[3] = Convert.ToInt32(number4.Text);
            numbers[4] = Convert.ToInt32(number5.Text);

            float result;
            result = numbers[0] * w[0] + numbers[1] * w[1] + numbers[2] * w[2] +
                     numbers[3] * w[3] + numbers[4] * w[4] + w[5];

            resultstring.Text = "The result is: " + Convert.ToString(result)
                                + " iter: " + Convert.ToString(iter)
                                + " w0: " + Convert.ToString(w[0])
                                + " w1: " + Convert.ToString(w[1])
                                + " w2: " + Convert.ToString(w[2])
                                + " w3: " + Convert.ToString(w[3])
                                + " w4: " + Convert.ToString(w[4])
                                + " w5: " + Convert.ToString(w[5]);
        }
    }
}