Looking to plot best-fit with equation on graph
조회 수: 1 (최근 30일)
이전 댓글 표시
I am looking to plot a line of best-fit with the equation also on the graph.
plot(0,ptv_0,'o-','MarkerFaceColor','red')
hold on
plot(5,ptv_5,'o-','MarkerFaceColor','red')
hold on
plot(10,ptv_10,'o-','MarkerFaceColor','red')
hold on
plot(15,ptv_15,'o-','MarkerFaceColor','red')
hold on
plot(20,ptv_20,'o-','MarkerFaceColor','red')
hold on
plot(25,ptv_25,'o-','MarkerFaceColor','red')
hold on
plot(30,ptv_30,'o-','MarkerFaceColor','red')
hold on
plot(35,ptv_35,'o-','MarkerFaceColor','red')
This is the code I've used to plot my data. Each numeric x value represents a frequency and each ptv_# value is a velocity corresponding to such frequency.
What would be the best course to take to plot a best fit?
Thank you.
댓글 수: 0
채택된 답변
Image Analyst
2022년 11월 29일
Try this:
x = 0 : 5 : 35;
y = [ptv_0, ptv_5, ptv_10, ptv_15, ptv_20, ptv_25, ptv_30, ptv_35];
plot(x, y, 'b.', 'MarkerSize', 30);
% Let's assume y is a quadratic in x.
coefficients = polyfit(x, y, 2);
% Get a fit to 1000 points between the min x and max x.
xFit = linspace(min(x), max(x), numel(x));
yFit = polyval(coefficients, xFit);
hold on;
plot(xFit, yFit, 'r-', 'LineWidth', 3);
xlabel('Frequency');
ylabel('Velocity');
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!