Is there a way to display the curvefit equation on the graph produced without having to write it down myself manually every time? By GUI or command-line, anything is okay. Any hacks, some way to get around this?
Probably easiest to use the fit utility which is the non-graphical equivalent of using curvefit:
% sample data
x=[1:10]';
y = x+randn(10,1)*0.5;
plot(x,y,'o')
pars=fit(x,y,'poly1');
pars contains the result of the fit, which you can overlay on the plot above with
hold on
plot(pars)
If you want to see the values of individual parameters, you can type pars.p1 or pars.p2 (for this example, there may be other parameters "pn" for other models)
To display on the figure, you can do something simple like
xpos=3;
ypos=9;
text(xpos,ypos,{num2str([pars.p1;pars.p2])})
For more info look into the documentation for curvefit or try help curvefit or help fit.
Related
I am asking a short question because I would like to have some opinions on the ideal dimensions that you require/use for a MATLAB figure in a publication.
I am using a direct built-in function of MATLAB in order to generate a figure with appropriate dimensions but I keep being unsatisfied about the result...
My LaTex text document is : width = 470 inches, height = 720 inches.
What do you think is more relevant ?
Thank you for your attention,
I would suggest to you save your figure using the function axis tight and define a function to export all your figures.
I've learned to avoid cropping the figure inside LaTeX to reduce compiling time, and I'm using the function below to export all my graphics. Complete Description. It exports figures like this one: Figure Example
function savefig_tight(h, outfilename)
set(h,'Units','Inches');
pos = get(h,'Position');
set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)]);
print(outfilename ,'-dpdf','-fillpage');
end
I have a set data and I'd like to find the upper and lower peaks of it.
In Matlab, I am trying findpeaks command, but the result is strange.
Here is my simple code:
[pks,locs] = findpeaks(Data);
plot(locs,pks,'or')
and here is the result:
Can someone guide me on how I can find only the very top and very bottom peaks of the data?
You could use the additional input MinPeakProminence to tell Matlab to look only for, yeah well prominent peaks.
[pks,locs] = findpeaks(Data,'MinPeakProminence',4);
plot(locs,pks,'or')
You can play with the parameter and see what works best for you.
I have an array named Area, which contains a set of values.
The histogram of the array looks like this
The bin width is 60 in this case. I'd like to fit two gaussians to the two peaks here (even if it won't be a great fit).
So I used:
options = statset('Display','final');
obj = gmdistribution.fit(area,2,'Options',options);
gausspdf = pdf(obj, xaxis);
A = sum(gausspdf);
gausspdf = gausspdf/A;
But when I try to plot the two fitted Gaussians, the resulting curve looks like this:
I'm quite confused, as there should be two peaks appearing in the plot?
The gmdistribution.fit method fits data according to maximum-likelihood criterion; that is, it tries to find parameters which maximize the likelihood given the data. It will not necessarily fit what you see or expect visually. Still, there is the possibility that the algorithm converged to a "bad" local minimum. You can try and set the initial conditions according to what you want to get, practically 'helping' the algorithm to converge to the desired result. You do this using the Start option to the fit method, which enables you to give it either an initial guess, in which case you should try and estimate the parameters from the histogram, or an initial component index for each data sample. See the documentation for more details.
I think that your peaks are too close and the function can't distinguish them. so maybe you should change the options for gmdistribution or apply a non-linear function to your data first to get more separate peaks in histogram.
I am trying to add Gaussian fit to the histogram in MATLAB, but I do not know how to apply the fit to one particular peak only.
http://postimg.org/image/phms61rdh/ first plot
http://postimg.org/image/sindrn8er/ second plot
I am also posting part of the code I have been using:
data_Rb = (importdata('path\name.txt'));
counts_Rb = data_Rb.data(:,3);
figure
hist(counts_Rb, nbins);
xlim([0 120]);
title('Rubidium');
histfit(counts_Rb,1000);
pd=fitdist(counts_Rb(:),'normal')
x=0:0.001:120;
PDF=pdf(pd,x); %PDF is a vector of y values: it's our fit
PDF=PDF/max(PDF); %nor
y=ylim;
PDF=PDF*y(2);
hold on
plot(x,PDF,'r-','LineWidth',2);
hold off
These two blocks give me two different Gaussians as shown in the first picture. I do not understand why they fit data so badly: is it because of the tail on the RHS?
In the second plot I need to apply Gaussian fit to the last peak only. How should I do it?
And finally, after applying the fit, fit results are outputed onto the screen. Is there a function to save them into an array or so for later use?
Thanks in advance!
Regarding your last question, see How can I access the fitted distribution parameters from the HISTFIT function in Statistiics Toolbox ?
There is a workaround for this isssue by making a copy of the HISTFIT
function and modifying the code to pass out the "pd" variable. One way
to do this is to change the "h" output on the first line of the code
to "varargout" and add the following to the end of the file:
h = [hh; hh1];
argout={h,pd};
if nargout > length(argout)
error('Too many output arguments.');
end
[varargout{1:nargout}]=argout{1:nargout};
why when i use ezplot in for example [1 1.5] interval, a discontinuity will appear in some pieces of lines but when i use a closer interval like [1.3 1.5], the discontinuity will be annihilated?
EZPLOT is a general purpose plotting function that will automatically select a set of points at which to evaluate and plot the function you pass to it. Most of the time, things work fine. But there are some special cases where EZPLOT can have some trouble. It may not render well near discontinuities or points where there are rapid changes in the function (which it may mistake for a discontinuity).
That's the drawback of a function that is designed to be general enough to accept any function you give it: it's hard to make it general enough to handle everything exactly right, so some special edge cases look a little funny. In such cases, you should avoid functions like EZPLOT (which make a lot of choices for you) and plot things yourself by evaluating your functions at points you choose and plotting those points using the PLOT function. Here's a helpful link for this.
The problem is that ezplot() is useful, but not that robust.
A better option for plotting a function without discrete points is fplot(). Check out the documentation for it.
Here is an example of how to use it compared with ezplot():
lowerBound = 0;
upperBound = 1;
%# The ezplot way:
ezplot('y=sin(1/x)',[lowerBound,upperBound,-1,1])
%# The fplot way:
fplot('sin(1/x)',[lowerBound,upperBound])
fplot() will evaluate more points where the function changes more rapidly. Thus discontinuities will still cause problems in the graph if you look closely, but it will try harder to plot them accurately.
To plot a level curve of a function with three variables requires a little more typing:
%# First create a grid where you want the function to be drawn
[x,y]=meshgrid(-2:.01:2);
%# Remember that -2:.01:2 creates a vector with values from -2 to 2
%# in steps of .01
%# Then define your function
z=-3*y./(x.^2+y.^2+1);
%# Now graph the level curve of the function. I chose the level z=0.5:
contour(x,y,z,[0.5])