Fitting data polynomial fitlm

7 ビュー (過去 30 日間)
Chameleon
Chameleon 2020 年 11 月 12 日
回答済み: Mathieu NOE 2020 年 11 月 13 日
I am trying to fit data, first to a polynomial of degree 1 and then to a polynomial of degree 1 but log(y) instead of y. How make a predicting to what the data would look like the year 2020 and plot it? (The first x-value is 1972 and it increases with 1 until 2011)
clear all
clf
load moore.mat %xtime and y are 40x1 double
%data for mooores law
%polynomial of degree 1
figure(13)
mdl1 = fitlm(xtime,y)
subplot(2,1,1)
plot(mdl1,'Color','m')
hold on
ttime = 1972:1:2020;
T = [];
for j=1972:1:2020
t = ((j.*4.4366.^20) -8.8006^10);
T = [T t];
end
plot(ttime,T)
%polynomial of degree 1 mwith log y instead of y
mdl3 = fitlm((xtime),log(y));
%maybe I should use predict
%R^2 för modellerna -> this I get with fitlm
%transforming the logarithmically fitted version back to normal
%using data from fitlm
Y = [];
for i=1972:1:2020
y_ny = (i.*0.52342-1030.3);
Y = [Y y_ny];
end
xtime_ny = 1972:1:2020;
%Får ut med fitlm i cmd window
subplot(2,1,2)
plot(xtime_ny,Y,'c-*')
legend('label1')
%are the residuals normally distributed, investigate with qqplot
%making a new plot with a prediction in year 2020 (the x data is in years)
%this plot is supposed to be with the polynomial of the first degree...
%...and the polynomial that used the logaritm
%look at graphs for the year 2020

採用された回答

Mathieu NOE
Mathieu NOE 2020 年 11 月 13 日
hello Karolina
here my suggestion; if you need further help , let me know
yes, it's a first polynomial fit once the y data are in log scale
clear all
clf
load moore.mat %xtime and y are 40x1 double
%data for mooores law
% so log(y) is linear with time (linear)
figure(1);semilogy(xtime,y);
y_log10 = log10(y);
% do some smoothing ?
N = 10;
y_log10_smoothed = myslidingavg(y_log10, N);
figure(2);plot(xtime,y_log10,'b',xtime,y_log10_smoothed,'*-r');
% polynomial fit
% Fit a polynomial p of degree 1 to the (x,y) data:
% + remove first and last points of smoothed data
ind = 2:length(xtime)-1;
xs = xtime(ind);
yls = y_log10_smoothed(ind);
p = polyfit(xs,yls,1);
% Evaluate the fitted polynomial p and plot:
% yls_fit = polyval(p,xtime);
% Y = POLYVAL(P,X) returns the value of a polynomial P evaluated at X. P
% is a vector of length N+1 whose elements are the coefficients of the
% polynomial in descending powers:
%
% Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)
yls_fit = p(1)*xtime + p(2);
figure(3);plot(xtime,y_log10,'o', xs,yls,'--',xtime,yls_fit,'-')
legend('data','data smoothed','linear fit')
% going back from log to linear scale for y
y_lin = 10.^(yls_fit);
% now use the fit model to predict future value
new_x_data = [2015 2020 2025]; % can be scalar or vector
new_y_data = 10.^(p(1)*new_x_data + p(2)); % can be scalar or vector
figure(4);semilogy(xtime,y,'o',xtime,y_lin,'-',new_x_data,new_y_data,'*');
legend('data','model fit','extrapolation')

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultiple Linear Regression についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by