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!