Looking to plot best-fit with equation on graph

3 ビュー (過去 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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by