Professional Documents
Culture Documents
Matlab Project.
Matlab Project.
Matlab Project.
HASSAN QURESHI(23I3029)
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.
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.
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.
This sets up a perpetual loop. The loop will continue until explicitly broken with the break
statement.
8
sales_change = diff(laptops_sold);
Calculates the rate of change of laptop sales using the diff function.
Asks the user to input a year for which laptop sales will be predicted.
Uses the linear regression model to predict laptop sales for the specified year and all years up
to that year
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;
Pauses the program and waits for the user to press any key before continuing.
Plots the rate of change of sales and the average rate of change.
10
Plots the total laptops sold over the years.
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
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