필터 지우기
필터 지우기

Finding slope for the polyfit line

조회 수: 81 (최근 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 CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by