How do I get the line of best fit and its equation from multiple datasets?

19 visualizaciones (últimos 30 días)
How do I find the logarithmic or polynomial line of best fit from multiple datasets
I'm trying to find the line of best fit from a multiple data points all on a single graph. There isn't a x-axis as they all start from zero and go up by 1, but I have multiple y-axis values. The code below graphs them all on a single graph. I want to find the line of best fit from the graph and get the quation from it.
The data is just two columns; the first is time, and the second is the value.
close all
clear all
clc
totalVars = 5440;
rgeTimeTot = "A1:A" + totalVars;
time = readvars("Data.csv",Range=rgeTimeTot);
Error using readvars
Unable to find or open 'Data.csv'. Check the path and filename or file permissions.
timeOffset = 1720; time = time - timeOffset;
rgeTot = "B1:B" + totalVars;
pointTot = readvars("Data.csv",Range=rgeTot);
startA = 4; % First Curve Starting Point
endA = 632; % First Curve Ending Point
startB = 1410; % Second Curve Starting Point
endB = 2072; % Second Curve Ending Point
startC = 2690; % Third Curve Starting Point
endC = 3237; % Third Curve Ending Point
startD = 4403; % Fourth Curve Starting Point
endD = 5433; % Fourth Curve Ending Point
rgeA = "B" + startA + ":B" + endA;
rgeB = "B" + startB + ":B" + endB;
rgeC = "B" + startC + ":B" + endC;
rgeD = "B" + startD + ":B" + endD;
pointA = readvars("UV_4Cycles.csv",Range=rgeA);
pointB = readvars("UV_4Cycles.csv",Range=rgeB);
pointC = readvars("UV_4Cycles.csv",Range=rgeC);
pointD = readvars("UV_4Cycles.csv",Range=rgeD);
figure(1);
plot(pointA(1:10:end),'o'); hold on;
plot(pointB(1:10:end),'x'); hold on;
plot(pointC(1:10:end),'c*');
plot(pointD(1:10:end),'^');
grid on;
xlabel('Time (secs)')
ylabel('Light Intensity (mW/cm^2')
legend('Cycle 1','Cycle 2','Cycle 3','Cycle 4', 'Best Fit')
  3 comentarios
dpb
dpb el 5 de Oct. de 2022
time = readvars("Data.csv",Range=rgeTimeTot);
Error using readvars
Unable to find or open 'Data.csv'. Check the path and filename or file permissions.
First off, the file either doesn't exist or isn't in the current working directory so must solve that problem before anything else will matter...

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 5 de Oct. de 2022
Presuming the problem with the file location is resolved, a fresh start is in order -- readvars is not a good choice here it would seem...
data=readmatrix("Data.csv"); % read the time, total values
timeOffset = 1720;
data(:,1)=data(:,1)- timeOffset;
% none of the above is ever used???
START=[4;1410;2690;4403]; % the starting rows in UV file for pieces
END=[632;2072;3237;5433]; % and the ending to match...
STYLES={'o','x','c*','^'};
points=readmatrix("UV_4Cycles.csv",Range='B:B','numheaderlines',3);
for i=1:numel(START)
plot(points(START(i):10:end),STYLES{i});
if i==1, hold on, end
end
grid on;
xlabel('Time (secs)')
ylabel('Light Intensity (mW/cm^2')
legend('Cycle 1','Cycle 2','Cycle 3','Cycle 4', 'Best Fit')
NOTA BENE:
There's no time on the X axis; above plots the points versus ordinal position, would have to have commensurate time vector for each to match to have actual time.
As other noted, having the actual files to look at and have better idea what the data are would help, but the above ought to come close with somewhat less cumbersome code.
But, got to get the data files located first; use fullfile and the data directory location, do NOT cd to another location to run the code to solve that issue.

William Rose
William Rose el 5 de Oct. de 2022
It appears from the code that you are reaidng data from 2 files, and you ar extracting four portions of the data in column B from the seocnd file. The portions have different lengths: lengths of portions A, B, C, D=629, 663, 548, 1031.
You only plot every 10th data point.
Do you only want to fit every 10th data point?
Based on your comments, I assume you want to use xA=1:629, xB=1:663, etc., for the fitting. If this is not true then specify the actual x values you want to use.
Na=629; Nb=663; Nc=548; Nd=1031;
xA=1:Na; xB=1:Nb; xC=1:Nc; xD=1:Nd;
T=800; %period
pointsA=sin(2*pi*xA/T)+rand(size(xA))+xA/100;
pointsB=sin(2*pi*xB/T)+rand(size(xB))+xB/100+1;
pointsC=sin(2*pi*xC/T)+rand(size(xC))+xC/100+2;
pointsD=sin(2*pi*xD/T)+rand(size(xD))+xD/100+3;
plot(xA,pointsA,'r.',xB,pointsB,'g.',xC,pointsC,'b.',xD,pointsD,'m.')
grid on; xlabel('X'); ylabel('Y')
To fit all of them with a single line, combine all the data into a single x vector and a single y vector:
xAll=[xA,xB,xC,xD];
yAll=[pointsA,pointsB,pointsC,pointsD];
mdl=fitlm(xAll,yAll);
Plot the best fit line:
hold on; %hold existing plot
plot(xD,predict(mdl,xD'),'-k','Linewidth',2)
legend('A','B','C','D','Fit');
Try it.
  2 comentarios
William Rose
William Rose el 5 de Oct. de 2022
@Kogulan Paulmurugan, you can substitute in the values for pointsA, pointsB, xA, xB, etc., from your spreadsheet files. Since you did not provide those files, I made up some values.
William Rose
William Rose el 5 de Oct. de 2022
@Kogulan Paulmurugan, for quadratic or higher order model:
Na=629; Nb=663; Nc=548; Nd=1031;
xA=1:Na; xB=1:Nb; xC=1:Nc; xD=1:Nd;
T=800; %period
pointsA=sin(2*pi*xA/T)+rand(size(xA))+xA/100;
pointsB=sin(2*pi*xB/T)+rand(size(xB))+xB/100+1;
pointsC=sin(2*pi*xC/T)+rand(size(xC))+xC/100+2;
pointsD=sin(2*pi*xD/T)+rand(size(xD))+xD/100+3;
plot(xA,pointsA,'r.',xB,pointsB,'g.',xC,pointsC,'b.',xD,pointsD,'m.')
grid on; xlabel('X'); ylabel('Y')
xAll=[xA,xB,xC,xD];
yAll=[pointsA,pointsB,pointsC,pointsD];
mdl1=fitlm(xAll,yAll); %linear
mdl2=fitlm(xAll,yAll,'quadratic'); %quadratic
mdl4=fitlm(xAll,yAll,'poly4'); %4th order polynomial
Plot the best fit curves:
hold on; %hold existing plot
plot(xD,predict(mdl1,xD'),'-k','Linewidth',2)
plot(xD,predict(mdl2,xD'),'-y','Linewidth',5)
plot(xD,predict(mdl4,xD'),'-c','Linewidth',5)
legend('A','B','C','D','Linear','Quadratic','4th order','location','northwest');
Try it.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by