% Define the maximum and
minimum temperature data from
2019 to 2023
max_temperature_data = [
28, 32, 35, 37, 36, 37, 36, 36,
36, 35, 33, 30; % 2019
29, 32, 36, 38, 35, 36, 36, 35,
35, 34, 32, 29; % 2020
29, 33, 36, 37, 37, 35, 35, 35,
36, 35, 32, 31; % 2021
29, 32, 36, 36, 36, 35, 35, 36,
35, 34, 32, 30; % 2022
29, 33, 34, 41, 39, 38, 36, 35,
35, 35, 34, 31 % 2023
];
min_temperature_data = [
10, 12, 16, 19, 22, 23, 25, 26,
25, 22, 20, 12; % 2019
12, 13, 18, 21, 22, 24, 26, 23,
26, 25, 19, 14; % 2020
14, 12, 22, 22, 24, 23, 25, 24,
26, 24, 17, 14; % 2021
12, 14, 21, 22, 24, 24, 26, 27,
25, 20, 20, 15; % 2022
10, 16, 21, 21, 19, 12, 26, 26,
26, 23, 20, 16 % 2023
];
% Input year and month from
user
year = input('Enter the year
(2019-2028): ');
month = input('Enter the month
(1-12): ');
% Check if the input year is valid
if year < 2019 || year > 2028
error('Year should be between
2019 and 2028.');
end
% Historical data years (2019-
2023)
years = 2019:2023;
% Perform linear regression
(degree 1 polynomial) for each
month
p_max = zeros(12, 2); % Linear
regression
coefficients for max temperature
(2 coefficients)
p_min = zeros(12, 2); % Linear
regression coefficients for min
temperature (2 coefficients)
for i = 1:12
% Fit linear regression to the
maximum temperature data for
each month
p_max(i, :) = polyfit(years,
max_temperature_data(:,i), 1);
% Fit linear regression to the
minimum temperature data for
each month
p_min(i, :) = polyfit(years,
min_temperature_data(:,i), 1);
end
% Generate predicted data for
maximum and minimum
temperatures from 2024 to 2028
pred_max = zeros(5, 12); %
Predicted max temperatures for
2024-2028
pred_min = zeros(5, 12); %
Predicted min temperatures for
2024-2028
future_years = 2024:2028;
for i = 1:12
% Predict maximum
temperatures for 2024-2028
using the linear regression model
pred_max(:, i) =
polyval(p_max(i, :),
future_years)';
% Predict minimum
temperatures for 2024-2028
using the linear regression model
pred_min(:, i) =
polyval(p_min(i, :),
future_years)';
end
% Combine the predicted values
with the historical data (2019-
2023)
all_max_data =
[max_temperature_data;
pred_max];
all_min_data =
[min_temperature_data;
pred_min];
% Extract the predicted data for
the selected year
year_index = year - 2019 + 1; %
Index for the selected year (1 for
2019, 2 for 2020, ..., 9 for 2028)
selected_max =
all_max_data(year_index, :);
selected_min =
all_min_data(year_index, :);
% Use Lagrange interpolation to
estimate the temperature for the
selected year and month
% Normalize the year value
normalized_year = (year -
2019) / (2028 - 2019); %
Normalized to range from 0 to 1
% Lagrange interpolation for
maximum temperature
max_temp_pred =
lagrange_interpolation(normalize
d_year,
0:1:9, all_max_data(:, month));
% Lagrange interpolation for
minimum temperature
min_temp_pred =
lagrange_interpolation(normalize
d_year, 0:1:9, all_min_data(:,
month));
% Output the predicted
temperatures for the selected
month and year
fprintf('Predicted Maximum
Temperature for %d/%d: %.2f°C\
n', month, year,
max_temp_pred);
fprintf('Predicted Minimum
Temperature for %d/%d: %.2f°C\
n', month, year, min_temp_pred);
% Plot only the selected year's
predicted maximum and
minimum temperatures
figure;
hold on;
plot(months, selected_max, '-o',
'DisplayName', 'Predicted Max
Temp',
'LineWidth', 2);
plot(months, selected_min, '-o',
'DisplayName', 'Predicted Min
Temp', 'LineWidth', 2);
title(['Predicted Monthly
Temperatures for ',
num2str(year)]);
xlabel('Month');
ylabel('Temperature (°C)');
legend('show');
grid on;
% Define the Lagrange
interpolation function (Move this
to the end of the script)
function y_interp =
lagrange_interpolation(x, x_data,
y_data)
n = length(x_data);
L = ones(n, 1);
y_interp = 0;
for i = 1:n
for j = 1:n
if j ~= i
L(i) = L(i) * (x -
x_data(j)) /
(x_data(i) - x_data(j));
end
end
y_interp = y_interp +
y_data(i) * L(i);
end
end