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

27 ビュー (過去 30 日間)
Kenny Kaasgaard Thomsen 2018 年 11 月 27 日
コメント済み: Brendan Hamm 2018 年 11 月 28 日
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
Picture 2: Is the linear regression i made on the second plot, which is also as i desire.
Picture 3: Illustrates the desired plot i need for my project
(Of course with my own values from the expiriments)
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

サインインしてコメントする。

### 採用された回答

Brendan Hamm 2018 年 11 月 27 日
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 件のコメント表示非表示 1 件の古いコメント
Brendan Hamm 2018 年 11 月 28 日
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.

サインインしてコメントする。

### カテゴリ

Find more on Linear Predictive Coding in Help Center and File Exchange

R2018b

### Community Treasure Hunt

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

Start Hunting!

Translated by