MATLAB Answers

Data Curve Fitting Not Fitting Third Point?

1 view (last 30 days)
Kelly McGuire
Kelly McGuire on 20 Feb 2019
Answered: Kelly McGuire on 20 Feb 2019
Any ideas why it appears the third data point is not being fit?
xdata = [1;10;100];
ydata = [0;0;0.3];
fun = @(x,xdata)(1./(1+(x(1)./xdata).^x(2)));
lb = [1,1];
ub = [1000,5];
x0 = [50,2];
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
figure
hold on
plot(xdata,ydata,'ko');
xlim([0 200]);
xlabel('Concentration','FontWeight','bold');
ylim([0 1]);
ylabel('Average Mortality','FontWeight','bold');
box off
y = 0:200;
plot(1./(1+(x(1)./y)).^x(2))
xlim([0 200])
ylim([0 1])

  0 Comments

Sign in to comment.

Answers (1)

Kelly McGuire
Kelly McGuire on 20 Feb 2019
Fixed the problem. In the second plot, my x(2) was in the wrong term. It should have been brought into the left parthensis. Here is the correct code:
xdata = [1;10;100];
ydata = [0;0;0.3];
fun = @(x,xdata)(1./(1+(x(1)./xdata).^x(2)));
lb = [170,1];
ub = [200,2];
x0 = [180,1.6];
%x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
opts = optimset('MaxFunEvals',1E+4, 'MaxIter',1E+4 );
x = fminsearch(@(x) norm(ydata - fun(x,xdata)), x0, opts)
figure
hold on
plot(xdata,ydata,'ko');
xlim([0 200]);
xlabel('Concentration','FontWeight','bold');
ylim([0 1]);
ylabel('Average Mortality','FontWeight','bold');
box off
y = 0:600;
plot(1./(1+(x(1)./y).^x(2)))
xlim([0 200])
ylim([0 1])

  0 Comments

Sign in to comment.

Sign in to answer this question.