clear all;
clc;
disp('POLYNOMIAL REGRESSION METHOD');
V = [ 0 8 8 8 8 8 8 12 1153 4086 6712 6686 15361 21662 22641 19537 14130 15349 12351 26811 29737 28437 26144 26046 16653 9866 19997 25313 20821 18041 14468 13743 15462 24205 29151 29015 25720 16471 15787 16308 34407 40364 36543 42049 30438 16846 14290 28295 32494 30587 26702 21494 15529 23091 39161 23726 28561 27196 19505 9621 9825 32809 33133 27170 28222 18800 10667 14747 30084 34504 39466 12449 26402 5316 15651 50977 49599 54663 55493 47503 26888 32303 61200 48740 18039 116 33 10750 10295 60261 76551 83648 80603 60597 45208 45987 95009 99552 21988 107340 73776 65396 62416 95420 98798 105655 117563 90277 72480 81183 64436 151309 154866 157949 124618 133804 128912 197963 215876 204839 221706 180066 164282 140573 235623 250529 252773 268604 215968 187877 168956 197274 231733 258773 263012 236196 217807 206015 313761 340043 375842 376909 337478 324745 291217 421479 424541];
D = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150];

n = length(V);
Data(:,1) = V';
Data(:,2) = V'.^2;
Data(:,3) = V'.^3;
Data(:,4) = V'.^4;
Data(:,5) = D';
Data(:,6) = V'.*D';
Data(:,7) = D'.*(V'.^2);

fprintf('%s\t     %s\t     %s\t       %s\t\t    %s\t  %s\t     %s\tn    %s\n  ',' Vaccination(V) ', 'V^2', 'V^3','V^4', 'Day(D)','V*D','D*(V^2)')
disp('==========================================================================================================================================')
fprintf('%9.0f\t  &12.0f\t  %18.0f\t  %22.0f\t  %9.0f\t  %12.0f\t  %17.0f\n', Data')
fprintf('============================================================================================================\n')
fprintf('%9.0f\t  &12.0f\t  %18.0f\t  %22.0f\t  %9.0f\t  %12.0f\t  %17.0f\n', sum(Data(:,1)),sum(Data(:,2)),sum(Data(:,3)),sum(Data(:,4)),sum(Data(:,5)),sum(Data(:,6)),sum(Data(:,7))


s_x_x = sum(Data(:,2))-((sum(Data(:,1))^2)/n;
s_x_y = sum(Data(:,6))-((sum(Data(:,1))*(sum(Data(:,5))))/n;
s_x_x2 = sum(Data(:,3))-((sum(Data(:,2)))*(sum(Data(:,1))))/n;
s_x2_y = sum(Data(:,7))-((sum(Data(:,2)))*(sum(Data(:,5))))/n;
s_x2_x2 = sum(Data(:,4))-((sum(Data(:,2)))^2)/n;
s_x2 = sum(Data(:,2));

display(['sum_x2 = ' num2str(s_x2)]);
display(['sum_x_x = ' num2str(s_x_x)]);
display(['sum_x_y = ' num2str(s_x_y)]);
display(['sum_x_x2 = ' num2str(s_x_x2)]);
display(['sum_x2_y = ' num2str(s_x2_y)]);
display(['sum_x2_x2 = ' num2str(s_x2_x2)]);

disp('=====================================================================================================================')

a2 = ((s_x2_y*s_x_x)-(s_x_y*s_x_x2))/(s_x_x*s_x2_x2)-(s_x_x2)^2);
a1 = ((s_x_y*s_x2_x2)-(s_x2_y*s_x_x2))/((s_x_x*s_x2_x2)-(s_x_x2)^2);
a0 = mean(D)-al*(mean(V))-a2*(s_x2/n);

display(['a0 = ' num2str(a0)]);
display(['a1 = ' num2str(a1)]);
display(['a2 = ' num2str(a2)]);

Data(:,10) = (D' mean(V)).^2;
Data(:,11) = (D'-a0-a1.*V').^2;
syx = sqrt(sum(Data(:,7))/(n-2));
r_2 = (sum(Data(:,10))-sum(Data(:,11)))/sum(Data(:,10);

display(['coefficient of correlation, R2 = ' num2str(r_2)]);


x = [0:500000];
y = a0+(a1*x)+(a2*x.^2);
plot( x, y, "linewidth", 3,'r');;
set(gca,  "fontsize" , 20);
hold on;
plot(V, D, 'bo');
grid on;
title(' NO. of Vaccination vs NO. of Days' , "fontsize",26);
xlabel(' Number of Vaccination',"fontsize",24);
ylabel(' Number of Days',"fontsize",24);
disp('***************************************************')
disp('*')
disp('*')

disp(' LINEAR REGRESSION METHOD');
v = [ 0 8 8 8 8 8 8 12 1153 4086 6712 6686 15361 21662 22641 19537 14130 15349 12351 26811 29737 28437 26144 26046 16653 9866 19997 25313 20821 18041 14468 13743 15462 24205 29151 29015 25720 16471 15787 16308 34407 40364 36543 42049 30438 16846 14290 28295 32494 30587 26702 21494 15529 23091 39161 23726 28561 27196 19505 9621 9825 32809 33133 27170 28222 18800 10667 14747 30084 34504 39466 12449 26402 5316 15651 50977 49599 54663 55493 47503 26888 32303 61200 48740 18039 116 33 10750 10295 60261 76551 83648 80603 60597 45208 45987 95009 99552 21988 107340 73776 65396 62416 95420 98798 105655 117563 90277 72480 81183 64436 151309 154866 157949 124618 133804 128912 197963 215876 204839 221706 180066 164282 140573 235623 250529 252773 268604 215968 187877 168956 197274 231733 258773 263012 236196 217807 206015 313761 340043 375842 376909 337478 324745 291217 421479 424541];
d = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150];
n = length(v);
table(:,1) = [1:n];
table(:,2) = v';
table(:,3) = v'.^2;
table(:,4) = d';
table(:,5) = v'.*d';

disp(' i             V           V^2            D          V*D');
disp('============================================================')
fprintf('%2.0f\t %8.0f\t  %13.0f\t  %8.0f\t  %11.0f\n',table')
fprintf('==========================================================\n')
fprintf('sum\t  %2.0f\t %8.0f\t %11.0f\t %4.0f\t %12.0f\n', sum(table(:,2)),sum(table(:,3)),sum(table(:,4)),sum(table(:,5)))
fprintf('\n===========================================================================\n')

a1=(n*sum(table(:,5))-sum(table(:,4))*sum(table(:,2)))/(n*sum(table(:,3))-(sum(table(:,2))^2));
a0=mean(D)-al*mean(V);

ST = (d' - 43287.19.^2;
St = sum(ST);

SY = (d' - a0 - a1*v').^2;
Sy = sum(SY)

disp('   (yi -y)^2                   (yi-a0-a1x)^2');
disp('=============================================')
fprintf('%2.2f\t             %2.2f\n', ST, SY)
disp('==============================================')
fprintf('%2.2f\t             %2.2f\n', St, Sy);
R2 = (St-Sy)/St;
disp('========================================')
display(['a0 =' num2str(a0)]);
display(['a1 =' num2str(a1)]);
display(['coefficient of correlation, R2 =' num2str(R2)]);
disp('*****************************************')
x =[0:500000];
y = (a1*x)+(a0);
plot( x,y, "linewidth", 3, 'b');
set(gca, "fontsize", 20)
hold on:
plot (v, d, 'do');
grid on;
title('NO. OF DAYS VS NO. OF VACCINATION', "fontsize",26);
xlabel(' NO OF VACCINATION',"fontsize", 24);
ylabel(' NO OF DAYS ' , "fontsize", 24);
