Lazarus继承了Delphi7的界面风格,通过拖拉控件,得到一个能用的界面:
其中X0指解方程时迭代的初始值,需要猜测选择一个值。
err指允许的误差。
逻辑代码:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus, Math;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit; { Intercept }
Edit2: TEdit; { B1 }
Edit3: TEdit; { B2 }
Edit4: TEdit; { B3 }
Edit5: TEdit; { Y }
Edit6: TEdit; { X }
Edit7: TEdit; { X0 }
Edit8: TEdit; { err }
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function cubic(x: Double; abc: array of Double): Double;
begin
cubic := abc[0] + abc[1] * x + abc[2] * Power(x, 2) + abc[3] * Power(x, 3) - abc[4];
end;
function cubic_(x:Double; abc: array of Double): Double;
begin
cubic_ := abc[1] + 2 * x * abc[2] + 3 * abc[3] * Power(x, 2);
end;
function fixedPoint(x0: Double; e0: Double; abc: array of Double): Double;
Var
y, y_, x_: Double;
begin
y := cubic(x0, abc);
if Abs(y) < e0 then
fixedPoint := x0
else
begin
y_ := cubic_(x0, abc);
x_ := x0 - y / y_;
fixedPoint := fixedPoint(x_, e0, abc);
end;
end;
function round4(x:Double):string;
begin
round4 := FloatToStr(Round(x * 10000) / 10000);
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
op: Double;
x1: Double;
e1: Double;
abc: array of Double;
begin
x1 := StrtoFloat(Edit7.Text);
e1 := StrtoFloat(Edit8.Text);
abc := [StrtoFloat(Edit1.Text), StrtoFloat(Edit2.Text), StrtoFloat(Edit3.Text), StrtoFloat(Edit4.Text), StrtoFloat(Edit5.Text)];
op := fixedPoint(x1, e1, abc);
Edit6.Text := round4(op);
end;
end.
以上是Free Pascal代码,目前使用的人已经很少,但是阅读起来并不困难。除了几个小点要注意。
:= 表示赋值
= 表示判断
函数定义中的形参列表用;隔开
函数调用的时候实参使用,隔开
编译,运行,得到程序,输入参数试验一下: