wpf输入二维数据拟合曲线代码
时间: 2023-10-10 10:06:26 浏览: 151
曲线拟合的代码
以下是基于WPF的二维数据拟合曲线代码:
首先,需要一个类来表示二维数据点:
public class DataPoint
{
public double X { get; set; }
public double Y { get; set; }
}
接下来,需要一个类来表示拟合函数:
public abstract class CurveFit
{
public abstract double Evaluate(double x);
public static CurveFit CreateFit(double[] x, double[] y, int degree)
{
CurveFit fit = null;
switch (degree)
{
case 1:
fit = new LinearFit(x, y);
break;
case 2:
fit = new QuadraticFit(x, y);
break;
case 3:
fit = new CubicFit(x, y);
break;
// add more cases for higher degrees if needed
}
return fit;
}
}
现在,可以创建具体的拟合函数类,例如一次函数:
public class LinearFit : CurveFit
{
private readonly double _a;
private readonly double _b;
public LinearFit(double[] x, double[] y)
{
int n = x.Length;
double sumx = x.Sum();
double sumy = y.Sum();
double sumxy = x.Zip(y, (xi, yi) => xi * yi).Sum();
double sumx2 = x.Select(xi => xi * xi).Sum();
_a = (n * sumxy - sumx * sumy) / (n * sumx2 - sumx * sumx);
_b = (sumy - _a * sumx) / n;
}
public override double Evaluate(double x)
{
return _a * x + _b;
}
}
同样地,可以创建二次函数和三次函数类:
public class QuadraticFit : CurveFit
{
// implementation omitted for brevity
}
public class CubicFit : CurveFit
{
// implementation omitted for brevity
}
现在,可以创建UI界面,其中包含一个DataGrid用于显示输入的数据点,以及一个ComboBox用于选择拟合函数的次数:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Curve Fitting" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Degree:"/>
<ComboBox x:Name="degreeComboBox" SelectedIndex="0" Width="50">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
<ComboBoxItem Content="3"/>
</ComboBox>
</StackPanel>
<DataGrid x:Name="dataGrid" Grid.Row="1" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="X" Binding="{Binding X}"/>
<DataGridTextColumn Header="Y" Binding="{Binding Y}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
接下来,在代码中,需要在窗口加载时将数据点添加到DataGrid中:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dataGrid.ItemsSource = new List<DataPoint>
{
new DataPoint { X = 0, Y = 1 },
new DataPoint { X = 1, Y = 2 },
new DataPoint { X = 2, Y = 3 },
new DataPoint { X = 3, Y = 4 },
new DataPoint { X = 4, Y = 5 },
new DataPoint { X = 5, Y = 6 },
new DataPoint { X = 6, Y = 7 },
new DataPoint { X = 7, Y = 8 },
new DataPoint { X = 8, Y = 9 },
new DataPoint { X = 9, Y = 10 }
};
}
}
最后,在ComboBox的SelectionChanged事件处理程序中,可以计算拟合函数,并将其绘制在一个Chart控件中:
private void degreeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int degree = degreeComboBox.SelectedIndex + 1;
double[] x = dataGrid.Items.OfType<DataPoint>().Select(dp => dp.X).ToArray();
double[] y = dataGrid.Items.OfType<DataPoint>().Select(dp => dp.Y).ToArray();
CurveFit fit = CurveFit.CreateFit(x, y, degree);
chart.Series.Clear();
var lineSeries = new LineSeries();
for (double xx = x.Min(); xx <= x.Max(); xx += 0.01)
{
lineSeries.Points.Add(new DataPoint(xx, fit.Evaluate(xx)));
}
chart.Series.Add(lineSeries);
}
在这个例子中,Chart控件是通过NuGet包OxyPlot.Wpf来实现的,因此需要在项目中安装这个包。
阅读全文