Clear Filters
Clear Filters

create equation from polyfit

4 views (last 30 days)
Jakob
Jakob on 16 Feb 2016
Edited: Jakob on 16 Feb 2016
Hi,
Just got matlab installed. You can only get that far in Excel ;)
The problem I have is that I am not completely into the syntax in matlab yet so I would really appreciate some help.
I have a data set in an csv file which represent a curve. I read them from matlab into two arrays. x's and y's. Then I use polyfit and find a good fit.
The issue I have now is that how do I use the results from polyfit as an equation. First I assumed that I had define a variable x using 'syms x' and then simply use eq1=polyval(p,x); (where p is the array with the constants from the polyfit). This doesnt work. Now I am thinking simply using P*[x^n x^n-1 .. 1]' but this is a bit of a workaround since then I have to create the variable x vector. Is there a simpler way?
Regards
Jakob
  2 Comments
Stephen23
Stephen23 on 16 Feb 2016
Edited: Stephen23 on 16 Feb 2016
What do you want to do with the equation? If you just need to plot the data (or do some other operations) then you do not need the equation, but can use the output of polyfit: the polyfit documentation has examples of how to do this.
Why exactly do you need this equation?
Jakob
Jakob on 16 Feb 2016
I would like to solve it for different values. In this case x is time and the curve is rising from y=20 to y=60. An example would be that I would like to know how long time it takes to reach y=30.
For this I would use solve(eqn==30,x) and then use this result later.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 16 Feb 2016
Edited: Stephen23 on 16 Feb 2016
A pure numeric solution is very easy using polyval and fzero (no syms needed):
>> X = 1:9;
>> Y = X.^2 + 8*rand(1,numel(X))-4;
>> P = polyfit(X,Y,2);
>> T = fzero(@(x)polyval(P,x)-30,X([1,end]))
T =
5.3825
And it is easy to plot too:
>> plot(X,Y,'or', X,polyval(P,X),'xb')
>> hold on
>> plot(T([1,1]),get(gca,'Ylim'),'g')
>> plot(X([1,end]),[30,30],'g')
>> legend({'original','polyfit','solved'})
  1 Comment
Jakob
Jakob on 16 Feb 2016
Edited: Jakob on 16 Feb 2016
ok - thats look nice.
I have though now accepted that maybe I should just use the data set as they are and then dont use polyfit.
Instead I simply use:
[c index]=min(abs(y1-30)) ;
x1(index)
This give the time value that I am interested in.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!