Fitting data in x,y to a known function

16 ビュー (過去 30 日間)
Raúl Alonso Merino
Raúl Alonso Merino 2019 年 12 月 12 日
コメント済み: Star Strider 2019 年 12 月 13 日
Hi everyone. I have a function that is f = 1/(a+b*x) where a and b are the values to obtain and I have some data:
x1 = linspace(1,32,32);
y1 = [0.01 0.02 0.02 0.02 0.02 0.02 0.03 0.03 0.03 0.04 0.04 0.05 0.05 0.06 0.07 0.07 0.08 0.10 0.11 0.14 0.14 0.17 0.17 0.16 0.21 0.31 0.31 2.43 2.43 29.53 29.53 29.53];
I need to fit these two variables x1 and y1 into the function above to obtain a and b. How can I do this?
I'm sorry I'm pretty new in Matlab. Thank you.

採用された回答

Star Strider
Star Strider 2019 年 12 月 12 日
Use the fminsearch function to fit your data:
x1 = linspace(1,32,32);
y1 = [0.01 0.02 0.02 0.02 0.02 0.02 0.03 0.03 0.03 0.04 0.04 0.05 0.05 0.06 0.07 0.07 0.08 0.10 0.11 0.14 0.14 0.17 0.17 0.16 0.21 0.31 0.31 2.43 2.43 29.53 29.53 29.53];
f = @(p,x) 1./(p(1) + p(2).*x);
P = fminsearch(@(p) norm(y1 - f(p,x1)), rand(2,1));
x1v = linspace(min(x1), max(x1));
figure
plot(x1, y1, 'p')
hold on
plot(x1v, f(P,x1v), '-r')
hold off
grid
producing:
P =
0.845464263829100
-0.025490734971736
where ‘P(1)=a’, and ‘P(2)=b’.
The plot appears correct, even though it looks a bit strange.
  2 件のコメント
Raúl Alonso Merino
Raúl Alonso Merino 2019 年 12 月 13 日
Thank you so much. It worked as I wanted!
Star Strider
Star Strider 2019 年 12 月 13 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Nicolas B.
Nicolas B. 2019 年 12 月 12 日
If you have it, I would recommend to use the Curve Fitting Toolbox. You can give it the expected function and it fits the parameters.
Otherwise, the only solution would be to yourself try to fit the function on the samples based on an optimization method but that requires to investigate mathematic fitting.
  1 件のコメント
Raúl Alonso Merino
Raúl Alonso Merino 2019 年 12 月 12 日
I tried and I get a fitted curve, thank you.
General model:
myfit(x) = 1/(a+b*x)
Coefficients (with 95% confidence bounds):
a = 0.9058 (0.4335, 1.378)
b = -0.02738 (-0.04219, -0.01256)
My question now is: is it correct to fit it directly to that function or do I have to fit it to the integral of 1/(a+b*x) which is log(a+b*x)/b + c?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by