Finding slope for the polyfit line
53 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Michelangelo Cannistraro
le 2 Déc 2022
Commenté : Torsten
le 2 Déc 2022
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.
0 commentaires
Réponse acceptée
Voss
le 2 Déc 2022
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 commentaires
Torsten
le 2 Déc 2022
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)).
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!