How can we fit hyperbola to data?
102 ビュー (過去 30 日間)
古いコメントを表示
Hi, I have x and y coordinates for my data points. The data is fitted well with exponential fitting. However, I have to fit a hyperbola (a must condition for my results). I am using code,
% fit data k=0.002; % hit and trial
x = 1/xdata; y = k*1/x; p=plot(x,y)
I have attached excel file of my data. Accept my thanks in advance.
0 件のコメント
採用された回答
Star Strider
2017 年 9 月 10 日
Try this:
D = xlsread('data for hyperbola fitting.csv');
D = sortrows(D,1);
x = D(:,1);
y = D(:,2);
hyprb = @(b,x) b(1) + b(2)./(x + b(3)); % Generalised Hyperbola
NRCF = @(b) norm(y - hyprb(b,x)); % Residual Norm Cost Function
B0 = [1; 1; 1];
B = fminsearch(NRCF, B0); % Estimate Parameters
figure(1)
plot(x, y, 'pg')
hold on
plot(x, hyprb(B,x), '-r')
hold off
grid
text(0.7, 0.52, sprintf('y = %.4f %+.4f/(x %+.4f)', B))
‘Accept my thanks in advance.’
The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
7 件のコメント
Rik
2021 年 3 月 13 日
Have you read the Wikipedia article about the R2? It is actually fairly easy to write code that calculates it.
Star Strider
2022 年 9 月 30 日
D = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/87569/data%20for%20hyperbola%20fitting.csv');
D = sortrows(D,1);
x = D(:,1);
y = D(:,2);
hyprb = @(b,x) b(1) + b(2)./(x + b(3)); % Generalised Hyperbola
B0 = [1; 1; 1];
mdl = fitnlm(x, y, hyprb, B0)
B = mdl.Coefficients.Estimate;
figure(1)
plot(x, y, 'pg')
hold on
plot(x, hyprb(B,x), '-r')
hold off
grid
xlabel('x')
ylabel('y')
text(0.7, 0.52, sprintf('$y = %.4f + \\frac{%.4f}{x %+.4f}$', B), 'Interpreter','latex', 'FontSize',12)
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Curve Fitting Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!