Matlab论坛
直播中

Witanfang

6年用户 5经验值
私信 关注
[问答]

Matlab nlinfit 用自定义函数拟合数据 求自定义函数中多个参数

目标
已有数据:x和y,均为1×3736列向量
已有自定义函数表达式
欲用自定义函数拟合数据

自定义函数描述:
自变量w = 400:1200;
欲求待定参数6个
wp  wl  wt  r  R  d

表达式:R0(w),过长,因此拆分成许多分式来表示,因此R0是一个嵌套的函数
(略过即可)
a = 5.35*(1 - wp.^2./(w.^2 + r.^2) + (wl.^2-wt.^2)*(wt.^2 - w.^2)./((wt.^2 - w.^2).^2 + (w.*R).^2));
b = 5.35*(wp.^2*r./(w.^3 + w*r.^2) + (wl.^2-wt.^2)* w * R./((wt.^2 - w.^2).^2 + (w.*R).^2));
n2 = (0.5*a + 0.5*(a.^2 + b.^2).^0.5).^0.5;
k2 = (-0.5*a + 0.5*(a.^2 + b.^2).^0.5).^0.5;
w1 = 348.3; w2 = 438.9; w3 = 568.2; w4 = 633.6; w5 = 809.6; w6 = 85723.7; w7 = 137621.4;
s1 = 0.33; s2 = 2.788; s3 = 2.98; s4 = 0.145; s5 = 0.0185; s6 = 0.65068713; s7 = 1.4313993;
T1 = 0.011*w1; T2 = 0.006*w2; T3 = 0.012*w3; T4 = 0.010*w4; T5 = 0.157*w5; T6 = 0.00001*w6; T7 = 0.00001*w7;
a1 = s1 * w1.^2 * (w1.^2 - w.^2)./((w1.^2 - w.^2).^2 + (w.* T1).^2); b1 = s1 * w1.^2 * T1 * w ./((w1.^2 - w.^2).^2 + (w.* T1).^2);
a2 = s2 * w2.^2 * (w2.^2 - w.^2)./((w2.^2 - w.^2).^2 + (w.* T2).^2); b2 = s2 * w2.^2 * T2 * w ./((w2.^2 - w.^2).^2 + (w.* T2).^2);
a3 = s3 * w3.^2 * (w3.^2 - w.^2)./((w3.^2 - w.^2).^2 + (w.* T3).^2); b3 = s3 * w3.^2 * T3 * w ./((w3.^2 - w.^2).^2 + (w.* T3).^2);
a4 = s4 * w4.^2 * (w4.^2 - w.^2)./((w4.^2 - w.^2).^2 + (w.* T4).^2); b4 = s4 * w4.^2 * T4 * w ./((w4.^2 - w.^2).^2 + (w.* T4).^2);
a5 = s5 * w5.^2 * (w5.^2 - w.^2)./((w5.^2 - w.^2).^2 + (w.* T5).^2); b5 = s5 * w5.^2 * T5 * w ./((w5.^2 - w.^2).^2 + (w.* T5).^2);
a6 = s6 * w6.^2 * (w6.^2 - w.^2)./((w6.^2 - w.^2).^2 + (w.* T6).^2); b6 = s6 * w6.^2 * T6 * w ./((w6.^2 - w.^2).^2 + (w.* T6).^2);
a7 = s7 * w7.^2 * (w7.^2 - w.^2)./((w7.^2 - w.^2).^2 + (w.* T7).^2); b7 = s7 * w7.^2 * T7 * w ./((w7.^2 - w.^2).^2 + (w.* T7).^2);
a0 = 1 + a1 + a2 + a3 + a4 + a5 + a6 + a7;
b0 = b1 + b2 + b3 + b4 + b5 + b6 + b7;
n3 = (0.5*a0 + 0.5*(a0.^2 + b0.^2).^0.5).^0.5;
k3 = (-0.5*a0 + 0.5*(a0.^2 + b0.^2).^0.5).^0.5;
x = exp(-4 * pi * d * k2.* w);
u = 4 * pi * d * n2.* w;
A = (1 - n2).* (n2 + n3) + (k2 + k3).* k2;
B = (n2 + n3).* k2 - (1 - n2).* (k2 + k3);
C = (1 + n2).* (n2 - n3) - (k2 - k3).* k2;
D = (n2 - n3).* k2 + (1 + n2).* (k2 - k3);
E = (1 + n2).* (n2 + n3) - (k2 + k3).* k2;
F = (n2 + n3).* k2 + (1 + n2).* (k2 + k3);
G = (1 - n2).* (n2 - n3) + (k2 - k3).* k2;
H = (n2 - n3).* k2 - (1 - n2).* (k2 - k3);
X = A + C.* x.* cos(u) - D.* x.* sin(u);
Y = B - C.* x.* sin(u) - D.* x.* cos(u);
Z = E + G.* x.* cos(u) + H.* x.* sin(u);
V = F - H.* x.* cos(u) + G.* x.* sin(u);
R0 =100*((X.*Z -Y.*V).^2 + (X.*V + Y.*Z).^2)./(Z.^2 + V.^2).^2;


我目前的解决方案
用nlinfit函数:
把上述表达式中的待定参数
wp  wl  wt  r  R  d替换为q(1)至q(6)
R0 =inline('100*((X.*Z -Y.*V).^2 + (X.*V + Y.*Z).^2)./(Z.^2 + V.^2).^2','q','w');
q=nlinfit(x1,y,R0,[0 0 0 0 0 0])

报错结果:
错误使用 nlinfit (line 199)
Error evaluating inline model function.

出错 fit (line 39)
q=nlinfit(x1,y,R0,[0 0 0 0 0 0])

原因:
    错误使用 inlineeval (line 14)
    内联表达式出错 ==> 100*((X.*Z -Y.*V).^2 + (X.*V + Y.*Z).^2)./(Z.^2 + V.^2).^2
     未定义函数或变量 'X'。


想问问大佬们如何修改?
或者提出不一样的解决方案?

非常感谢!!

更多回帖

发帖
×
20
完善资料,
赚取积分