24小时热门版块排行榜    

查看: 1806  |  回复: 9

wtisunday

木虫 (正式写手)

[求助] 非线性方程组matlab拟合

各位大侠,我想搞一下非线性最小二乘拟合,但我的变量x、y分别都是向量,我想用matlab的高斯牛顿法求解,我编的如下:
function ff=fun(vector)
x=vector(1)
y=vector(2)
for i=1:4
    for j=1:4
        A(i,j)=0,B(i,j)=0,b(i)=0,c(i)=0
        for k=1:10
            A(i,j)=A(i,j)+(w(k)*x(j))^2*(w(k)*x(i))^2/(G1(k))^2/(1+(w(k)*...
                x(j))^2)/(1+(w(k)*x(i))^2);
            B(i,j)=B(i,j)+2*y(i)*w(k)^2*x(i)*(w(k)*x(j))^2/(G1(k)^2)/(1+...
                (w(k)*x(i))^2)^2/(1+(w(k)*x(j))^2);
            b(i)=b(i)+(w(k)*x(i))^2/G1(k)/(1+(w(k)*x(i))^2);
            c(i)=c(i)+2*y(i)*x(i)*(w(k))^2/G1(k)/(1+(w(k)*x(i))^2);
        end
    end
end
b=b';
c=c';
ff(1)=A*x-b;
ff(2)=B*y-c;

在赋值x=[1 100 1000 10000],y=[100000,20000,900,20]后出现如下问题:
fsolve(@fun1,x0,y0)

x =

    0.0100


y =

    0.2000


A =

     0


B =

     0


b =

     0


c =

     0

??? Undefined function or method 'w' for input arguments of
type 'double'.

Error in ==> fun1 at 8
            A(i,j)=A(i,j)+(w(k)*x(j))^2*(w(k)*x(i))^2/(G1(k))^2/(1+(w(k)*...
            
Error in ==> fsolve at 254
            fuser = feval(funfcn{3},x,varargin{:});

Caused by:
    Failure in initial user-supplied objective function
    evaluation. FSOLVE cannot continue.

请大家帮帮忙了!万分感谢
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

w估计是权重,你没有定义,怎么运行呢
The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
2楼2011-11-24 11:07:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wtisunday

木虫 (正式写手)

还请版主给出详细的解答啊,谢谢了
3楼2011-11-24 11:09:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

引用回帖:
3楼: Originally posted by wtisunday at 2011-11-24 11:09:43:
还请版主给出详细的解答啊,谢谢了

能把你的问题详细描述下吗,我好明白你要做什么,要优化的是什么?
The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
4楼2011-11-24 11:12:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wtisunday

木虫 (正式写手)

我是要优化求解x,y的值,也就是两组向量值,式子中的w、G1、G2都是实验测量量,为已知。程序中的i和j就是我想求的向量中的元素个数。不知道这么说可以吗
5楼2011-11-24 11:16:59
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

麻烦你给出w G1 G2的数值,要不然我只随便取些做个范例帮你算一下了
The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
6楼2011-11-24 11:24:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wtisunday

木虫 (正式写手)

w:
0.00100000000000000
0.0200000000000000
0.130000000000000
0.910000000000000
8.70000000000000
76
1001
4301
20032
100000

G1:
1000
2034
3000
4700
6002
10032
50345
109820
620000
1200000

G2:
6000
7200
9000
11000
13000
23000
38000
70012
209903
632098
7楼2011-11-24 11:29:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

wtisunday(金币+20): 呵呵,整明白了,主要是fsolve只能解决未知量为向量的 2011-11-24 17:24:29
CODE:
function AA
x=[1 100 1000 10000 100000,20000,900,20];
fsolve(@fun,x)

function ff=fun(x)
w=[0.00100 0.02000 0.1300 0.910000 8.7000 76 1001 4301 20032 100000]';
G1=[1000 2034 3000 4700 6002 10032 50345 109820 620000 1200000]';
G2=[6000 7200 9000 11000 13000 23000 38000 70012 209903 632098]';
for i=5:8
    y(mod(i,5)+1)=x(i);
end
   
for i=1:4
    for j=1:4
        A(i,j)=0,B(i,j)=0,b(i)=0,c(i)=0
        for k=1:10
            A(i,j)=A(i,j)+(w(k)*x(j))^2*(w(k)*x(i))^2/(G1(k))^2/(1+(w(k)*...
                x(j))^2)/(1+(w(k)*x(i))^2);
            B(i,j)=B(i,j)+2*y(i)*w(k)^2*x(i)*(w(k)*x(j))^2/(G1(k)^2)/(1+...
                (w(k)*x(i))^2)^2/(1+(w(k)*x(j))^2);
            b(i)=b(i)+(w(k)*x(i))^2/G1(k)/(1+(w(k)*x(i))^2);
            c(i)=c(i)+2*y(i)*x(i)*(w(k))^2/G1(k)/(1+(w(k)*x(i))^2);
        end
    end
end
b=b';
c=c';
X=x(1:4)';
Y=x(5:8)';
ff1=A*X-b;
ff2=B*Y-c;
ff=[ff1;ff2];

The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
8楼2011-11-24 11:59:12
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wtisunday

木虫 (正式写手)

你给的程序还是无法运行啊
9楼2011-11-24 14:36:33
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

直接复制代码 到editor中保存为m文件,然后点run

结果是


No solution found.

fsolve stopped because the relative size of the current step is less than the
default value of the step size tolerance squared, but the vector of function values
is not near zero as measured by the default value of the function tolerance.




ans =

  1.0e+004 *

    0.0000    0.0100    0.1000    1.0000   10.0000    2.0000    0.0900    0.0020

你的初值不太合适
The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
10楼2011-11-24 16:31:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 wtisunday 的主题更新
信息提示
请填处理意见