我必须通过在0到1和k之间的界限内改变n来最小化e f g和h而没有边界。
% Data is as follows
x = [ 1.4 15.15 49.395 98.8 151.475 184.41 230.51 259.2 ];
y = [ 12.15 21.2125 25.15125 25.3 24.63125 28.8975 29.8725 35.2 ];
% create model function q with parameters p(1) = k and p(2) = n
q = @(p, x) p(1)*x.^(-p(2));
%create the desired error-functions for minimization
e = @(p) sum((y.^2 - q(p, x)).^2); %// minimization function
f = @(p) sum(abs(y - q(p, x))); %// better sum over absolute values
g = @(p) sum(sqrt(abs(q(p, x) - y))); %// better take square roots of absolute values
h = @(p) sum((q(p, x) - y).^2); %// default minimizaton function
p0 = [1, -0.5]; % an initial guess
[p_fit_e, r_e] = fminsearch(e, p0) % Optimize
[p_fit_f, r_f] = fminsearch(f, p0) % Optimize
[p_fit_g, r_g] = fminsearch(g, p0) % Optimize
[p_fit_h, r_h] = fminsearch(h, p0) % Optimize
%之后我有另一组x和y数据,并且与上面相同
x = [ 1.6 17.17 46.54 96.68 147.743 181.67 237.51 251.2 ];
y = [ 11.13 21.22 27.15125 33.3 36.125 43.5689 56.25 65.12 ];