![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/359635/image.png)
Fit curve when corresponding to one x value we have multiple y value?
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Akshay Kumar
le 12 Sep 2020
Modifié(e) : Image Analyst
le 12 Sep 2020
I have a data set where corresponding to one x value multiple y values are available. I tring to a*x^b function to these data using curve fitting app.
I an getting error 'Data sizes are incompatible'.
Do'nt know to arrange these data.
Help needed.
x = [ 0.0500 0.5000 5.0000 15.0000 25.0000];
y = [ 1.0000 0.9686 0.9893 0.9585 0.9484;...
1.0000 0.8911 0.8813 0.8420 0.8375;...
1.0000 0.7802 0.7747 0.7802 0.8022;...
1.0000 0.9819 0.9392 0.9038 0.8923;...
1.0000 0.9590 0.8974 0.8840 0.7308;...
1.0000 0.8803 0.8872 0.6493 0.7214;...
1.0000 0.9551 0.8923 0.8163 0.7953;...
1.0000 0.9591 0.9084 0.8098 0.6922;...
1.0000 0.8306 0.7610 0.6142 0.6299;...
1.0000 0.8993 0.8583 0.8115 0.7535;...
1.0000 0.9072 0.8780 0.7501 0.6728;...
1.0000 0.9348 0.8529 0.6733 0.5870;...
1.0000 0.8333 0.8167 0.7667 0.7000;...
1.0000 0.9048 0.8493 0.7405 0.6214;...
1.0000 0.8892 0.7967 0.6878 0.5504];
0 commentaires
Réponse acceptée
Image Analyst
le 12 Sep 2020
Modifié(e) : Image Analyst
le 12 Sep 2020
Usually what I do in that situation is to add a very tiny, insignificant bit a noise to the x values. It will be so small that it won't affect the fit but it will prevent you from having duplicate x values:
x = [ 0.0500 0.5000 5.0000 15.0000 25.0000];
y = [ 1.0000 0.9686 0.9893 0.9585 0.9484;...
1.0000 0.8911 0.8813 0.8420 0.8375;...
1.0000 0.7802 0.7747 0.7802 0.8022;...
1.0000 0.9819 0.9392 0.9038 0.8923;...
1.0000 0.9590 0.8974 0.8840 0.7308;...
1.0000 0.8803 0.8872 0.6493 0.7214;...
1.0000 0.9551 0.8923 0.8163 0.7953;...
1.0000 0.9591 0.9084 0.8098 0.6922;...
1.0000 0.8306 0.7610 0.6142 0.6299;...
1.0000 0.8993 0.8583 0.8115 0.7535;...
1.0000 0.9072 0.8780 0.7501 0.6728;...
1.0000 0.9348 0.8529 0.6733 0.5870;...
1.0000 0.8333 0.8167 0.7667 0.7000;...
1.0000 0.9048 0.8493 0.7405 0.6214;...
1.0000 0.8892 0.7967 0.6878 0.5504];
[rows, columns] = size(y)
xm = repmat(x, [rows, 1])
% Make into vectors.
xv = xm(:);
yv = y(:);
plot(xv, yv, '+', 'LineWidth', 3, 'MarkerSize', 12);
% Add a tiny bit of noise to each x value
xv = xv + 0.000001 * rand(length(xv), 1);
% Fit to a cubic
coefficients = polyfit(xv, yv, 3);
% Get new values
yFit = polyval(coefficients, xv);
hold on
plot(xv, yFit, 'r-', 'LineWidth', 3);
grid on;
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
legend('data', 'fit');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/359635/image.png)
Use fitnlm since you want a power law. I'm attaching some examples of fitnlm().
0 commentaires
Plus de réponses (1)
Star Strider
le 12 Sep 2020
If you want to regress all of them to the same set of parameters, this works:
xm = repmat(x, size(y,1), 1);
fcn = @(p,x) p(1) .* x.^p(2);
cf = @(b) norm(y - fcn(b,xm));
B = fminsearch(cf, [1;1]);
RL = fcn(B,xm);
figure
plot(x, y)
hold on
plot(x, RL, '--', 'LineWidth',2)
hold off
grid
xlabel('x')
ylabel('y')
producing:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/359629/image.png)
Use a loop and loop through the rows of ‘y’ to fit them individually.
.
0 commentaires
Voir également
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!