Example: SplineData Chart
This example makes use of the SplineData class as well as the two spline smoothing classes in Imsl.Math
.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Imsl.Math;
using Imsl.Chart2D;
using Random = Imsl.Stat.Random;
public class SplineDataEx1 : FrameChart
{
private const int nData = 21;
private const int nSpline = 100;
public SplineDataEx1()
{
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);
chart.ChartTitle.SetTitle(new Text("Smoothed Spline"));
Legend legend = chart.Legend;
legend.SetTitle(new Text("Legend"));
legend.SetViewport(0.7, 0.9, 0.1, 0.3);
legend.IsVisible = true;
// Original data
double[] xData = grid(nData);
double[] yData = new double[nData];
for (int k = 0; k < nData; k++)
{
yData[k] = f(xData[k]);
}
Data data = new Data(axis, xData, yData);
data.DataType = Imsl.Chart2D.Data.DATA_TYPE_MARKER;
data.MarkerType = Data.MARKER_TYPE_HOLLOW_CIRCLE;
data.MarkerColor = System.Drawing.Color.Red;
data.SetTitle("Original Data");
// Noisy data
Random random = new Random(123457);
double[] yNoisy = new double[nData];
for (int k = 0; k < nData; k++)
{
yNoisy[k] = yData[k] + (2.0 * random.NextDouble() - 1.0);
}
data = new Data(axis, xData, yNoisy);
data.DataType = Imsl.Chart2D.Data.DATA_TYPE_MARKER;
data.MarkerType = Data.MARKER_TYPE_FILLED_SQUARE;
data.MarkerSize = 0.75;
data.MarkerColor = System.Drawing.Color.Blue;
data.SetTitle("Noisy Data");
chartSpline(axis, new CsSmooth(xData, yData), System.Drawing.Color.Red,
"CsSmooth");
chartSpline(axis, new CsSmoothC2(xData, yData, nData),
System.Drawing.Color.Orange, "CsSmoothC2");
}
static private void chartSpline(AxisXY axis, Imsl.Math.Spline spline,
System.Drawing.Color color, System.String title)
{
Data data = new SplineData(axis, spline);
data.DataType = Imsl.Chart2D.Data.DATA_TYPE_LINE;
data.LineColor = color;
data.SetTitle(title);
}
static private double[] grid(int nData)
{
double[] xData = new double[nData];
for (int k = 0; k < nData; k++)
{
xData[k] = 3.0 * k / (double) (nData - 1);
}
return xData;
}
static private double f(double x)
{
return 1.0 / (0.1 + System.Math.Pow(3.0 * (x - 1.0), 4));
}
public static void Main(string[] argv)
{
System.Windows.Forms.Application.Run(new SplineDataEx1());
}
}
Output
Link to C# source.