Need help in getting a polynomial to go through 0,0
조회 수: 18 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Richard Willey
2012년 3월 12일
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
Richard Willey
2012년 3월 14일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!