使用Lazarus编写GUI牛顿法计算三次方程求解程序

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代码,目前使用的人已经很少,但是阅读起来并不困难。除了几个小点要注意。
:= 表示赋值
= 表示判断
函数定义中的形参列表用;隔开
函数调用的时候实参使用,隔开

编译,运行,得到程序,输入参数试验一下:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值