Need help in getting a polynomial to go through 0,0
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am stuck trying to get this polynomial to go through 0,0 and was told that the polyfit function is not capable of doing so. I'm new to matlab and any help or comments is much appreciated! Thank you.
format bank
rho=1.2; % air density in kg/m3
v=0:20:160; % velocity in km/h
v=v.*.2778; % convert velocity to m/s
f=[0 10 50 109 180 300 420 565 771]; % given force in N
p=polyfit(v,f,2); % gives the polynomial p for the second degree
polyval(p,v) % calculates the value of polynomial for each element of the vector v
vx=0:10:60; % vector vx to be used for plotting the polynomial p
fx=polyval(p,vx); % create vector fx with values of the polynomial p at each vx
plot(v,f,'o',vx,fx)
xlabel('Velocity (m/s)');
ylabel('Force (N)');
title('Drag Coefficient');
0 Kommentare
Antworten (1)
Richard Willey
am 12 Mär. 2012
I'm attaching code that illustrates how to solve the problem using either Statistics Toolbox or base MATLAB.
The Statistic Toolbox solution uses some new functionality that's shipping in 12a. The base MATLAB solution will work regardless of what version you have.
regards,
Richard
%%Generate some random data
% Note: This data includes an intercept so the regression model won't be
% accurate
X = 1:100;
X = X';
Y = 50 + 5*X + randn(100,1);
%%Option 1: Using Statistics Toolbox
myFit = LinearModel.fit(X, Y, 'y ~ -1 + x1')
% The -1 in the formula indicates that this model does not include an
% intercept
%%Option 2: Using MATLAB
coefficients = X\Y
%%With a second order polynomial
Y = 50 + 5*X + 3*X.^2 + randn(100,1);
%%Using Statistics Toolbox
myFit2 = LinearModel.fit(X,Y, 'y ~ -1 + x1^2')
%%Using MATLAB
NewX = [X, X.^2];
coefficients = NewX\Y
2 Kommentare
Richard Willey
am 14 Mär. 2012
LinearModel.fit requires new functionality in Statistics Toolbox R2012a. If you don't have access to this, you can still use the backslash command.
In my example, X is the independent variable and Y is the dependent variable.
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!