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

4 Ansichten (letzte 30 Tage)
Ilan
Ilan am 4 Sep. 2023
Beantwortet: William Rose am 4 Sep. 2023
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

Antworten (2)

Star Strider
Star Strider am 4 Sep. 2023
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 am 4 Sep. 2023
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.

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by