How do I correctly code with polyfit?
4 views (last 30 days)
Show older comments
Eduardo Gallegos
on 14 Dec 2022
Edited: Image Analyst
on 14 Dec 2022
Hello everyone, I've been working on this problem for the last hour and I can't find a way to use polyfit and polyval like how they want it in the instructions
(1 pt) Enter the following data
x = 0 : 1 : 8;
y = [47, 12, 0, 40, 30, 36, 60, 110, 156];
(1 pt) Open a figure and plot the data with circle markers.
(3 pts) Use the polyfit() function to compute the coefficients of a 3rd-order fit to this data.
(3 pts) Set x3 = 0 : 0.2 : 8;
Use the polyval() function with the computed coefficients and the x3 vector to compute the y values of the 3rd-
order fit.
(2 pts) Use hold on and grid on.
Then plot the 3rd-order fit on the same figure.
My code
x = 0 : 1 : 8;
y = [47, 12, 0, 40, 30, 36, 60, 110, 156];
figure;
plot(x,y,'o')
3 Comments
Jonas
on 14 Dec 2022
perfect
but please use code format to post code in the forum here (see the most left button in the code tool section)
e.g.
x3 = 0:0.2:8;
instead of
x3 = 0:0.2:8;
Accepted Answer
Image Analyst
on 14 Dec 2022
Edited: Image Analyst
on 14 Dec 2022
See my polyfit demo, attached.
Adapt as needed to replace my data with yours, like this:
% Demo to illustrate how to use the polyfit routine to fit data to a polynomial
% and to use polyval() to get estimated (fitted) data from the coefficients that polyfit() returns.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
x = 0 : 1 : 8;
y = [47, 12, 0, 40, 30, 36, 60, 110, 156];
%============= CUBIC FIT ===================================
% Plot the training set of data.
plot(x, y, 'r.', 'MarkerSize', 40, 'LineWidth', 2);
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Cubic Fit', 'FontSize', fontSize);
% Do the regression with polyfit to fit a cubic polynomial.
cubicCefficients = polyfit(x, y, 3)
% The x coefficient, slope, is coefficients(1).
% The constant, the intercept, is coefficients(2).
% Make fit. It does NOT need to have the same
% number of elements as your training set,
% or the same range, though it could if you want.
% Make 500 fitted samples going the whole range of x.
xFit = linspace(min(x), max(x), 500);
% Get the estimated values with polyval()
yFit = polyval(cubicCefficients, xFit);
% Plot the fit
hold on;
plot(xFit, yFit, 'b-', 'LineWidth', 2);
grid on;
legend('Training Set', 'Fit', 'Location', 'Northwest');
caption = sprintf('Cubic Fit. Equation: y = %.2f * x^3 + %.2f * x^2 + %.2f * x + %.2f', ...
cubicCefficients(1), cubicCefficients(2), cubicCefficients(3), cubicCefficients(4));
title(caption, 'FontSize', fontSize);
0 Comments
More Answers (0)
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!