画函数图形的C#程序,兼论一个病态函数 (转)

本文介绍了一个使用C#编写的程序,用于绘制指定函数的图像,并讨论了一个病态函数的例子,展示了如何通过调整函数参数来观察图像的变化。此外,还探讨了该程序在实现上的局限性。

该程序在屏幕上画出指定的函数的图像,函数的自变量的取值范围由用户指定,程序自动计算该区间内函数的值的取值范围。首先,让我们来看一幅正弦函数: f(x)  =  sin(x) 的图像:
plot-01.PNG
这幅图像中,ClientSize: 529x152 表示该程序画图区宽度为 529 个像素,高度为 152 个像素。该函数自变量 x 的取值范围从 -3.5 到 3.5,函数值 y 的取值范围应该是从 -1 到 1,但因为浮点数运算误差的关系,程序计算出来的是从 -0.999979504787793 到 0.999979504787793。接下来的 rx 表示函数自变量每单位值用多少个象素表示,ry  表示函数的值每单位值用多少个象素表示,r 值如果小于1表示图形纵向被压扁,反之则被拉伸。

plot-02.PNG 
这是抛物线的图像吗?不,这就是我们要讨论的病态函数: f(x) = 3 * x2 + π-4 * ln[(π-x)2] + 1 在 x 位于区间 [-10, 10] 之间的图像。程序计算出来的函数在这区间的取值范围是从 1.02350355338907 到 301.052885868965。r  值是 0.0165397334645646,表明该图像被极大地压扁,也就是,实际的图像要高得多。但是,由于这个函数是病态的,程序的计算实际上是错误的,在该区间内,函数的最小值应该是负无穷大,并且,在 π - 10-667 到 π + 10-667 这一极小区间内方程 f(x) = 0 有两个实根。让我们在 π 值附近放大图像看一看: 
plot-03.PNG
plot-04.PNG
plot-05.PNG
这下看得比较清楚了吧!
实际上这个病态函数是《C数值算法(第二版)》第三章“内插法和外推法”中提到的:

---------------------------------------------------------------------------
可以很容易地构造一些病态函数使内插法失败。例如,考虑函数 
f(x) = 3 * x2 + π-4 * ln[(π-x)2] + 1
它除了 x = π 之外都有定义,而 x = π 时无定义,其它情况,值有正有负。而这函数在任何基于数值 x = 3.13, 3.14, 3.15, 3.16 的插值法,都肯定在 x = 3.1416 处得到一个错误的解,尽管通过这五个点所画的曲线确实相当平滑!(用计算器试试看。)
---------------------------------------------------------------------------

这个画函数图形的C#程序有一个严重的缺点,就是函数表达式是直接写的源程序中的,不能象SciLab和Matlab那样交互式地输入。不知道用 System.Reflection.Emit.ILGenerator 类能不能动态地生成用户输入的函数表达式?好了,下面就是该程序的源代码:

None.gif //  plot.cs: 画函数图形, 编译方法: csc /t:winexe plot.cs
None.gif
using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  Skyiv.Ben.Plot
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
sealed class PlotForm : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
// 要画的函数,如果能在 TextBox 中输入函数的表达式就更好了
InBlock.gif
    double Function(double x)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
double u = Math.PI - x;
InBlock.gif      
double pi2 = Math.PI * Math.PI;
InBlock.gif      
return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// 仅仅用来显示在屏幕上
InBlock.gif
    string FunctionString()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return "f(x) = 3 * x^2 + pi^-4 * ln[(pi-x)^2] + 1";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
const int yBase = 24// 屏幕保留区域的高度
InBlock.gif

InBlock.gif    TextBox tbxX0, tbxX1; 
// 函数自变量的取值范围
InBlock.gif
    
InBlock.gif    PlotForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      SuspendLayout();
InBlock.gif      
InBlock.gif      Button btnSubmit 
= new Button();
InBlock.gif      btnSubmit.Text 
= "刷新";
InBlock.gif      btnSubmit.Location 
= new Point(00);
InBlock.gif      btnSubmit.Size 
= new Size(4824);
InBlock.gif      btnSubmit.Click 
+= new EventHandler(BtnSubmit_Click);
InBlock.gif
InBlock.gif      tbxX0 
= new TextBox();
InBlock.gif      tbxX0.Text 
= "-10";
InBlock.gif      tbxX0.Location 
= new Point(553);
InBlock.gif      tbxX0.Size 
= new Size(15020);
InBlock.gif
InBlock.gif      tbxX1 
= new TextBox();
InBlock.gif      tbxX1.Text 
= "10";
InBlock.gif      tbxX1.Location 
= new Point(2103);
InBlock.gif      tbxX1.Size 
= new Size(15020);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      Controls.AddRange(
new Control[]dot.gif{btnSubmit, tbxX0, tbxX1});
InBlock.gif      Text 
= "Plot";
InBlock.gif      BackColor 
= Color.White;
InBlock.gif      ClientSize 
= new Size(600600 + yBase);
InBlock.gif      
// WindowState = FormWindowState.Maximized;
InBlock.gif

InBlock.gif      ResumeLayout(
false);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void BtnSubmit_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Invalidate();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
protected override void OnSizeChanged(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Invalidate();
InBlock.gif      
base.OnSizeChanged(e);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
protected override void OnPaint(PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
double x0 = double.Parse(tbxX0.Text);
InBlock.gif      
double x1 = double.Parse(tbxX1.Text);
InBlock.gif      Graphics gc 
= e.Graphics;
InBlock.gif      Size size 
= ClientSize;
InBlock.gif      
int i0 = 0;
InBlock.gif      
int i1 = size.Width - 1;
InBlock.gif      
int j0 = yBase;
InBlock.gif      
int j1 = size.Height - 1;
InBlock.gif      Pen pen 
= new Pen(Color.Black, 1);
InBlock.gif      gc.DrawLine(pen, i0, j0, i1, j0); 
// 画图区和保留区的分界线
InBlock.gif
      double rx = (x1 - x0) / (i1 - i0);
InBlock.gif      
double y0, y1;
InBlock.gif      GetFunctionValueRange(x0, rx, i0, i1, 
out y0, out y1);
InBlock.gif      
double ry = (y1 - y0) / (j1 - j0);
InBlock.gif      Out(gc, 
0"ClientSize: {0}x{1}", i1 - i0 + 1, j1 - j0 + 1);
InBlock.gif      Out(gc, 
1, FunctionString());
InBlock.gif      Out(gc, 
2"x:[{0}, {1}] range:{2}", x0, x1, x1 - x0);
InBlock.gif      Out(gc, 
3"y:[{0}, {1}] range:{2}", y0, y1, y1 - y0);
InBlock.gif      Out(gc, 
4"rx:{0}"1 / rx);  // 函数自变量每单位值用多少个象素表示
InBlock.gif
      Out(gc, 5"ry:{0}"1 / ry);  // 函数的值每单位值用多少个象素表示
InBlock.gif
      Out(gc, 6"r :{0}", rx / ry); // 该值如果小于1表示图形纵向被压扁,反之则被拉伸
InBlock.gif
      pen.Color = Color.Green;
InBlock.gif      
int j = j1 + (int)(y0 / ry);
InBlock.gif      
if (j >= j0 && j <= j1) gc.DrawLine(pen, i0, j, i1, j); // x坐标轴
InBlock.gif
      int i = i0 - (int)(x0 / rx);
InBlock.gif      
if (i >= i0 && i <= i1) gc.DrawLine(pen, i, j0, i, j1); // y坐标轴
InBlock.gif
      pen.Color = Color.Red;
InBlock.gif      
for (i = i0; i <= i1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
double x = x0 + (i - i0) * rx;
InBlock.gif        
double y = Function(x);
InBlock.gif        
if (double.IsInfinity(y) || double.IsNaN(y)) continue;
InBlock.gif        j 
= j1 - (int)((y - y0) / ry);
InBlock.gif        
if (j > j1 || j < j0) continue;
InBlock.gif        gc.DrawLine(pen, i, j, i 
+ 1, j); // 画函数的图形
ExpandedSubBlockEnd.gif
      }

InBlock.gif      
base.OnPaint(e);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// 函数值的取值范围
InBlock.gif
    void GetFunctionValueRange(double x0, double rx, int i0, int i1, out double y0, out double y1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      y0 
= double.MaxValue;
InBlock.gif      y1 
= double.MinValue;
InBlock.gif      
for (int i = i0; i <= i1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
double x = x0 + (i - i0) * rx;
InBlock.gif        
double y = Function(x);
InBlock.gif        
if (double.IsInfinity(y) || double.IsNaN(y)) continue;
InBlock.gif        
if (y0 > y) y0 = y;
InBlock.gif        
if (y1 < y) y1 = y;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// 在指定的位置写字符串
InBlock.gif
    void Out(Graphics gc, int line, string fmt, params object [] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      gc.DrawString(
string.Format(fmt, args), new Font("Courier New"10), Brushes.Blue, new PointF(5, yBase + 15 * line));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Application.Run(
new PlotForm());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/gjahead/archive/2007/08/18/861162.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值