Extrapolating from linear fit
35 ビュー (過去 30 日間)
古いコメントを表示
I have a code, and it works, except is there a way to extend the linear fits past the data they are fitted to? Currently, it plots a line over my data. How can I specify the limits of the linear fit (I'd like to extrapolate). Here is my code:
close all
clear
clc
[D,S] = xlsread('C:\PATH\kvsl.xlsx','500K');
one_over_l = D(:,10);
one_over_k = D(:,11);
ballistic_x = one_over_l((1:7),1);
ballistic_y = one_over_k((1:7),1);
diffusive_x = one_over_l((8:17),1);
diffusive_y = one_over_k((8:17),1);
figure(1)
plot(ballistic_x, ballistic_y,'o')
hold on
P = polyfit(ballistic_x,ballistic_y,1);
yfit1 = P(1)*ballistic_x+P(2);
hold on;
plot(ballistic_x,yfit1,'r-.');
Q = polyfit(diffusive_x,diffusive_y,1);
yfit2 = Q(1)*diffusive_x+Q(2);
hold on;
plot(diffusive_x,yfit2,'b-');
plot(diffusive_x, diffusive_y,'x')
grid on
ylabel('1/\kappa (W/m.K)^{-1}')
xlabel('1/L (\mum^{-1})')
0 件のコメント
回答 (2 件)
Bruno Luong
2018 年 11 月 9 日
編集済み: Bruno Luong
2018 年 11 月 9 日
" Currently, it plots a line over my data. How can I specify the limits of the linear fit (I'd like to extrapolate)."
You get it all wrong, polyfit returns coefficients, for later usage it doesn't care the interval used for fit. You can plot in larger interval if you want
Instead of
yfit1 = P(1)*ballistic_x+P(2);
which is similar to stock function
yfit1 = polyval(P,ballistic_x);
Simply extend x
minx = min(ballistic_x);
maxx = max(ballistic_x);
dx = 0.1*(maxx-minx);
xextended = [minx-dx, maxx+dx];
yextended = polyval(P,xextended );
plot(xextended, yextended ,'r-.');
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Linear and Nonlinear Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!