How to fit hyperbola to scatter plot?

9 visualizzazioni (ultimi 30 giorni)
I have attached data I want to fit with hyperbola. As you can see in attached picture, the fit is appearing behind the scattered plot, however, I want it above the scatter plot. Here is the code I am using,
XLSdatafile = 'b46.csv';
XLIM = [0.4 0.24 ]; % beginning/end of yellow selection region
YLIM = [0.12 0.4 ]; % beginning/end of yellow selection region
INITIAL_FIT_GUESS = 0.0200; % initial ecd guess
INITIAL_2FIT_GUESS = 2.35 * INITIAL_FIT_GUESS;
if ~exist('XLSdatafile_loaded','var') || ~strcmp(XLSdatafile, XLSdatafile_loaded)
M = load(XLSdatafile);
XLSdatafile_loaded = XLSdatafile;
X = M(:,1);
Y = M(:,2);
end
f3=figure;
bins=75;
xi = linspace(min(X(:)),max(X(:)),bins);
yi = linspace(min(Y(:)),max(Y(:)),bins);
xr = interp1(xi,1:numel(xi),X,'nearest')';
yr = interp1(yi,1:numel(yi),Y,'nearest')';
scatter2dheat = log10(accumarray([xr' yr'], 1, [bins bins]));
scatter2dheat(isinf(scatter2dheat))=0; % replace inf with 0
pp3 = surf(xi,yi,scatter2dheat');
%this is a nice colormap
colormap([1 1 1;0.949999988079071 1 1;0.899999976158142 1 1;0.850000023841858 1 1;0.800000011920929 1 1;0.75 1
1;0.699999988079071 1 1;0.649999976158142 1 1;0.600000023841858 1 1;0.550000011920929 1 1;0.5 1 1;0.449999988079071 1
1;0.400000005960464 1 1;0.349999994039536 1 1;0.300000011920929 1 1;0.25 1 1;0.200000002980232 1 1;0.150000005960464 1
1;0.100000001490116 1 1;0.0500000007450581 1 1;0 1 1;0.0476190485060215 1 0.952380955219269;0.095238097012043 1
0.904761910438538;0.142857149243355 1 0.857142865657806;0.190476194024086 1 0.809523820877075;0.238095238804817 1
0.761904776096344;0.28571429848671 1 0.714285731315613;0.333333343267441 1 0.666666686534882;0.380952388048172 1
0.61904764175415;0.428571432828903 1 0.571428596973419;0.476190477609634 1 0.523809552192688;0.523809552192688 1
0.476190477609634;0.571428596973419 1 0.428571432828903;0.61904764175415 1 0.380952388048172;0.666666686534882 1
0.333333343267441;0.714285731315613 1 0.28571429848671;0.761904776096344 1 0.238095238804817;0.809523820877075 1
0.190476194024086;0.857142865657806 1 0.142857149243355;0.904761910438538 1 0.095238097012043;0.952380955219269 1
0.0476190485060215;1 1 0;1 0.954545438289642 0;1 0.909090936183929 0;1 0.863636374473572 0;1 0.818181812763214 0;1
0.772727251052856 0;1 0.727272748947144 0;1 0.681818187236786 0;1 0.636363625526428 0;1 0.590909063816071 0;1
0.545454561710358 0;1 0.5 0;1 0.454545468091965 0;1 0.409090906381607 0;1 0.363636374473572 0;1 0.318181812763214 0;1
0.272727280855179 0;1 0.227272734045982 0;1 0.181818187236786 0;1 0.136363640427589 0;1 0.0909090936183929 0;1
0.0454545468091965 0;1 0 0]);
set(gca,'Clim',[max(min(scatter2dheat(:)),1e-3) max(scatter2dheat(:))]);
view(2)
grid off
shading flat
axis([min(X) max(X) min(Y) max(Y)]);
hold on
fit_const = INITIAL_FIT_GUESS;
time = 0.01:0.001:0.3;
current = fit_const./time;
figure(f3)
hold on
p_fit1 = plot(time,current,'r-');
set(gca,'ylim',[0.05,0.5]);
hold on
fit_const = INITIAL_2FIT_GUESS;
time = 0.01:0.001:0.5;
current = fit_const./time;
figure(f3)
hold on
p_fit1 = plot(time,current,'r-');
Hyperbola fit is my only choice. Could anyone suggest a fitting code?

Risposta accettata

Ameer Hamza
Ameer Hamza il 20 Mag 2018
The problem is happening because you are viewing 3D plot from a 2D projection. In order to bring the line to the front, you need to give them z values greater the then the scatter plot points. You can use the following code to get the desired results
XLSdatafile = 'b46.csv';
XLIM = [0.4 0.24 ]; % beginning/end of yellow selection region
YLIM = [0.12 0.4 ]; % beginning/end of yellow selection region
INITIAL_FIT_GUESS = 0.0200; % initial ecd guess
INITIAL_2FIT_GUESS = 2.35 * INITIAL_FIT_GUESS;
if ~exist('XLSdatafile_loaded','var') || ~strcmp(XLSdatafile, XLSdatafile_loaded)
M = load(XLSdatafile);
XLSdatafile_loaded = XLSdatafile;
X = M(:,1);
Y = M(:,2);
end
f3=figure;
bins=75;
xi = linspace(min(X(:)),max(X(:)),bins);
yi = linspace(min(Y(:)),max(Y(:)),bins);
xr = interp1(xi,1:numel(xi),X,'nearest')';
yr = interp1(yi,1:numel(yi),Y,'nearest')';
scatter2dheat = log10(accumarray([xr' yr'], 1, [bins bins]));
scatter2dheat(isinf(scatter2dheat))=0; % replace inf with 0
pp3 = surf(xi,yi,scatter2dheat');
set(gca,'Clim',[max(min(scatter2dheat(:)),1e-3) max(scatter2dheat(:))]);
view(2)
grid off
shading flat
axis([min(X) max(X) min(Y) max(Y)]);
hold on
fit_const = INITIAL_FIT_GUESS;
time = 0.01:0.001:0.3;
current = fit_const./time;
figure(f3)
hold on
p_fit1 = plot3(time,current, max(max(scatter2dheat))*ones(size(time)),'r-', 'LineWidth', 4);
hold on
fit_const = INITIAL_2FIT_GUESS;
time = 0.01:0.001:0.5;
current = fit_const./time;
figure(f3)
hold on
p_fit2 = plot3(time,current, max(max(scatter2dheat))*ones(size(time)), 'r-','LineWidth', 4);
I have removed the colormap to keep code compact. You can add a color map of your choice.
To get the optimal parameters for your hyperbole, you can use lsqcurvefit().

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by