Create function from polyfit curve

조회 수: 10 (최근 30일)
Megan Miller
Megan Miller 2020년 9월 10일
댓글: Ameer Hamza 2020년 9월 10일
I'm trying to make an equation that correlates engine speed with pressure. My goal is to be able to input an rpm into a function and have it spit out a pressure. I have a plot of the data and a polyfit curve.
Here is the relevant snipit from my script:
[p, S, mu] = polyfit(Input_Speed, press, 7);
v = polyval(p, Input_Speed, S, mu);
figure(3)
plot(Input_Speed, press)
hold on
plot(Input_Speed, v)
xlabel("Speed (rpm)")
ylabel("Pressure (psi)")
legend('Filtered Data', 'Fit Curve')
What is the best way of doing this? A lookup table? Use poly2sym to turn the polyfit coefficients into an equation? I'm open to any suggestions. Thank you!
  댓글 수: 1
KSSV
KSSV 2020년 9월 10일
Already you have coefficients in hand..you can use polyval to get the output values for given input. poly2sym shows you a polynomial in x as symbolic class but you need to substitue and get the value again.

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 10일
An easy solution is to wrap the result of polyfit into an anonymous function
[p_coff, S, mu] = polyfit(Input_Speed, press, 7);
P = @(Input_Speed) polyval(p_coff, Input_Speed, S, mu);
Then you can call it like a function, without the need to specify p_coff, S, and mu each time
figure(3)
plot(Input_Speed, press)
hold on
plot(Input_Speed, P(Input_Speed))
xlabel("Speed (rpm)")
ylabel("Pressure (psi)")
legend('Filtered Data', 'Fit Curve')
  댓글 수: 2
Megan Miller
Megan Miller 2020년 9월 10일
That did the trick. I completely spaced making Input_Speed the variable instead of the default of x. I used p_coff, S, and mu just to get rid of a warning message saying the data was unfit for polyfit but your method got rid of that too. Thank you!!
Ameer Hamza
Ameer Hamza 2020년 9월 10일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by