You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how can curve fitting
2 views (last 30 days)
Show older comments
hi...i have some scatter points in x-y coordinate.how can curve fitting without using of cftool?i want do it by gussian fitt but in gussian code there are x and y.i domt know how obtain them!
% f = fit(x,y,'gauss8'); % plot(f,x,y); % coeff=fit(x,y,ft);
G is sample matrix with 106*47 in size and meanValue is mean of columns in matrix.result matrix is 1*47 that it is scatter points.
meanValue = mean(G)
thank you
1 Comment
Image Analyst
on 17 May 2014
A screenshot of your data, or attaching your data file, would help us envision what you're talking about.
Accepted Answer
Star Strider
on 17 May 2014
Here’s my version of Gauss1 using fminsearch:
% % Single Gaussian Regression
% % Generate x- and y- data:
% x = linspace(0,10); % Generate x-data
% Normal distribution — b(1) = mean, b(2) = std
N = @(b,x) exp(-((x-b(1)).^2)./(2*b(2)^2)) ./ (b(2)*sqrt(2*pi));
% b = [5; 1];
% d1 = N(b,x); % Generate smooth y-data
% d2 = d1 + 0.05*(rand(size(d1))-.5); % Add noise
% % ————— Assign your variables to x and y here —————
x = your independent variable
y = your dependent variable
% % Use ‘fminsearch’ to do regression:
y = d2;
OLS = @(b) sum((N(b,x) - y).^2); % Ordinary Least Squares cost function
opts4 = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, rand(2,1))
Nfit = N(B,x); % Calculate estimated fit
The variables you need to have in your workspace are x and y (or assign them to x and y) to use this without modifying the code. The single ‘%’ commented lines are there for illustration. (Uncomment them if you want to see how it works. Remove them if you don’t need them.)
27 Comments
Star Strider
on 17 May 2014
I downloaded and fit the data in ‘meanValue.fig’. I changed my code to a 2-Gaussian model, and got what I consider a decent fit.
You will have to assign your x and y variables to the ones I call x and y in my code, just after my comment:
% % ————— Assign your variables to x and y here —————
after that, it should run without problems.
Initial choice of parameters, especially b(2) and b(5) in N2, is important in situations like these, since it’s possible for both Gaussian functions to fit to same peak, ignoring the other one.
I got for the fitted parameter set:
B =
584.9265
28.8754
4.3951
312.9486
38.4016
2.6977
fitting to your data with this code:
% % Double Gaussian Regression
N = @(b,x) b(1).*exp(-((x-b(2)).^2)./(2*b(3)^2)) ./ (b(3)*sqrt(2*pi));
N2 = @(b,x) b(1).*exp(-((x-b(2)).^2)./(2*b(3)^2)) ./ (b(3)*sqrt(2*pi)) + b(4).*exp(-((x-b(5)).^2)./(2*b(6)^2)) ./ (b(6)*sqrt(2*pi));
% % ————— Assign your variables to x and y here —————
x = ...;
y = ...;
% % Use ‘fminsearch’ to do regression:
OLS = @(b) sum((N2(b,x) - y).^2); % Ordinary Least Squares cost function
opts4 = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, [50 25 5 50 40 5]')
N2fit = N2(B,x); % Calculate estimated fit
Nfit(1,:) = N(B(1:3),x);
Nfit(2,:) = N(B(4:6),x);
figure(1)
plot(x, y, '+b')
hold on
plot(x, N2fit, '-r', 'LineWidth',1.5)
plot(x, Nfit(1,:), '-g')
plot(x, Nfit(2,:), '-g')
hold off
grid
text(5, 55, sprintf('Centre #1 = %.2f, \\sigma #1 = %.2f\nCentre #2 = %.2f, \\sigma #2 = %.2f', B(2:3), B(5:6)), 'FontName','Consolas', 'FontSize',7)
producing this plot:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178777/image.png)
fereshte
on 18 May 2014
Edited: fereshte
on 18 May 2014
dear Star Strider thanks a lot....great.very good.but i dont run it.error:
??? Error: File: Untitle.m Line: 6 Column: 3 The expression to the left of the equals sign is not a valid target for an assignment.
some Question?
1.Which line in this code read the my input image?
2.Is this code for any other data?
3.b Parameter must be calculated for each image?
4.how obtain coefficients by this code?they are important for my work.
Star Strider
on 18 May 2014
I omitted the lines that read your image because I thought you had access to those data without them. If you want to use that and other images (nothing wrong with that), insert these lines before the comment line:
ssf = openfig('meanValue.fig')
hf2 = get(gca, 'Children');
hf3xc = get(hf2, 'XData');
hf3yc = get(hf2, 'YData');
% % Double Gaussian Regression
then:
% % ————— Assign your variables to x and y here —————
x = hf3xc;
y = hf3yc;
If you want to use other figures as sources of data, simply insert their names in the openfig line. It should run without problems, although you might need to change the values for b(2) and b(5) as I mentioned previously, to initially approximate the centres of the peaks of your data.
I’ll re-post the entirety of my code if necessary, but these are the only changes you need to make it work. The code should work for all sets of x and y data that you want to give it. Unless you use your ‘.fig’ files as input (in which situation you only need to change their names in the openfig statement), you simply have to use the code in my previous comment, plug in the names of your variables in place of hf3xc and hf3yc in my code, and adjust two of the initial parameter estimates. The program should then successfully estimate all the parameters.
Star Strider
on 18 May 2014
My pleasure!
The sincerest and most valuable expression of thanks in MATLAB Answers is to Accept the Answer that is closest to answering your Question and solving your problem.
This code will calculate b(2), b(5) for all images of this type. You will have to give initial estimates for b(2), b(5) unless they are in approximately the same locations as they were in the figure you posted. In this situation, it is possible to create a function file out of my code, but I need to know more about the other images you want to analyse with it, and if you only want to analyse your images with it.
fereshte
on 18 May 2014
my database are footprint images of different people.I will run examples of them and compare with meanValue curve if they arent in the same locations,will post here.I've no word to express my seep gratitude.thank you for your time
Star Strider
on 18 May 2014
My pleasure!
Do you have the Signal Processing Toolbox?
Star Strider
on 18 May 2014
I asked because it has a findpeaks function that would help locate the peaks for you to estimate subsequently. You can use the max function to find the highest peak and its index (to define the x-coordinate of the peak).
If the first peak is always the highest, set that initial estimate as b(2), and define b(5)=b(2)+10 for its initial estimate.
Simply an idea to make your analysis easier!
Star Strider
on 19 May 2014
My pleasure!
fereshte
on 19 May 2014
in cftool, gauss 8 give me best fit of curve. this program generate a code for gauss 8 but i dont run it.i need to code to run for every image of right and left footprint.i dont write in command window cftool for every image and run it.its Boring.
Star Strider
on 19 May 2014
I don’t have the curve fitting toolbox (I don’t need it with the Statistics and Optimization Toolboxes) so I can’t help you with it.
Did setting the initial values to b(2)=20, b(5)=40 work?
Star Strider
on 19 May 2014
If I have time today, I’ll expand the two-peak plot to four and let you experiment with it.
Star Strider
on 21 May 2014
Yes. The findpeaks function would definitely work.
Star Strider
on 21 May 2014
My pleasure!
fereshte
on 22 May 2014
Edited: fereshte
on 22 May 2014
Sorry. by this code,i get the peak values, but sometimes I obtain 3 or 4 values for peak 1- Which values of these important(max of them)?
2-Is there the code to replace these values in the formula and I do not need to enter them manually for each of the image?
Star Strider
on 4 Jun 2014
I suggest you fit them as I did in my code. Use estimates for b(2) and b(5) from findpeaks as your initial parameter estimates for those elements in the call to fminsearch.
Image Analyst
on 29 Jun 2014
Please put that "Answer" right here. No need to put a comment here and then say that Star has to look somewhere else down the page for it, especially since it's not really an "Answer" to your original post but a comment to Star.
fereshte
on 29 Jun 2014
Edited: fereshte
on 29 Jun 2014
dear Star Strider
i use youre code for my dataset. but it dont work for all images. i change B line in this code respect to locs_Rwave for some images that dont get me true response.but its very difficult.can you help me to get the code that run perfectlly for all images with out any change in B line?
More Answers (1)
See Also
Categories
Find more on Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)