重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

vb.net最小二乘法 vb最小二乘法拟合直线算法

VB如何编写这两道题的最小二乘法?就大神解救!!!

首先表示一下同情。不过,这学校真这么过分吗?什么都不教就考?

福海网站建设公司创新互联,福海网站设计制作,有大型网站制作公司丰富经验。已为福海上千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的福海做网站的公司定做!

最小二乘法的原理还是要明白一点的:

在已知有限数量的结果数据对时,推导出最可能的函数匹配。

而事实上,在实际应用过程中大多数情况是先预设了一个可能的函数模型的。

比如说有10对数据,你可以假定它是一个直线模型,也可以假定它是一个弧度较大的抛物线。

由此产生的结果函数自然是天差地远,完全不一致的。

所以,用计算机实现最小二乘法基本都是根据猜测的函数模型而编写相应程序。没通用的!

了解了上面这些,你再看考题就很清楚了:要求你写出两段程序,分别对应两个猜测函数。

这种程度的要求,不应该交给新手去做的。所以我开头说你学校过分。

这个题目太麻烦,我没时间把它实现出来。给你几个链接供你参考吧:

它们都不是这个问题的解答,因为对应的猜测函数是不一样的,提供个思路吧。

VB直线最小二乘法拟合

'新建窗体,添加text1,command1,picture1

Private Sub Command1_Click()

If Text1.Text = "" Then Exit Sub

Dim x() As Single, y() As Single, cnt As Integer

Dim xmax As Single, xmin As Single, ymax As Single, ymin As Single

Dim p() As String, z() As String

Dim xyh As Single, xh As Single, yh As Single, xph As Single, k As Single, b As Single

p = Split(Text1.Text, "/")

For i = 0 To UBound(p)

If p(i) "" Then

z = Split(p(i), "*")

If UBound(z) = 1 Then

If IsNumeric(z(0)) And IsNumeric(z(1)) Then

If cnt = 0 Then xmax = z(0): xmin = z(0): ymax = z(1): ymin = z(1)

If xmax z(0) Then xmax = z(0)

If xmin z(0) Then xmin = z(0)

If ymax z(1) Then ymax = z(1)

If ymin z(1) Then ymin = z(1)

xyh = xyh + z(0) * z(1): xh = xh + z(0): yh = yh + z(1): xph = xph + z(0) ^ 2

ReDim Preserve x(cnt), y(cnt)

x(cnt) = z(0): y(cnt) = z(1): cnt = cnt + 1

End If

End If

End If

Next

Picture1.Cls

Picture1.DrawWidth = 1

If xmax = xmin And ymax = ymin Then

MsgBox "单点无法拟合"

ElseIf xmax = xmin Then

Picture1.Scale (xmin * 0.5, ymax + 0.2 * (ymax - ymin))-(xmin * 1.5, ymin - 0.2 * (ymax - ymin))

zuobiaozhou xmin * 0.5, ymax + 0.2 * (ymax - ymin), xmin * 1.5, ymin - 0.2 * (ymax - ymin)

Picture1.Line (xmax, ymax + 0.2 * (ymax - ymin))-(xmax, ymin - 0.2 * (ymax - ymin)), vbBlue

ElseIf ymax = ymin Then

Picture1.Scale (xmin - 0.2 * (xmax - xmin), ymax * 1.5)-(xmax + 0.2 * (xmax - xmin), ymin * 0.5)

zuobiaozhou xmin - 0.2 * (xmax - xmin), ymax * 1.5, xmax + 0.2 * (xmax - xmin), ymin * 0.5

Picture1.Line (xmin - 0.2 * (xmax - xmin), ymax)-(xmax + 0.2 * (xmax - xmin), ymax), vbBlue

Else

Picture1.Scale (xmin - 0.2 * (xmax - xmin), ymax + 0.2 * (ymax - ymin))-(xmax + 0.2 * (xmax - xmin), ymin - 0.2 * (ymax - ymin))

zuobiaozhou xmin - 0.2 * (xmax - xmin), ymax + 0.2 * (ymax - ymin), xmax + 0.2 * (xmax - xmin), ymin - 0.2 * (ymax - ymin)

k = (xyh - (xh * yh) / cnt) / (xph - xh ^ 2 / cnt)

b = yh / cnt - k * xh / cnt

Picture1.Line (xmin - 0.2 * (xmax - xmin), k * (xmin - 0.2 * (xmax - xmin)) + b)-(xmax + 0.2 * (xmax - xmin), k * (xmax + 0.2 * (xmax - xmin)) + b), vbBlue

End If

Picture1.DrawWidth = 5

For i = 0 To cnt - 1

Picture1.PSet (x(i), y(i)), vbRed

Next

Text1.SetFocus

End Sub

Private Sub Form_Activate()

Text1.SetFocus

End Sub

Private Sub Form_Load()

Text1.Text = ""

Text1.ToolTipText = "横纵坐标间以乘号*分隔,各点间以除号/分隔。例如:100*100/200*200"

Command1.Caption = "绘图"

Picture1.AutoRedraw = True

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 8 Or KeyAscii = 42 Or KeyAscii = 45 Or KeyAscii = 46 Or KeyAscii = 47) Then KeyAscii = 0

End Sub

Function zuobiaozhou(ByVal x1 As Single, y1 As Single, x2 As Single, y2 As Single)

For i = x1 + (x2 - x1) / 5 To x2 Step (x2 - x1) / 5

Picture1.Line (i, y2 + 100 * (y1 - y2) / Picture1.Height)-(i, y2)

Picture1.CurrentX = i - 250 * (x2 - x1) / Picture1.Width

Picture1.CurrentY = y2 + 350 * (y1 - y2) / Picture1.Height

Picture1.Print i

Next

For i = y2 + (y1 - y2) / 5 To y1 Step (y1 - y2) / 5

Picture1.Line (x1, i)-(x1 + 100 * (x2 - x1) / Picture1.Width, i)

Picture1.CurrentX = x1 + 150 * (x2 - x1) / Picture1.Width

Picture1.CurrentY = i + 80 * (y1 - y2) / Picture1.Height

Picture1.Print i

Next

End Function

VB 最小二乘法线性拟合

稍等好吗?

好了,但愿没有耽误你!代码如下(注意不用任何控件即可):

Private Sub Form_click()

Dim N As Integer, U() As Double, I As Integer

Dim A As Single, B As Single

Form1.Cls

Print "数据:";

N = Val(InputBox("原始数据个数", "输入", 7))

If N = 1 Then Exit Sub

ReDim U(4, N)

Print "共"; N; "组:"

For I = 1 To N

U(1, I) = Val(InputBox("自变量 X 的值:", "第一组", I))

U(2, I) = Val(InputBox("因变量 Y 的值:", "第一组", I * 5))

U(3, I) = U(1, I) * U(2, I)

U(4, I) = U(1, I) ^ 2

Print "x("; I; ") = "; U(1, I); " y("; I; ") = "; U(2, I)

U(1, 0) = U(1, 0) + U(1, I)

U(2, 0) = U(2, 0) + U(2, I)

U(3, 0) = U(3, 0) + U(3, I)

U(4, 0) = U(4, 0) + U(4, I)

Next I

U(1, 0) = U(1, 0) / N

U(2, 0) = U(2, 0) / N

B = (U(3, 0) - N * U(1, 0) * U(2, 0)) / (U(4, 0) - N * U(1, 0) ^ 2)

A = U(2, 0) - B * U(1, 0)

Print "拟合公式为:"

Print " y = "; A;

If B = 0 Then Print " + ";

Print B; "x"

End Sub

Private Sub Form_Load()

Form1.AutoRedraw = True

Form1.Caption = "最小二乘法拟合程序"

Print

Print "本程序执行的顺序为:"

Print "1、输入数据数量;"

Print "3、逐个输入各组数据;"

Print "2、输出拟合公式。"

Print

Print "特别说明:本程序不用任何控件!只要把窗口拉的足够大就行了。"

Print

Form1.Print "单击窗体开始..."

End Sub

已经运行过。


本文题目:vb.net最小二乘法 vb最小二乘法拟合直线算法
标题网址:http://cqcxhl.cn/article/doiheee.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP