如何输入值,绘制它们并找到最合适的值

时间:2016-04-18 19:20:38

标签: arrays matlab plot curve-fitting

我需要绘制具有坐标的点

~JC

然后我需要找到一个最合适的曲线到下面的等式:

 (0.8,825.5)
 (1.1,1096)
 (1.3,1293)
 (1.5,1404)

返回y = x*( a1*x + a2 )*( 1 – a12*25 ) a1a2的位置。

这是我到目前为止没有运气的尝试:

a12

我收到此错误:

Fzn = [0.8 1.1 1.3 1.4];
Dy = [825.5 1096 1293 1404];
x = Fzn;
y = Dy;

expr = 'x * (a1 * x + a2) * (1 - a12 * 25)';
ft = fittype(expr, 'independent', 'x', 'dependent','y');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.StartPoint = [1 1 1]; %[a1 a2 a12]
[fitresult, gof] = fit(x, y, ft, opts)

% plot
LFit = feval(fitresult, x);
figure(1);
h = plot( x,LFit,'r-', x, y,'g.');
set(h, 'LineWidth',2)
legend({'Fit Line', 'data points'}, 'Location','SouthEast')
grid on

1 个答案:

答案 0 :(得分:2)

请注意,您有一个自由度,因此会有无限的解决方案

%Put your points in a matrix
X =  [ 0.8 825.5 ; 1.1 1096 ; 1.3 1293 ; 1.5,1404 ]
plot( X(:,1) , X(:,2),'x')
hold on

%Fit the points to the equation
f = fittype('x.*( a*x + b )*( 1 – c*25 )');
[fit1,gof,fitinfo] = fit(X(:,1),X(:,2),f,'StartPoint',[1 1 1]);

%Your result is in fit1