Matlab Project.

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

MATLAB PROJECT

HASSAN QURESHI(23I3029)

AZFAAR SALAHUDDIN(23I3005) UMAR RIAZ(23I3028)


1.Objectives and Introduction:
The objective of this project is to solve the problem of finding the sales of a product over time
as well as to identify other trends such as the increase in sales and the total amount sold of the
product over a given amount of time. For this purpose a linear regression model is suitable due
to it being consistent and reliable in its results.

A linear regression model is used in cases when the data available has a low variance which in
this case is true i.e sales does not have a high variance. It is important to note that other
analytical and mathematical techniques will also be used to plot graphs and extrapolate data.

1
2.ANALYTICAL SOLUTION

2
3
4
5
6
3.Matlab Code:
Functions:
disp('text'): Presents a specified text in the command window.

input('prompt'): Prompts user input with a given message.

diff(array): Computes the differences between consecutive elements in an array, essential for
determining the rate of change of laptop sales.

fitlm(x, y): Constructs a linear regression model utilizing specified predictor variables x and a
response variable y.

predict(mdl, x): Predicts response variable values using a linear regression model (mdl) and
predictor variable values (x).

scatter(x, y, 'o', 'MarkerEdgeColor', 'b'): Generates a scatter plot with blue markers.

plot(x, y, 'r-', 'LineWidth', 2): Constructs a line plot with red lines and a defined line width.

cumsum(array): Calculates the cumulative sum of elements in an array.

Toolboxes:
While the provided code does not explicitly necessitate external toolboxes, standard MATLAB
functionalities are applied. In scenarios requiring additional capabilities, such as advanced
statistics or curve fitting, one might consider toolboxes like the Statistics and Machine Learning

7
Toolbox or Curve Fitting Toolbox. Ensure the relevant toolboxes are installed for the intended
code functionality.

4.Matlab Solution and Results:


Step-by-step demonstration:

Step 1: Displaying Names


disp('HASSAN QURESHI 23I3029.');
disp('UMAR RIAZ 23I3028.');
disp('AZFAAR SALAHUDDIN 23I3055.');

This step simply displays the names in the command window.

Step 2: Main Loop


while true
% Code inside the loop
end

This sets up a perpetual loop. The loop will continue until explicitly broken with the break
statement.

Step 3: Given Data


years = [2018, 2019, 2020, 2021, 2022];
laptops_sold = [2343, 2387, 3245, 2763, 4521];

Defines the years and the corresponding number of laptops sold.

Step 4: Calculate Rate of Change of Sales

8
sales_change = diff(laptops_sold);

Calculates the rate of change of laptop sales using the diff function.

Step 5: Create a Linear Regression Model for the Rate of Change


mdl_change = fitlm(years(2:end)', sales_change');
Creates a linear regression model for the rate of change using the fitlm function.

Step 6: User Input for Prediction Year


year_to_predict = input('Enter a year to predict laptop sales: ');

Asks the user to input a year for which laptop sales will be predicted.

Step 7: Predict Laptop Sales


all_years = 2018:year_to_predict;
mdl = fitlm(years', laptops_sold');
predicted_sales = predict(mdl, all_years');

Uses the linear regression model to predict laptop sales for the specified year and all years up
to that year

Step 8: Display Predicted Sales and Total Laptops Sold


disp(['Predicted laptop sales in ', num2str(year_to_predict), ': ',
num2str(predicted_sales(end))]);
total_laptops_sold = sum(predicted_sales);
disp(['Total laptops sold up to ', num2str(year_to_predict), ': ', num2str(total_laptops_sold)]);

Displays the predicted laptop sales for the specified year and the total laptops sold up to that
year.

9
Step 9: Plot Data Points and Regression Line
Figure;
scatter(years, laptops_sold, 'o', 'MarkerEdgeColor', 'b');
hold on; plot(all_years, predicted_sales, 'r-', 'LineWidth', 2); % ... (code for title, labels, legend)
hold off;

Plots the data points and the linear regression line.

Step 10: Pause and Continue


disp('Press any key to continue...');
pause;

Pauses the program and waits for the user to press any key before continuing.

Step 11: Plot Rate of Change


figure;
plot(years(2:end), sales_change, 'b-', 'LineWidth', 2);
hold on;
% ... (code for average rate of change and plotting)
hold off;

Plots the rate of change of sales and the average rate of change.

Step 12: Plot Total Laptop Sales Over the Years


figure;
plot(all_years, cumsum(predicted_sales), 'b-', 'LineWidth', 2);
% ... (code for title, labels)

10
Plots the total laptops sold over the years.

Step 13: User Input for Program Continuation


user_input = input('If you want to exit the program, press 0. Otherwise, press any other key: ',
's');
if strcmp(user_input, '0')
disp('Exiting the program...');
break; % exit the loop
end

Asks the user whether to exit the program or continue. If '0' is entered, the program exits the
loop and terminates. Otherwise, it continues to the next iteration.

These steps collectively create a MATLAB program that predicts and displays laptop sales, plots
various graphs, and allows the user to continue or exit the program.

The by-hand and matlab solutions offer the same information but in slightly different ways. The
matlab solution firstly, is more accurate in terms of its results as it uses precise equations and
algorithms while using the dataset whereas the by hand solution simply uses some analytical
techniques to get as close to the ‘real’ answer as possible. The matlab solution is also much
faster. Due to the aforementioned simple nature of the linear regression models, the by-hand
and matlab solutions have very little to no differences in terms of the graph alone.

The physical interpretation of the first graph: It shows a linear relation between sales in the
year and the year number hence as we go ahead in years the sales number also increases.
Physical interpretation of second graph: It shows that there is a constant increase in the sales
per year so that is why there is a constant gradient.
Physical interpretation of third graph: There is an exponential increase in the number of total
sales as the sales per year increases so the total sales increase at an increasing rate.

11
COMPARISON OF BY-HAND AND MATLAB SOLUTION

12
13
14
5.Flowchart: Start

Given Data: Years, Laptops Sold

Calculate Rate of Change of Sales

Create Linear Model for Rate of Change

User Input for Prediction Year

Predict Laptop Sales for Specified Year

Display Predicted Sales and Total Laptops Sold

Pause and continue

Plot Rate of Change and Average Rate


of Change

Plot Total Laptops Sold Over


the Years

User Input for


Program
Continuation

Input == 0

15
End
6.Conclusions:
The problem discussed in this project was of a predictive model in the context of predicting the
sales of a product over time. The method used to solve the problem was a linear regression
predictive model along with different analytical techniques to come to a reasonable solution.

The project proved to be highly fruitful in its endeavors and succeeded in accomplishing what it
was intended to do which not only included prediction of sales of a product over time but also
the estimation of total sales made in a given amount of time and the difference occurring in the
sales per year i.e the gradient.

16
7.Contribution:
Two of the members, Qureshi and Umar worked on the matlab related work whereas only One
member Azfaar worked on the handwritten solution. This is because of the much more
extensive work required in matlab as compared to the handwritten solution. The final report’s
creation was a team effort as it was equally distributed among the members while still keeping
in mind the role of each member hence Qureshi was still given the matlab code to explain in
the report.

The involvement of all group members was necessary for any progress to be made which is why
initially difficulties were faced mainly in communication and workload distribution, however
with the use of methods such as increased use of social media platforms, the communication
gap was closed which automatically led to a better distribution of tasks and allowed the
completion of the project on time.

17
18

You might also like