How can i code a Linreg for imported data (Elimination Capacity vs Loading Rate)

5 views (last 30 days)
Hi guys - Im having some issue with perfoming via code on doing LinReg on the following imported data. I would like to calculate Linear regression on the variables named "Elimination Capacity" vs "Loading Rate". Can someone please give an coded solution - the goal is to plot in the code below. Please se code and its corresponding comments for guides.
%Defining and Importing data
filename = 'CATValuesoct22.xlsx';
data = readmatrix(filename);
% Assign the columns to variables
EliminationCapacity = data(:,1); % elimination capacity is in the first column
LoadingRate = data(:,2); % loading rate is in the second column
RemovalEfficiency = data(:,3); % removal efficiency is in the third column
EliminationCapacity_100 = data(:,2);
%Linreg of variables EliminationCapacity and LoadingRate
%HOW CAN I CODE IT and use it in the code for plot below
%plot for oct
% Create a figure
figure
% Set the first y-axis
yyaxis left
plot(LoadingRate, RemovalEfficiency, '*','LineWidth', 2);
ylabel('Removal Efficiency [%]');
% Set the second y-axis
yyaxis right
plot(LoadingRate, EliminationCapacity,'.--','LineWidth', 2);
ylabel('Elimination Capacity [g/(m^3*h)]');
hold on
plot(LoadingRate, EliminationCapacity_100, 'g--', 'LineWidth', 2);
% Set x-axis
xlabel('Loading Rate [g/(m^3*h)]');
% Add a title
title('October');
% Add a legend
legend('Removal Efficiency', 'Elimination Capacity','100% Elimination Capacity');
Best Regards - Amil Ali

Answers (1)

Vijeta
Vijeta on 15 May 2023
Edited: Vijeta on 15 May 2023
To calculate the linear regression between the variables "Elimination Capacity" and "Loading Rate" and plot the results, you can use the polyfit function in MATLAB. Here's how you can modify your code to include the linear regression calculation and the corresponding plot:
matlabCopy code :
% Defining and Importing data
filename = 'CATValuesoct22.xlsx';
data = readmatrix(filename);
% Assign the columns to variables
EliminationCapacity = data(:,1); % elimination capacity is in the first column
LoadingRate = data(:,2); % loading rate is in the second column
RemovalEfficiency = data(:,3); % removal efficiency is in the third column
EliminationCapacity_100 = data(:,2);
% Perform linear regression
coeffs = polyfit(LoadingRate, EliminationCapacity, 1);
linear_fit = polyval(coeffs, LoadingRate);
% Plot for oct
% Create a figure
figure
% Set the first y-axis
yyaxis left
plot(LoadingRate, RemovalEfficiency, '*','LineWidth', 2);
ylabel('Removal Efficiency [%]');
% Set the second y-axis
yyaxis right
plot(LoadingRate, EliminationCapacity,'.--','LineWidth', 2);
hold on
plot(LoadingRate, EliminationCapacity_100, 'g--', 'LineWidth', 2);
plot(LoadingRate, linear_fit, 'r-', 'LineWidth', 2); % Plot the linear regression line
% Set x-axis
xlabel('Loading Rate [g/(m^3*h)]');
% Add a title
title('October');
% Add a legend
legend('Removal Efficiency', 'Elimination Capacity','100% Elimination Capacity', 'Linear Fit');
% Adjust the figure layout
grid on;
In the above code, polyfit is used to calculate the coefficients of the linear regression line. The polyvalfunction is then used to evaluate the linear fit at each point along the LoadingRate values. Finally, the linear fit is plotted on the figure using plot along with the other data.
Make sure you have the appropriate data file ('CATValuesoct22.xlsx') in the current working directory or provide the correct path to the file.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!