Esempio n. 1
0
 public void Apply()
 {
     item.Name         = name.Text;
     item.Dimensions   = (int)Dimensions.Value;
     item.ErrorColumns = Errors.Checked;
     item.Compile();
     Reset();
     Model.Items.Update(item);
 }
Esempio n. 2
0
        public FitDemo(PlotControl plot)
        {
            this.plot = plot;

            plot.Model.Clear();

            // create a new DataItem
            data = new DataItem();

            data.Dimensions   = 2;              // This property is only used by the LoadText method, it is not required for the fit algorithm.
            data.ErrorColumns = true;           // This property is only used by the LoadText method, it is not required for the fit algorithm.

            // preset the x, dx and dy values to functions:
            data.x.Source  = "n/Length*12 - 8"; // place the data in the visible plot range (x0: -8; x1: 4 in this demo application)
            data.dx.Source = "0.5";             // set the x errors to 0.5 (the x errors are not used by the fit algorithm)
            data.dy.Source = "y[n]*0.2";        // set y errors to 20% of the x values
            data.Compile();

            data.LoadText("Gauss data.txt", "\n");             // Load the data from the "Gauss data.txt" text file.

            // Display data
            plot.Model.Add(data);

            // create a new function
            f = new Function1DItem(@"
				// M - gauss curves with derivative information in dfdp.
				const int M = 3;
				double arg, ex, fac, sum = 0;
				for (int n = 0; n < 3*M; n += 3) {
					arg = (x - p[n + 1])/p[n + 2];
					ex = Math.Exp(-arg*arg);
					fac = p[n]*ex*2*arg;

					// Compute derivative information in order to speed up the Marquardt fitting algorithm. Marquardt also works with no
					// derivative information provided by the funciton, so you can also omit this, it then computes numerical derivatives.
					// The NelderMead fitting algorithm uses no derivatives.
					dfdp[n] = ex;
					dfdp[n + 1] = fac/p[n + 2];
					dfdp[n + 2] = fac*arg/p[n + 2];

					// compute the sum over all gauss curves.
					sum += p[n]*ex;
				}
				return sum;
			"            );
            f.Compile();

            // Set initial fit guess.

            ResetGuess();

            // Display function
            plot.Model.Add(f);
        }
Esempio n. 3
0
 private void Compile()
 {
     data.Compile(true);
     if (!data.compiled)
     {
         string msg = "There are errors in the formulas:\n";
         for (int i = 0; i < data.errors.Length; i++)
         {
             msg += data.errors[i] + "\n";
         }
         MessageBox.Show(msg, "Compile error");
     }
 }
Esempio n. 4
0
 public void Apply()
 {
     item.loadsource = source.Text;
     item.Compile(true);
     if (!item.compiled)
     {
         string text = "Compile Errors:\n";
         for (int i = 0; i < item.errors.Length; i++)
         {
             text += item.errors[i] + "\n";
         }
         MessageBox.Show(text);
     }
 }
Esempio n. 5
0
 public void Apply()
 {
     item.name      = name.Text;
     item.Color     = colorLabel.BackColor;
     item.lineStyle = (DashStyle)lineStyle.SelectedIndex;
     try {
         item.lineWidth = float.Parse(lineWidth.Text);
     } catch { item.lineWidth = 1; }
     item.lines = line.Checked;
     item.marks = marks.Checked;
     item.Compile(true);
     Reset();
     if (!deleted)
     {
         graph.Invalidate();
     }
     mainform.ResetMenu();
 }
Esempio n. 6
0
        private void asciiClick(object sender, System.EventArgs e)
        {
            DataItem data = new DataItem();

            //The loadsource represents the body of the following function:
            //void Load(System.IO.FileStream stream) {
            //  ...
            //}
            //The function loads the data from the stream and sets the arrays x, y, dx and dy accordingly.
            //The size of the arrays is automatically adjusted upon access.
            data.loadsource =
                "using (StreamReader r = new StreamReader(stream)) {" +
                "  int n = 0; string line; string[] tokens;" +
                "  char[] separator = \";,|\".ToCharArray();" +
                "  while ((line = r.ReadLine()) != null) {" +
                "    tokens = line.Split(separator);" +
                "    try {x[n] = double.Parse(tokens[0]);} catch {x[n] = 0;}" +
                "    try {y[n] = double.Parse(tokens[1]);} catch {y[n] = 0;}" +
                "    try {dx[n] = double.Parse(tokens[2]);} catch {dx[n] = 0;}" +
                "    try {dy[n] = double.Parse(tokens[3]);} catch {dy[n] = 0;}" +
                "    n++;" +
                "  }" +
                "}";
            data.Compile(true);
            if (!data.compiled)
            {
                MessageBox.Show("Error in sourcecode:\n" + data.errors[0]);
            }
            try {
                data.LoadFromFile("data.csv");
            } catch (Exception ex) {
                MessageBox.Show("Could not open the file data.csv\n" + ex.Message);
            }
            graph.Model.Items.Add(data);
            Console.WriteLine("data Length: {0}", data.Length);
            Console.WriteLine("x     y     dx     dy");
            for (int n = 0; n < data.Length; n++)
            {
                Console.WriteLine("{0}, {1}, {2}, {3}", data.x[n], data.y[n], data.dx[n], data.dy[n]);
            }
            graph.Invalidate();
        }