Looking to plot best-fit with equation on graph

2 vues (au cours des 30 derniers jours)
Ryan
Ryan le 29 Nov 2022
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.

Réponse acceptée

Image Analyst
Image Analyst le 29 Nov 2022
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');

Plus de réponses (0)

Catégories

En savoir plus sur Chemistry dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by