回复 2楼 TonyDeng
提示: 作者被禁止或删除 内容自动屏蔽
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using namespace WpfApplication1
{
public partial class MainWindow : Window
{
private Point _origin = new Point(0, 0); // 坐标系原点在屏幕上的绝对位置
private Double _xScale = 1.0; // x 轴放大系数
private Double _yScale = 1.0; // y 轴放大系数
private Double _Increase = 0.001; // 自变量步进幅度
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Create_Axis(Grid1.ActualWidth / 2, Grid1.ActualHeight / 2, "O", "x", "y", 80.0, 2.0);
Draw_Picture();
}
// 建立右手直角坐标系
private void Create_Axis(Double x, Double y, String oTitle, String xTitle, String yTitle, Double xScale, Double yScale)
{
_origin.X = x;
_origin.Y = y;
_xScale = xScale;
_yScale = yScale;
Grid1.Children.Add(new Label()
{
Width = 20,
Height = 30,
Content = oTitle,
FontFamily = new FontFamily("Times New Roman"),
Margin = new Thickness(_origin.X - 20, _origin.Y, _origin.X, _origin.Y - 30),
Foreground = Brushes.Magenta,
Background = Brushes.Transparent
});
Double arrowLength = 10.0;
Double arrowAngle = Math.PI / 6;
Line x_Axis = new Line();
x_Axis.Stroke = Brushes.Magenta;
x_Axis.X1 = 0;
x_Axis.Y1 = _origin.Y;
x_Axis.X2 = Grid1.ActualWidth;
x_Axis.Y2 = x_Axis.Y1;
Grid1.Children.Add(x_Axis);
Line x_Axis1 = new Line();
x_Axis1.Stroke = Brushes.Magenta;
x_Axis1.X1 = x_Axis.X2;
x_Axis1.Y1 = x_Axis.Y2;
x_Axis1.X2 = x_Axis1.X1 - arrowLength * Math.Cos(arrowAngle);
x_Axis1.Y2 = x_Axis1.Y1 - arrowLength * Math.Sin(arrowAngle);
Grid1.Children.Add(x_Axis1);
Line x_Axis2 = new Line();
x_Axis2.Stroke = Brushes.Magenta;
x_Axis2.X1 = x_Axis.X2;
x_Axis2.Y1 = x_Axis.Y2;
x_Axis2.X2 = x_Axis2.X1 - 10 * Math.Cos(arrowAngle);
x_Axis2.Y2 = x_Axis2.Y1 + 10 * Math.Sin(arrowAngle);
Grid1.Children.Add(x_Axis2);
Grid1.Children.Add(new Label()
{
Width = 20,
Height = 30,
Content = xTitle,
FontFamily = new FontFamily("Times New Roman"),
Margin = new Thickness(Grid1.ActualWidth - 20, _origin.Y, 0, _origin.Y - 30),
Foreground = Brushes.Magenta,
Background = Brushes.Transparent
});
Line y_Axis = new Line();
y_Axis.Stroke = Brushes.Magenta;
y_Axis.X1 = _origin.X;
y_Axis.Y1 = Grid1.ActualHeight;
y_Axis.X2 = y_Axis.X1;
y_Axis.Y2 = 0;
Grid1.Children.Add(y_Axis);
Line y_Axis1 = new Line();
y_Axis1.Stroke = Brushes.Magenta;
y_Axis1.X1 = y_Axis.X2;
y_Axis1.Y1 = y_Axis.Y2;
y_Axis1.X2 = y_Axis1.X1 - arrowLength * Math.Sin(arrowAngle);
y_Axis1.Y2 = y_Axis1.Y1 + arrowLength * Math.Cos(arrowAngle);
Grid1.Children.Add(y_Axis1);
Line y_Axis2 = new Line();
y_Axis2.Stroke = Brushes.Magenta;
y_Axis2.X1 = y_Axis.X2;
y_Axis2.Y1 = y_Axis.Y2;
y_Axis2.X2 = y_Axis2.X1 + 10 * Math.Sin(arrowAngle);
y_Axis2.Y2 = y_Axis2.Y1 + 10 * Math.Cos(arrowAngle);
Grid1.Children.Add(y_Axis2);
Grid1.Children.Add(new Label()
{
Width = 20,
Height = 30,
Content = yTitle,
FontFamily = new FontFamily("Times New Roman"),
Margin = new Thickness(_origin.X, 0, _origin.X - 20, Grid1.ActualHeight - 30),
Foreground = Brushes.Magenta,
Background = Brushes.Transparent
});
}
// 将数学点转换为屏幕坐标
private Point Calculate_ScreenPoint(Double x, Double y)
{
return new Point(_origin.X + x * _xScale, _origin.Y - y * _yScale);
}
// 原函数
private Double The_Function(Double x)
{
return 2 * Math.Pow(x, 3) - 4 * Math.Pow(x, 2) + 3 * x - 6;
}
// 原函数的一阶导数
private Double The_Function1(Double x)
{
return 6 * Math.Pow(x, 2) - 8 * x + 3;
}
// 原函数的二阶导数
private Double The_Function2(Double x)
{
return 12 * x - 8;
}
private void Draw_Picture()
{
// 原函数图像
Polyline myPolyline = new Polyline();
myPolyline.Stroke = Brushes.Black;
myPolyline.StrokeThickness = 1;
myPolyline.FillRule = FillRule.EvenOdd;
PointCollection myPointCollection = new PointCollection();
Double start = -4;
Double end = 5;
for (Double x = start; x < end; x += _Increase)
{
myPointCollection.Add(Calculate_ScreenPoint(x, The_Function(x)));
}
myPolyline.Points = myPointCollection;
Grid1.Children.Add(myPolyline);
// 用牛顿切线法求方程的数值解
Double x1 = 1; // 解区间左端点
Double x2 = 5; // 解区间右端点
Double error = 0.0001; // 解的精度范围
Double result = x1; // 解结果
Point p1, p2, p3;
p1 = Calculate_ScreenPoint(1, 0);
p2 = Calculate_ScreenPoint(1, The_Function(1));
Grid1.Children.Add(new Line() { X1 = p1.X, Y1 = p1.Y, X2 = p2.X, Y2 = p2.Y, Stroke = Brushes.DodgerBlue });
Grid1.Children.Add(new Label()
{
Width = 80,
Height = 30,
Content = "x = " + x1.ToString("F4"),
FontFamily = new FontFamily("Times New Roman"),
Margin = new Thickness(p1.X - 40, p1.Y - 30, Grid1.ActualWidth - p1.X - 40, p1.Y),
Foreground = Brushes.Magenta,
Background = Brushes.Transparent
});
p3 = Calculate_ScreenPoint(5, 0);
Grid1.Children.Add(new Label()
{
Width = 80,
Height = 30,
Content = "x = " + x2.ToString("F4"),
FontFamily = new FontFamily("Times New Roman"),
Margin = new Thickness(p3.X - 40, p3.Y, Grid1.ActualWidth - p3.X - 40, p3.Y - 30),
Foreground = Brushes.Magenta,
Background = Brushes.Transparent
});
while (Math.Abs(The_Function(result)) > error)
{
Double xTemp = 0.0;
if (The_Function(x1) * The_Function2(x1) > 0)
{
xTemp = x1;
x1 -= The_Function(x1) / The_Function1(x1);
result = x1;
}
else if (The_Function(x2) * The_Function2(x2) > 0)
{
xTemp = x2;
x2 -= The_Function(x2) / The_Function1(x2);
result = x2;
}
p1 = Calculate_ScreenPoint(xTemp, The_Function(xTemp));
p2 = Calculate_ScreenPoint(xTemp, 0);
p3 = Calculate_ScreenPoint(result, 0);
Grid1.Children.Add(new Line() { X1 = p1.X, Y1 = p1.Y, X2 = p2.X, Y2 = p2.Y, Stroke = Brushes.DodgerBlue });
Grid1.Children.Add(new Line() { X1 = p1.X, Y1 = p1.Y, X2 = p3.X, Y2 = p3.Y, Stroke = Brushes.LightSkyBlue });
}
p1 = Calculate_ScreenPoint(result, 0);
Grid1.Children.Add(new Label()
{
Width = 80,
Height = 30,
Content = "x = " + result.ToString("F4"),
Margin = new Thickness(p1.X - 40, p1.Y, Grid1.ActualWidth - p1.X - 40, p1.Y - 30),
Foreground = Brushes.Red,
Background = Brushes.Transparent
});
}
}
}
