how can curve fitting
2 次查看(过去 30 天)
显示 更早的评论
fereshte
2014-5-17
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 个评论
Image Analyst
2014-5-17
A screenshot of your data, or attaching your data file, would help us envision what you're talking about.
采纳的回答
Star Strider
2014-5-17
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 个评论
Star Strider
2014-5-17
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
2014-5-18
编辑:fereshte
2014-5-18
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
2014-5-18
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.
fereshte
2014-5-18
The program successfully run. dear friend, thank you so much for your help.youre very clever.Good luck in your life. only if it is possible help me to get code that calculate b(2) and b(5) for other images.
Star Strider
2014-5-18
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
2014-5-18
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
2014-5-18
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!
fereshte
2014-5-19
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
2014-5-19
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
2014-5-19
If I have time today, I’ll expand the two-peak plot to four and let you experiment with it.
fereshte
2014-5-19
can i find x-coordinate of peaks by this code?
[~,locs_Rwave] = findpeaks(meanValue);
fereshte
2014-5-22
编辑:fereshte
2014-5-22
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?
fereshte
2014-6-4
Star Strider
how obtain b(1),...b(5) for any image? i only get b(2) and b(5) by [~,locs_Rwave] = findpeaks(meanValue);
Star Strider
2014-6-4
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
2014-6-29
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
2014-6-29
编辑:fereshte
2014-6-29
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?
更多回答(1 个)
另请参阅
类别
在 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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)