| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1822 人关注过本帖
标题:如何将matlab中的polyfit function 转换成 VBA code
只看楼主 加入收藏
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
 问题点数:0 回复次数:9 
如何将matlab中的polyfit function 转换成 VBA code
如题;

或者 有没有大虾能将以下的matlab语言转成VBA的, 一样的效果
function p=mypolyfit(x,y,n)
A=[];
for i=0:n
    A=[A x'.^i];
end
p=inv(A'*A)*A'*y';
p=fliplr(p');






以下是matlab 中 polyfit 的帮助文档(有一些数学符号复制不过来,所以不是非常完整,但大体在那了, 对造成的不便抱歉--其实大家如果能把那个matlab code转成VBA, 就是polyfit function 了, 感谢!!)

polyfitPolynomial curve fitting

      Syntax
   p = polyfit(x,y,n)[p,S] = polyfit(x,y,n)[p,S,mu] = polyfit(x,y,n)Descriptionp = polyfit(x,y,n) finds the coefficients
of a polynomial p(x) of degree n that
fits the data, p(x(i)) to y(i), in a
least squares sense. The result p is a row vector of length n+1 containing
the polynomial coefficients in descending powers
[p,S] = polyfit(x,y,n) returns
the polynomial coefficients p and a structure S for
use with polyval to obtain error estimates or predictions.
Structure S contains fields R, df,
and normr, for the triangular factor from a QR decomposition
of the Vandermonde matrix of X, the degrees of freedom,
and the norm of the residuals, respectively. If the data Y are
random, an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df,
where Rinv is the inverse of R. If the
errors in the data y are independent normal with constant
variance, polyval produces error bounds that contain
at least 50% of the predictions.[p,S,mu] = polyfit(x,y,n) finds
the coefficients of a polynomial in
where
and
. mu is
the two-element vector
. This centering
and scaling transformation improves the numerical properties of both the polynomial
and the fitting algorithm.ExamplesThis example involves fitting the error function, erf(x),
by a polynomial in x. This is a risky project because erf(x) is
a bounded function, while polynomials are unbounded, so the fit might not
be very good. First generate a vector of x points, equally spaced
in the interval
; then evaluate erf(x) at
those points. x = (0: 0.1: 2.5)';
y = erf(x); The coefficients in the approximating polynomial of degree 6 are p = polyfit(x,y,6)

p =

0.0084  -0.0983   0.4217   -0.7435  0.1471   1.1064  0.0004There are seven coefficients and the polynomial is
To see how good the fit is, evaluate the polynomial at the data points
withf = polyval(p,x); A table showing the data, fit, and error is  table = [x y f y-f]

table =

    0          0          0.0004    -0.0004
    0.1000     0.1125     0.1119     0.0006
    0.2000     0.2227     0.2223     0.0004
    0.3000     0.3286     0.3287    -0.0001
    0.4000     0.4284     0.4288    -0.0004
    ...
    2.1000     0.9970     0.9969     0.0001
    2.2000     0.9981     0.9982    -0.0001
    2.3000     0.9989     0.9991    -0.0003
    2.4000     0.9993     0.9995    -0.0002
    2.5000     0.9996     0.9994     0.0002So, on this interval, the fit is good to between three and four digits.
Beyond this interval the graph shows that the polynomial behavior takes over
and the approximation quickly deteriorates. x = (0: 0.1: 5)';
y = erf(x);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
axis([0  5  0  2])
AlgorithmThe polyfit M-file forms the Vandermonde matrix,
, whose elements are powers
of
.
It then uses the backslash operator, \,
to solve the least squares problem
You can modify the M-file to use other functions of
as the basis functions.


非常感谢
搜索更多相关主题的帖子: matlab polyfit function VBA 数学 
2008-04-02 19:05
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
请问有强人知道吗?会员的水平决定论坛的质量, 这个论坛发展的挺不错的啊,应该会有人能解答这个问题吧。。。:)
2008-04-03 13:01
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
这并不是一个非常难得问题啊, 我是水平有限,对VBA不是很熟悉,但是从matlab来看,这个程序并不复杂啊。。。。
2008-04-03 13:04
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
程序可能不复杂,但是鸟文很复杂,所以我没看

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-04-03 15:10
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
哈哈 谢谢版主的回复啊

不好意思 那个帮助文档就是那样的

其实不用看那个帮助文档 我的错误 贴出来多此一举 混淆视听

您看一下以下这个matlab code您可以转换成 VBA 吗?
function p=mypolyfit(x,y,n)
A=[];
for i=0:n
    A=[A x'.^i];
end
p=inv(A'*A)*A'*y';
p=fliplr(p');

就是线性代数里的polyfit 就是给了固定的几个点 根据这些点 我需要找到合适的系数去赋予一个多项式,使得这个多项式能最好的模拟这些点

譬如说我发现图形为一条直线的多项式能最好fit固定点 也就是说这些点到这条直线的距离最小 有点像线性回归

非常感谢
2008-04-03 22:07
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
哦,求多项式拟和系数咯,这个我以前找到过一个VB写的,等下周一找找看

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-04-04 16:18
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
多谢了

我现在在做一个小project 纯兴趣的  因为这个步骤卡在这里了

如果您有空 能稍微早一点找一下, 那最好!! 但是到星期一也没关系, 没什么时间限制, 但是我希望早一点做出来。 您也知道, 如果编个不是很小的程序编一半放个三四天再拾起来比较麻烦。

不管怎样, 谢谢您了! 等您的回复!

[[it] 本帖最后由 acting 于 2008-4-4 22:15 编辑 [/it]]
2008-04-04 19:39
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
明天就星期一了 把帖子顶上去 谢谢斑竹的帮助:)
2008-04-06 18:51
acting
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
永远的极光 斑竹

也许你的计算机水平很高, 但食言可不是件好事情啊。
2008-04-07 18:31
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
不好意思,真的很不好意思,事情一忙给忘记了,对不起

下面是vb写的多项式拟合函数,从网上找的,我改了一些,已经测试过能用的
'*************************************************************************
'**函 数 名:Iapcir
'**输    入:X()(Double)                -实型一维数组,长度为 n 。存放给定 n 个数据点的 X 坐标。
'**        :Y()(Double)                -实型一维数组,长度为 n 。存放给定 n 个数据点的 Y 坐标。
'**        :ByVal n(Integer)           -变量。给定数据点的个数。
'**        :ByRef a()(Double)          -实型一维数组,长度为 m 。返回 m-1 次拟合多项式的 m 个系数。
'**        :ByVal m(Integer)           -拟合多项式的项数,即拟合多项式的最高次数为 m-1。
'**                                    要求 m<=n 且m<=20。若 m>n 或 m>20 ,则本函数自动按 m=min{n,20} 处理。?
'**        :ByRef rdblAverageX(Double) -返回给定n个数据点的 X 坐标的平均值
'**        :ByRef dt()(Double)         -实型一维数组,长度为 3。其中:
'**                                    dt(0) 返回拟合多项式与数据点误差的平方和;
'**                                    dt(1) 返回拟合多项式与数据点误差的绝对值之和;
'**                                    dt(2) 返回拟合多项式与数据点误差绝对值的最大值。
'**输    出:无
'**功能描述:
'**全局变量:
'**调用模块:
'**作    者:
'**日    期:2008-01-10 09:34:44
'**修 改 人:
'**日    期:
'**版    本:V1.0.0
'*************************************************************************
Public Sub Iapcir(X() As Double, Y() As Double, ByVal n As Integer, _
ByRef A() As Double, ByVal m As Integer, ByRef rdblAverageX As Double, _
ByRef dt() As Double)

Dim i As Integer, j As Integer, K As Integer
Dim Z As Double, P As Double, C As Double, g As Double, Q As Double, D1 As Double, D2 As Double
Dim S(19) As Double, T(19) As Double, B(19) As Double

For i = 0 To m - 1
    A(i) = 0
Next i

If m > n Then m = n
If m > 20 Then m = 20

Z = 0#

For i = 0 To n - 1
    rdblAverageX = rdblAverageX + X(i)
    Z = Z + X(i) / (1# * n)
Next i
rdblAverageX = rdblAverageX / n

B(0) = 1#
D1 = 1# * n
P = 0#
C = 0#

For i = 0 To n - 1
    P = P + (X(i) - Z)
    C = C + Y(i)
Next i

C = C / D1
P = P / D1
A(0) = C * B(0)

If m > 1 Then
    T(1) = 1#
    T(0) = (-1) * P
    D2 = 0#
    C = 0#
    g = 0#
    For i = 0 To n - 1
        Q = X(i) - Z - P
        D2 = D2 + Q * Q
        C = C + Y(i) * Q
        g = g + (X(i) - Z) * Q * Q
    Next i
    C = C / D2
    P = g / D2
    Q = D2 / D1
    D1 = D2
    A(1) = C * T(1)
    A(0) = C * T(0) + A(0)
End If
  
For j = 2 To m - 1
    S(j) = T(j - 1)
    S(j - 1) = (-1) * P * T(j - 1) + T(j - 2)
    If j >= 3 Then
        For K = j - 2 To 1 Step -1
            S(K) = (-1) * P * T(K) + T(K - 1) - Q * B(K)
        Next K
    End If
    S(0) = (-1) * P * T(0) - Q * B(0)
    D2 = 0#
    C = 0#
    g = 0#
    For i = 0 To n - 1
        Q = S(j)
        For K = j - 1 To 0 Step -1
            Q = Q * (X(i) - Z) + S(K)
        Next K
        D2 = D2 + Q * Q
        C = C + Y(i) * Q
        g = g + (X(i) - Z) * Q * Q
    Next i
    C = C / D2
    P = g / D2
    Q = D2 / D1
    D1 = D2
    A(j) = C * S(j)
    T(j) = S(j)
    For K = j - 1 To 0 Step -1
        A(K) = C * S(K) + A(K)
        B(K) = T(K)
        T(K) = S(K)
    Next K
Next j

dt(0) = 0#
dt(1) = 0#
dt(2) = 0#

For i = 0 To n - 1
    Q = A(m - 1)
    For K = m - 2 To 0 Step -1
        Q = A(K) + Q * (X(i) - Z)
    Next K
    P = Q - Y(i)
    If Abs(P) > dt(2) Then
        dt(2) = Abs(P)
    End If
    dt(0) = dt(0) + P * P
    dt(1) = dt(1) + Abs(P)
Next i


Dim aa As Double, bb As Double, cc As Double, dd As Double, Ave As Double
Ave = rdblAverageX
aa = A(3)
bb = A(2) - 3 * Ave * A(3)
cc = 3 * Ave * Ave * A(3) - 2 * A(2) * Ave + A(1)
dd = A(2) * Ave * Ave - A(3) * Ave * Ave * Ave - A(1) * Ave + A(0)
A(3) = aa
A(2) = bb
A(1) = cc
A(0) = dd
End Sub


vba和vb差不多,应该可以直接用的,再次对我的疏忽表示歉意

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-04-10 08:37
快速回复:如何将matlab中的polyfit function 转换成 VBA code
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064562 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved