fminsearch出错(第191行)fv(:,1)= funfcn(x,varargin {:});

时间:2016-02-04 07:33:25

标签: matlab fminsearch

我想最小化下面的函数,它使用很少的常数(Hz,h,LR,k,T),但最终会出现错误,....请让我离开这个.... 我已经加载了.rpt文件,其中包含代码顶部显示的值.......提前致谢。

3.25E+008   9.55E+002
3.03E+008   1.61E+005
2.77E+008   3.35E+005
2.54E+008   1.98E+006
1.17E+008   1.48E+003
1.13E+008   6.20E+004
1.00E+008   7.96E+005
9.15E+007   2.12E+003

format compact
format long

Hz = 3;
h = 3.99E-10;
LR = 0.1;
k = 8.3145;
T = 297.15;

load sdata_ndata.rpt
count = size(sdata_ndata,1);
xdata = zeros(count,1);% Stress
ydata = zeros(count,1);% Nf

for i=1:count

   xdata(i) = sdata_ndata(i,1);
   ydata(i) = sdata_ndata(i,2);

end

%Function to calculate the sum of residuals for a given p1 and p2
fun = @(p) sum((ydata-(((p(1)*(Hz*h*(1-LR))*xdata)*(exp((p(2))/(k*T))))/(((k*T)^2)*((exp((p(1)*xdata)/(k*T)))-(exp((p(1)*LR*xdata)/(k*T))))))).^2);

%starting guess
pguess = [1.338463e-003;1.234006e+005];

%optimise
[p,fminres] = fminsearch(fun,pguess)

1 个答案:

答案 0 :(得分:1)

错误在于fun的定义。那里,部分

...(k*T))))/(((k*T)^2)...

创建一个矩阵除法,得到(n,n)形状而不是预期的(n,1)形状。因此,减法不再起作用。用元素划分替换除法

...(k*T))))./(((k*T)^2)...