How do I make a polynomial regression of data points and afterwards calculate the prediction accuracy

5 Ansichten (letzte 30 Tage)
I have this code (below at the end of the question) which visualizes the flowrate (Results.Flow) as a function of the pressure difference (Results.Tryk) and I made a linear regression line on this one.
After that, I validated the models precission by calculating the coefficient of determination with equation "Rsq1", which gave me a precision of about 99 percent.
The second part makes a graph of the flowrate(Results.Flow) as a function of the pressure difference squared. Again i made i linear regression to illustrate the mean value along the way which gave me a prediction precision of 83 percent (Rsq2). But i also need a polynomial regression to see if that makes a better fit for the graph. (Because the best fitted line concludes if the flow is laminar or turbulent in my hydraulic system)
So how do make that polynomial regression, and afterwards calculate the model accuracy like I did for the other two lines?
I've already tried some things with polyfit command, but sadly I couldn't make it work.
(I have included my workspace as an attachment.)
THE LINE MUST TO GO THROUGH ORIGIN (0,0)
I also attached some pictures of the desired result i seek.
Picture 1: Is the first plot, which is perfectly as i want it
1.PNG
Picture 2: Is the linear regression i made on the second plot, which is also as i desire.
2.PNG
Picture 3: Illustrates the desired plot i need for my project
(Of course with my own values from the expiriments)
3.PNG
format long
ResDivFlow = Results.Tryk\Results.Flow
yCalc1 = ResDivFlow*Results.Tryk;
scatter(Results.Tryk,Results.Flow,'.')
hold on
grid on
plot(Results.Tryk,yCalc1)
xlabel('\DeltaP [Pa]')
ylabel('Q [m^3/s]')
title('Flowrate som funktion af trykforskellen')
legend('Datapunkter','Tendenslinje for gennemsnitligt flow','Location','northwest')
Rsq1 = 1 - sum((Results.Flow - yCalc1).^2)/sum((Results.Flow- mean(Results.Flow)).^2)
hold off
PressureDiffTurb = sqrt(Results.Tryk)
ResDivFlow = PressureDiffSquared\Results.Flow
yCalc2 = ResDivFlow*PressureDiffSquared;
scatter(PressureDiffTurb,Results.Flow,'.')
hold on
grid on
plot(PressureDiffTurb,yCalc2)
xlabel('sqrt(\DeltaP) [sqrt(Pa)]')
ylabel('Q [m^3/s]')
title('Flowrate som funktion af kvadraten af trykforskellen')
legend('Datapunkter','Lineær tendenslinje for gennemsnitligt flow','Location','northwest')
Rsq2 = 1 - sum((Results.Flow - yCalc2).^2)/sum((Results.Flow- mean(Results.Flow)).^2)
hold off

Akzeptierte Antwort

Brendan Hamm
Brendan Hamm am 27 Nov. 2018
I notice a few issues. Based on the second line of your portion, I assume that you mean to call the variable PressureDiffSquared. Furthermore, if you want to fit a function of the square, you probably want to quare the value and not take the square root.
PressureDiffTurb = sqrt(Results.Tryk)
ResDivFlow = PressureDiffSquared\Results.Flow
So, this should be:
PressureDiffSquared = Results.Tryk.^2
ResDivFlow = PressureDiffSquared\Results.Flow
Next, this fits a coeeficient for only the quadratic term, of the form . Likely you will also want to have a linear term in this model as well to fit the function,
PressureDiffSquared = [Results.Tryk,Results.Tryk.^2]
ResDivFlow = PressureDiffSquared\Results.Flow
Now, we have a vector of both beta coefficients, so we need to take this into account in the Lin Alg. later on. In particular on the line:
yCalc2 = ResDivFlow*PressureDiffSquared;
you would need to convert this to:
yCalc2 = PressureDiffSquared*ResDivFlow;
I believe this will generate the plot you are looking for.
  2 Kommentare
Kenny Kaasgaard Thomsen
Kenny Kaasgaard Thomsen am 28 Nov. 2018
Thanks for the answer.
About the variable "PressureDiffSquared", I think the misunderstanding is just about the variable name. Because I want to calculate the square root as I did, but didn't think about that squared means to the second power. So it's just a language misunderstanding between my danish and my english, because these are called two very different things in danish :-)
But the rest of the answer helped me a lot, so thanks a lot for that.
Brendan Hamm
Brendan Hamm am 28 Nov. 2018
Glad to hear. The only other thing I would consider, is if you have the statistics and machine learning toolbox, it would be considerably easier to fit the model and get the R^2 value using the fitlm function.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by