fitting with custom equation

6 views (last 30 days)
Somnath Kale
Somnath Kale on 12 Oct 2024
Answered: John D'Errico on 12 Oct 2024
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!

Answers (2)

John D'Errico
John D'Errico on 12 Oct 2024
(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')
mdl =
General model: mdl(a,b,x) = x*a + (100 - x)*b
fittedmdl = fit(x(:),1./y(:),mdl,'start',[0.001 0.001]) % note that fit wants COLUMN vectors of data.
fittedmdl =
General model: fittedmdl(x) = x*a + (100 - x)*b Coefficients (with 95% confidence bounds): a = -7.796e-09 (-2.357e-08, 7.975e-09) b = 5.469e-08 (3.57e-08, 7.367e-08)
plot(fittedmdl,x,1./y)
a = 1./fittedmdl.a
a = -1.2826e+08
b = 1./fittedmdl.b
b = 1.8286e+07
Again though, I don't think your model fits the data very well.

Star Strider
Star Strider on 12 Oct 2024
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)
B = 2×1
1.0e+05 * 1.8497 1.8286
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rn = 2.4935e-06
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)
.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!