Plot shows in legend but not in graph?

4 次查看(过去 30 天)
subplot(2,2,1);
plot(x1,y1,'bs')
hold on
p1 = polyfit(x1,y1,35);
f1 = polyval(p1,x1);
plot(x1,f1,'--r')
hold off
legend
title('Data Set 1')
xlabel('X1 Values')
ylabel('Y1 Values')
subplot(2,2,2);
plot(x2,y2,'g*')
hold on
p2 = polyfit(x2,y2,35);
f2 = polyval(p2,x2);
plot(x2,f2,'--b')
hold off
legend
title('Data Set 2')
xlabel('X2 Values')
ylabel('Y2 Values')

回答(1 个)

Jaswanth
Jaswanth 2024-6-11
Hi John,
As per the code you have provided, I observed that you are using a high-degree polynomial which is having a value of ‘35’ when calling the “polyfit” function for fitting your data, which can cause issues similar to one you are facing where plot is not visible.
This problem often arises because a polynomial fit of degree ‘35’ is extremely sensitive to the specific data points, leading to overfitting. Overfitting makes the model too closely aligned with the data points, which can result in a plot that does not visually represent the data well.
When testing with a similar setup, MATLAB throws a warning that the polynomial is badly conditioned, suggesting that the fit may not be appropriate.
Kindly refer to the image below to see the warning:
The above warning points towards possible solutions: adding more data points, reducing the polynomial's degree, or adjusting the data through centering and scaling.
For a more detailed explanation and potential solutions, please refer to the following MathWorks documentation on the "polyfit” function: www.mathworks.com/help/matlab/ref/polyfit.html
I hope the information provided above is helpful in resolving the issue.

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by