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.

 Akzeptierte Antwort

Star Strider
Star Strider am 10 Sep. 2017

6 Stimmen

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 Kommentare

ishita agrawal
ishita agrawal am 11 Sep. 2017
Thank you so much.
Star Strider
Star Strider am 11 Sep. 2017
As always, my pleasure!
lakshmikanth ayyadevara
lakshmikanth ayyadevara am 3 Mär. 2021
code is really nice
Rik
Rik am 3 Mär. 2021
@lakshmikanth ayyadevara Flags are not personal bookmarks, they are to alert staff and members with editing privileges. I have removed your flag.
Raj Lakhani
Raj Lakhani am 12 Mär. 2021
@Star Strider can you also show how to evaluate the R(square) after this? thanks in advance
Rik
Rik am 13 Mär. 2021
Have you read the Wikipedia article about the R2? It is actually fairly easy to write code that calculates it.
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)
mdl =
Nonlinear regression model: y ~ b1 + b2/(x + b3) Estimated Coefficients: Estimate SE tStat pValue __________ ________ ________ __________ b1 0.094879 0.015709 6.0399 2.4472e-09 b2 0.14875 0.017335 8.5807 5.5591e-17 b3 -0.0097497 0.029435 -0.33123 0.74057 Number of observations: 739, Error degrees of freedom: 736 Root Mean Squared Error: 0.0502 R-Squared: 0.772, Adjusted R-Squared 0.772 F-statistic vs. constant model: 1.25e+03, p-value = 2.94e-237
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)
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by