fitting with custom equation
6 次查看(过去 30 天)
显示 更早的评论
Hi I am havng troble in fitting the data with costoum eqaution: I/y = x/a + (1-x)/b. with and b are the fitting parametrs. x and y values are as given below.
y = [1.6E5 2.5E5 4.1E5 8E5 1E6 2E6 7E6 2E7]
x = [17 20 27 59 62 81 89 95]
Thank you in advance for your support!
0 个评论
回答(2 个)
John D'Errico
2024-10-12
(I assume you intended to write 1/y, and not I/y, where the variable I is not defined.)
y = [1.6E5 2.5E5 4.1E5 8E5 1E6 2E6 7E6 2E7];
x = [17 20 27 59 62 81 89 95];
Often it is the case, when you cannot fit a model to your data, it means you model does not have the correct shape. This model would be a nice STRAIGHT line, when we plot it as as 1/y versus x.
If we plot 1/y versus x, we would expect to see that behavior in the data. And here, it looks like we have not at all a straight line.
plot(x,1./y,'o')
I might also point out that since your data is scaled to go from 0-100 in x, so the model you wanted to write was probably
I/y = x/a + (100-x)/b.
How can we estimate a and b in that model? Easily enough. We could even use polyfit. There is no need to use a custom model in fit. But you can. Next, note that I'll fit the model as
1/y = x*a + (100-x)*b
and then invert a and b. fit manages to make it converge better if I do so.
mdl = fittype('x*a + (100 - x)*b','indep','x')
fittedmdl = fit(x(:),1./y(:),mdl,'start',[0.001 0.001]) % note that fit wants COLUMN vectors of data.
plot(fittedmdl,x,1./y)
a = 1./fittedmdl.a
b = 1./fittedmdl.b
Again though, I don't think your model fits the data very well.
0 个评论
Star Strider
2024-10-12
This is probably as good as you can hope for —
% I/y = x/a + (1-x)/b
y = [1.6E5 2.5E5 4.1E5 8E5 1E6 2E6 7E6 2E7];
x = [17 20 27 59 62 81 89 95];
figure
plot(x, y)
grid
figure
plot(x, 1./y)
grid
objfcn = @(b,x) x./b(1) + (1 - x)./b(2);
opts = optimset('MaxFunEvals',1E6);
[B,rn] = fminsearch(@(b) norm(1./y - objfcn(b,x)), randn(2,1)+1E10, opts)
figure
plot(x, 1./y, 'pb', 'MarkerSize',12, 'MarkerFaceColor','b', 'DisplayName','Data')
hold on
plot(x, objfcn(B,x), '-r', 'LineWidth',2, 'DisplayName','Regression')
hold off
grid
xlabel('$x$', 'Interpreter','LaTeX', 'FontSize',12)
ylabel('$\frac{1}{y}$', 'Interpreter','LaTeX', 'FontSize',12)
legend('Location','best')
text(35, 5E-6, sprintf('$\\frac{1}{y} = \\frac{x}{%13.5E} + \\frac{1-x}{%13.5E}$',B), 'Interpreter','LaTeX', 'FontSize',14)
.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!