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
댓글 수: 0
답변 (2개)
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’.
.
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!
