?precision Agriculture Yield Forecast Using MATLAB
?precision Agriculture Yield Forecast Using MATLAB
Project Report:
Precision Agriculture Yield Forecast
Submitted to
Section : B
Trimester : Fall-2024
Submitted by
Name ID
Md.Amit Hasan 021 223 0084
Md. Luman Hossain 021 221 034
1. Introduction:
As the global population grows, the demand for more food continues to rise, making efficient use of
agricultural resources more important than ever. Predicting crop yields is key to meeting this demand,
helping farmers and decision-makers plan better farming practices and ensure food availability.
This project, titled "Precision Agriculture Yield Forecast," focuses on understanding how fertilizer use
affects crop yields. By analyzing data and using predictive models, the project aims to reveal how
different amounts and types of fertilizers influence the amount of crops produced.
2. Problem Statement:
The main challenge of this project is to predict crop yields by examining the effects of two key factors:
fertilizer usage and rainfall. Fertilizers enhance soil nutrients, boosting plant growth and crop production.
At the same time, rainfall influences soil moisture, plant hydration, and overall crop health. This project
seeks to understand how these factors interact and affect crop yields, helping farmers make smarter
decisions to improve agricultural productivity.
Predicting crop yield based on fertilizer usage and rainfall involves several challenges that require a
comprehensive approach. Key issues include:
● Data Variability: Agricultural conditions differ greatly due to variations in soil types, crop
varieties, and local weather patterns. This makes it difficult to create predictive models that work
well across all environments.
● Nonlinear Relationships: The relationship between fertilizer usage, rainfall, and crop yield is
often nonlinear. Simple linear models may not accurately capture these complex interactions.
● Data Availability and Quality: Gathering accurate and complete data on fertilizer use, rainfall,
and crop yields can be difficult. Missing or unreliable data can reduce the accuracy of predictive
models.
● External Factors: Crop yield can also be affected by other factors, such as pests, diseases, and
farming practices. Including these additional variables in the model adds further complexity.
4. Methodology:
● Fertilizer Usage: Determines the nutrient content of the soil, which is essential for plant growth.
● Rainfall: Influences soil moisture and plant hydration, both crucial for healthy crop development.
Together, these variables help predict crop yields with greater precision.
After training the polynomial regression model, we analyzed it to understand how much and in what way
each predictor variable affected the outcome. We then evaluated the model's performance using metrics
like mean squared error or R-squared values to measure how accurately it predicts results.
6. Data Collection:
The dataset for this project was collected from various agricultural research institutions, government
agricultural departments, and local farming cooperatives. It includes historical data from different
agricultural seasons and regions, focusing on important variables for our predictive model:
Fertilizer Usage: Measures the amount of nitrogen-based fertilizer applied per area, in kilograms per
hectare (kg/ha).
Rainfall: Records the amount of precipitation over time, in millimeters (mm), and includes both total
rainfall and its distribution.
Crop Yield: The weight or quantity of crops produced per unit area, measured in kilograms per hectare
(kg/ha).
6.1. Data table:
In this section, we discuss the results of the second-order polynomial regression model, highlighting the
coefficients of the polynomial equation and offering graphical depictions of the regression curves. These
visual aids shed light on the complex relationships between fertilizer application, rainfall, and crop yield.
7.1. Code for Crop Yield vs. Fertilizer Usage (With Regression Line ):
Clc;clear all;
fertilizer = [50, 5.33, 200, 158.81, 8.02, 24.3, 90, 328.3, 20,180.55, 154.83, ...
131.53, 49.56,95.28, 178.57, 100.21, 56.85, 100.21, 32.07, 143.97];
crop_yield = [2, 1.98,3.2, 4.9, 3.44, 2.94, 3.5,3.8, 1.5, 6.57, 5.8, 6.21, ...
4.86, 18.84, 13.39, 4.75, 3.2, 2.65, 1.22, 2.19];
n = 1;
m = length(fertilizer);
A = zeros(n + 1, n + 1);
B = zeros(n + 1, 1);
for row = 1:n+1
for col = 1:n+1
if row == 1 && col == -4
A(row, col) = m;
else
A(row, col) = sum(fertilizer.^(row + col - 2));
end
end
B(row) = sum((fertilizer.^(row - 1)) .* crop_yield);
end
coefficients = A \ B;
disp('Coefficients of the quadratic regression:');
disp(coefficients);
predicted_yield = polyval(coefficients(end:-1:1), fertilizer)
residuals = predicted_yield - crop_yield;
SSE = sum(residuals.^2);
RMSE = sqrt(SSE / m);
disp(['Sum of Squared Residuals (SSE): ' num2str(SSE)]);
disp(['Root Mean Squared Error (RMSE): ' num2str(RMSE)]);
scatter(fertilizer, crop_yield, 'b', 'filled');
hold on;
g = linspace(min(fertilizer), max(fertilizer), 100);
regression_curve = polyval(coefficients(end:-1:1), g);
plot(g, regression_curve, 'r', 'LineWidth', 1.5);
xlabel('Fertilizer (kg per hectare)');
ylabel('Crop Yield (tonnes)');
title('Crop Yield vs. Fertilizer Usage (Quadratic Regression)');
legend('Data points', 'Quadratic Curve');
grid on;
hold off;
Error:
Sum of Squared Residuals (SSE): 320.364
Plot:
7.2. Code for Crop Yield vs. Fertilizer Usage (Polynomial):
Error:
● Sum of Squared Residuals (SSE): 282.5357
● Root Mean Squared Error (RMSE): 3.7586
Plot:
7.3. Code for Crop Yield vs. Rainfall with Regression Line:
clc;clear all;
rainfall = [534, 217.90, 1761, 2200, 1679.4, 2909, 537, 120, 1150, 2590, 1600, 1500, ...
150, 250, 112, 2950, 1600, 2450, 300, 80];
crop_yield = [2, 1.98,3.2, 4.9, 3.44, 2.94, 3.5,3.8, 1.5, 6.57, 5.8, 6.21, ...
4.86, 18.84, 13.39, 4.75, 3.2, 2.65, 1.22, 2.19];
n = 1;
m = length(rainfall);
A = zeros(n + 1, n + 1);
B = zeros(n + 1, 1);
for row = 1:n+1
for col = 1:n+1
if row == 1 && col == -4
A(row, col) = m;
else
A(row, col) = sum(rainfall.^(row + col - 2));
end
end
B(row) = sum((rainfall.^(row - 1)) .* crop_yield);
end
coefficients = A \ B;
disp('Coefficients of the quadratic regression:');
disp(coefficients);
predicted_yield = polyval(coefficients(end:-1:1), rainfall)
residuals = predicted_yield - crop_yield;
SSE = sum(residuals.^2);
RMSE = sqrt(SSE / m);
disp(['Sum of Squared Residuals (SSE): ' num2str(SSE)]);
disp(['Root Mean Squared Error (RMSE): ' num2str(RMSE)]);
scatter(rainfall, crop_yield, 'b', 'filled');
hold on;
g = linspace(min(rainfall), max(rainfall), 100);
regression_curve = polyval(coefficients(end:-1:1), g);
plot(g, regression_curve, 'r', 'LineWidth', 1.5);
xlabel('Rainfall (mm)');
ylabel('Crop Yield (tonnes)');
title('Crop Yield vs Rainfall ( Regression Line )');
legend('Data points', 'Regression Line');
grid on;
hold off;
Error:
● Sum of Squared Residuals (SSE): 330.1442
● Root Mean Squared Error (RMSE): 4.0629
Plot:
7.4. Code for Crop Yield vs. Rainfall with Quadratic curve (Polynomial):
clc;clear all;
rainfall = [534, 217.90, 1761, 2200, 1679.4, 2909, 537, 120, 1150, 2590, 1600, 1500, ...
150, 250, 112, 2950, 1600, 2450, 300, 80];
crop_yield = [2, 1.98,3.2, 4.9, 3.44, 2.94, 3.5,3.8, 1.5, 6.57, 5.8, 6.21, ...
4.86, 18.84, 13.39, 4.75, 3.2, 2.65, 1.22, 2.19];
n = 2;
m = length(rainfall);
A = zeros(n + 1, n + 1);
B = zeros(n + 1, 1);
for row = 1:n+1
for col = 1:n+1
if row == 1 && col == -4
A(row, col) = m;
else
A(row, col) = sum(rainfall.^(row + col - 2));
end
end
B(row) = sum((rainfall.^(row - 1)) .* crop_yield);
end
coefficients = A \ B;
disp('Coefficients of the quadratic regression:');
disp(coefficients);
predicted_yield = polyval(coefficients(end:-1:1), rainfall);
residuals = predicted_yield - crop_yield;
SSE = sum(residuals.^2);
RMSE = sqrt(SSE / m);
disp(['Sum of Squared Residuals (SSE): ' num2str(SSE)]);
disp(['Root Mean Squared Error (RMSE): ' num2str(RMSE)]);
scatter(rainfall, crop_yield, 'b', 'filled');
hold on;
g = linspace(min(rainfall), max(rainfall), 100);
regression_curve = polyval(coefficients(end:-1:1), g);
plot(g, regression_curve, 'r', 'LineWidth', 1.5);
xlabel('Rainfall (mm)');
ylabel('Crop Yield (tonnes)');
title('Crop Yield vs. Rainfall (Quadratic Regression)');
legend('Original Data', 'Quadratic Curve');
grid on;
hold off;
Error:
● Sum of Squared Residuals (SSE): 322.2163
● Root Mean Squared Error (RMSE): 4.0138
Plot:
clc;clear all;
fertilizer = [50, 5.33, 200, 158.81, 8.02, 24.3, 90, 328.3, 20,180.55, 154.83, ...
131.53, 49.56,95.28, 178.57, 100.21, 56.85, 100.21, 32.07, 143.97];
crop_yield = [2, 1.98,3.2, 4.9, 3.44, 2.94, 3.5,3.8, 1.5, 6.57, 5.8, 6.21, ...
4.86, 18.84, 13.39, 4.75, 3.2, 2.65, 1.22, 2.19];
n =2 ;
m = length(fertilizer);
A = zeros(n + 1, n + 1);
B = zeros(n + 1, 1);
for row = 1:n+1
for col = 1:n+1
if row == 1 && col == -4
A(row, col) = m;
else
A(row, col) = sum(fertilizer.^(row + col - 2));
end
end
B(row) = sum((fertilizer.^(row - 1)) .* crop_yield);
end
% Solve for coefficients
coefficients = A \ B;
disp('Coefficients of the quadratic regression:');
disp(coefficients);
% Predict crop yield for 150 tons of fertilizer
fertilizer_prediction = 150;
crop_yield_prediction = polyval(coefficients(end:-1:1), fertilizer_prediction);
fprintf('Predicted crop yield for %.2f tons of fertilizer: %.2f tons\n', ...
fertilizer_prediction, crop_yield_prediction);
Output:
7.6. Code for Optimal fertilizer amount for maximum crop yield( using
Differentiation method ):
Plot:
Output:
Visualizing the relationship between fertilizer usage and crop yield, and rainfall and crop yield, these
regression curves are plotted with fertilizer or rainfall on the x-axis and crop yield on the y-axis. Scatter
plots of the actual data points provide a comprehensive view of the trends.
Fertilizer vs. Crop Yield: The regression curve for fertilizer usage shows a nonlinear relationship. The
positive coefficient for the cross-term (Fertilizer * Rainfall) suggests that the combined effect of fertilizer
and rainfall can enhance crop yield more than their individual contributions.
Rainfall vs. Crop Yield: The regression curve for rainfall and crop yield also displays curvature,
indicating nonlinear sensitivity. The quadratic term (Rainfall^2) suggests diminishing returns with
increased rainfall. While adequate moisture is crucial for optimal yield, excessive rainfall might not
proportionally boost crop output.
10. Discussion
Significance of Findings:
Informed Decision-Making: The model's predictions enable farmers to make more informed decisions
about fertilizer application and irrigation. By identifying optimal fertilizer levels and considering rainfall
thresholds, farmers can maximize crop yield while minimizing resource waste.
Resource Allocation: With insights into the nonlinear responses of crops to fertilizer and rainfall,
resource allocation can be optimized. This includes determining the precise amount of fertilizer and
irrigation needed to achieve desired yield levels.
Sustainability: The model promotes sustainable agriculture by preventing the overuse of fertilizers and
optimizing irrigation practices. This approach minimizes the environmental impact associated with
excessive resource application and encourages environmentally conscious farming practices.
11. Conclusion:
This project, "Precision Agriculture Yield Forecast," demonstrates the nonlinear effects of fertilizer
and rainfall on crop yield using polynomial regression models. The results show that combined effects of
fertilizer and rainfall enhance yield more than their individual contributions, though excessive rainfall
can lead to diminishing returns.
The findings offer valuable insights for informed decision-making, resource allocation, and sustainable
agriculture by optimizing fertilizer and irrigation practices. This data-driven approach promotes efficient
and environmentally conscious farming methods.
END