wpf对excel中的二维浮点数据进行拟合,输出函数方程和函数曲线图,写出xaml代码和后端代码,列出需要的nuget库
时间: 2024-02-20 17:57:35 浏览: 123
为了对Excel中的二维浮点数据进行拟合,我们可以使用Math.NET Numerics库。以下是需要的nuget库:
- MathNet.Numerics
- MathNet.Numerics.Data.Excel
下面是XAML代码:
```xml
<Window x:Class="ExcelDataFitting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ExcelDataFitting"
Title="Excel Data Fitting" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Content="Excel File Path:"/>
<TextBox x:Name="FilePathTextBox" Grid.Row="0" Margin="5" VerticalAlignment="Center" Width="300"/>
<Button x:Name="OpenFileButton" Grid.Row="0" Margin="5" VerticalAlignment="Center" Width="80" Content="Open"/>
<Button x:Name="FitButton" Grid.Row="1" Margin="5" VerticalAlignment="Center" Width="80" Content="Fit"/>
<Grid x:Name="PlotGrid" Grid.Row="2"/>
</Grid>
</Window>
```
在后端代码中,我们需要使用以下命名空间:
```csharp
using System.Collections.Generic;
using System.IO;
using MathNet.Numerics;
using MathNet.Numerics.Data.Excel;
using MathNet.Numerics.LinearRegression;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
```
然后在窗口的构造函数中,我们可以添加以下代码:
```csharp
var plotModel = new PlotModel {Title = "Data Fitting"};
var linearSeries = new LineSeries {Title = "Linear"};
var polynomialSeries = new LineSeries {Title = "Polynomial"};
plotModel.Series.Add(linearSeries);
plotModel.Series.Add(polynomialSeries);
var xAxis = new LinearAxis {Position = AxisPosition.Bottom, Title = "X"};
var yAxis = new LinearAxis {Position = AxisPosition.Left, Title = "Y"};
plotModel.Axes.Add(xAxis);
plotModel.Axes.Add(yAxis);
PlotGrid.Children.Add(new PlotView {Model = plotModel});
OpenFileButton.Click += (sender, args) =>
{
var openFileDialog = new OpenFileDialog {Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"};
if (openFileDialog.ShowDialog() == true)
{
FilePathTextBox.Text = openFileDialog.FileName;
}
};
FitButton.Click += (sender, args) =>
{
var data = ExcelReader.Read<double>(FilePathTextBox.Text, "Sheet1");
var x = data.Column(0).ToArray();
var y = data.Column(1).ToArray();
var linearRegression = SimpleRegression.Fit(x, y);
var polynomialRegression = Polynomial.Fit(x, y, 3);
linearSeries.Points.Clear();
polynomialSeries.Points.Clear();
for (var i = 0; i < x.Length; i++)
{
linearSeries.Points.Add(new DataPoint(x[i], linearRegression.Intercept + linearRegression.Slope * x[i]));
polynomialSeries.Points.Add(new DataPoint(x[i], polynomialRegression.Item1 * Math.Pow(x[i], 3) + polynomialRegression.Item2 * Math.Pow(x[i], 2) + polynomialRegression.Item3 * x[i] + polynomialRegression.Item4));
}
plotModel.InvalidatePlot(true);
};
```
在上面的代码中,我们首先创建了一个PlotModel,并添加了两个LineSeries(Linear和Polynomial),并将其添加到了PlotGrid中。然后,我们为PlotModel添加了两个LinearAxis(X轴和Y轴)。在按钮的Click事件处理程序中,我们首先读取Excel文件中的数据,然后分别使用SimpleRegression和Polynomial.Fit方法进行线性回归和多项式回归。我们清空了两个LineSeries的数据点,并将拟合后的数据点添加到了这两个LineSeries中。最后,我们使用plotModel.InvalidatePlot方法更新了PlotModel。
阅读全文