필터 지우기
필터 지우기

Looking to plot best-fit with equation on graph

조회 수: 1 (최근 30일)
Ryan
Ryan 2022년 11월 29일
답변: Image Analyst 2022년 11월 29일
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.

채택된 답변

Image Analyst
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개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by