How to solve polynomial for a number of values x?

I have a data set with x and y values. (x=discharge and y = water level). Now I made a 3rd order polynomial fit and I need the value x given y for y = 0.00, 0.05, ..., 14.00 How do I solve the polynomial for x and get the x values in a nice matrix?
x = [5965 7867 9459 11763 13881 14794 16000 16619 20000] y = [3.7 6.34 7.04 7.89 8.61 8.91 9.25 9.42 10.19]
plot(x,y,'o')
[p,~,mu] = polyfit(x,y,3); y2 = polyval(p,x,[],mu); hold on plot(x,y2) hold off
h = [0.00:0.05:14.00]; %h are the values of y for which I want to know x
so the result should be a matrix like: [y1 x1 y2 x2 y3 x3 etc]
I'm only interested in real solutions

 채택된 답변

Torsten
Torsten 2015년 2월 16일

0 개 추천

x0=1;
for i=1:length(h)
X(i)=fzero(@(x)polyval(p,x)-h(i),x0);
x0=X(i)
end
Best wishes
Torsten.

댓글 수: 3

That script works, however the values do not correspond with the initial data. Is this caused by my part of the script or something else?
Torsten
Torsten 2015년 2월 16일
Maybe you have to work with the scaled values:
X(i)=fzero(@(x)polyval(p,x,[],mu)-h(i),x0);
?
Best wishes
Torsten.
You, Sir, are my hero for today. Many thanks.

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

추가 답변 (1개)

John D'Errico
John D'Errico 2015년 2월 16일

0 개 추천

Nothing personal, but that cubic polynomial is a relatively poor fit.
See that the cubic has some lack of fit, but that far more importantly, in extrapolation, it starts to do some nasty stuff, that does not seem indicated by your data.
Whereas, with essentially no options chosen at all, my SLM toolbox provides this result:
slm = slmengine(x,y,'plot','on','knots',[3000:3000:24000]);
Which seems to represent the data a bit more cleanly.
The inverse function is also easily done in one call to slmeval.

댓글 수: 1

Jelle van Zuijlen
Jelle van Zuijlen 2015년 2월 16일
편집: Jelle van Zuijlen 2015년 2월 16일
Can you explain how you achieved that? What is this slmengine ? I'm new to Matlab so I don't know about this
Edit: I've downloaded your slmengine package, will check if I can come to this fit.
Thanks anyways

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

카테고리

도움말 센터File Exchange에서 Polynomials에 대해 자세히 알아보기

질문:

2015년 2월 16일

편집:

2015년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by