Finding slope for the polyfit line

48 ビュー (過去 30 日間)
Michelangelo Cannistraro
Michelangelo Cannistraro 2022 年 12 月 2 日
コメント済み: Torsten 2022 年 12 月 2 日
load data_matrix.mat
data=SECTION_L;
data(:,1:3)=[];
row=9;
cleanup=data([1 2 3 row],:);
% year range
y1 = 1975;
y2 = 2016;
% init
x = y1:y2;
nx = numel(x);
mo_av = zeros(nx,12);
mo_max = zeros(nx,12);
mo_min = zeros(nx,12);
max_day = zeros(nx,12);
min_day = zeros(nx,12);
k = 0;
for k = 1:nx % year loop
col_dat_yr=find(cleanup(1,:)== x(k)); % find data corresponding to year = x(k)
data_yr=cleanup(:,col_dat_yr);
for mmonth = 1:12
col_dat_mo=find(data_yr(2,:)==mmonth);
data_mo=data_yr(:,col_dat_mo);
mo_av(k,mmonth)=mean(data_mo(4,:));
mo_max(k,mmonth)=max(data_mo(4,:));
mo_min(k,mmonth)=min(data_mo(4,:));
max_day(k,mmonth)=find(data_mo(4,:)==mo_max(k,mmonth));
min_day(k,mmonth)=find(data_mo(4,:)==mo_min(k,mmonth));
end
end
% plot montly data
str_mo = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};
figure
sgtitle('Monthly Data : Avg')
for ck = 1:12
coefs1=polyfit(x,mo_av(:,ck),1);
curve1=polyval(coefs1,x);
subplot(3,4,ck)
plot(x,mo_av(:,ck),'-',x,curve1);
xlim([1970,2020])
ylim([-25,5])
xlabel('Years')
ylabel('Temperature C')
title(str_mo(ck));
end
figure
sgtitle('Monthly Data : Max')
for ck = 1:12
coefs2=polyfit(x,mo_max(:,ck),1);
curve2=polyval(coefs2,x);
subplot(3,4,ck)
plot(x,mo_max(:,ck),'-',x,curve2);
xlim([1970,2020])
ylim([0,25])
xlabel('Years')
ylabel('Temperature C')
title(str_mo(ck));
end
figure
sgtitle('Monthly Data : Min')
for ck = 1:12
coefs3=polyfit(x,mo_min(:,ck),1);
curve3=polyval(coefs3,x);
subplot(3,4,ck)
plot(x,mo_min(:,ck),'-',x,curve3);
xlim([1970,2020])
ylim([-30,10])
xlabel('Years')
ylabel('Temperature C')
title(str_mo(ck));
end
I have made the plots with 12 graphs for three figures and have put a line through the 36 graphs. How would you find the slope of the polyfit and polyval lines for every graph.

採用された回答

Voss
Voss 2022 年 12 月 2 日
polyfit returns the coefficients in a vector, with the highest-degree coefficient first. Therefore, the slopes are the values of coefs1(1) (similarly, coefs2(1) and coefs3(1)) in each iteration.
  2 件のコメント
Michelangelo Cannistraro
Michelangelo Cannistraro 2022 年 12 月 2 日
but i need to get 36 slopes and my variable curve1 etc only give one slope
Torsten
Torsten 2022 年 12 月 2 日
Replace
coefs1=polyfit(x,mo_av(:,ck),1);
by
coefs1=polyfit(x,mo_av(:,ck),1);
slope1(ck) = coefs1(1);
(same for the remaining two loops).
Then you have 36 slopes (slope1(1:12),slope2(1:12) and slope3(1:12)).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by