Plots multiple lines-of-best-fits instead of just one

4 次查看(过去 30 天)
I am trying to make a line-of-best-fit based on input data. It plot the correct line-of-best-fit, but also another extranerous line. I am not sure why this other line is created.
coefficientsv12 = polyfit(transStrain1, longStrain1, 1)
% Create a new x axis with exactly 1000 points (or whatever you want).
xFit12 = linspace(min(transStrain1), max(transStrain1), 1000);
% Get the estimated yFit value for each of those 1000 new x locations.
yFit12 = polyval(coefficientsv12 , xFit12)
plot(transStrain1, longStrain1)
hold on
plot(xFit12, yFit12, 'r')
grid on
xlabel('Transverse Strain')
ylabel('Longitudnial Strain')
title('One-Direction for v12')
Any help is appreciated.
Thanks,
Ilan

回答(2 个)

Star Strider
Star Strider 2023-9-4
The data are missing, so an exact solution is not currently possible.
The additional lilne could be that you are plotting both the data and the fitted curve —
plot(transStrain1, longStrain1)
hold on
plot(xFit12, yFit12, 'r')
With no data and no image of the plot, it is not possible to say for certain.
The only other option I can think of is that the original data need to be sorted by ‘transStrain1’ to be certain that it is monotonically increasing and the others are sorted with respect to it. To do that, either use sortrows or sort with both outputs, and use the second one as the indedx reference to ‘longStrain1’.
.

William Rose
William Rose 2023-9-4
When I run your code, I do not get an extraneous line. See below:
transStrain1=0:.05:1; longStrain1=0.2+0.6*transStrain1+0.1*randn(1,21);
coefficientsv12 = polyfit(transStrain1, longStrain1, 1);
xFit12 = linspace(min(transStrain1), max(transStrain1), 1000);
yFit12 = polyval(coefficientsv12 , xFit12);
plot(transStrain1, longStrain1,'b*')
hold on
plot(xFit12, yFit12, 'r.')
xlabel('Transverse Strain'); ylabel('Longitudnial Strain'); grid on
title('One-Direction for v12'); legend('Data','Fit')
Try it.

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by