create equation from polyfit
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2016-2-16
编辑:Stephen23
2016-2-16
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?
回答(1 个)
Stephen23
2016-2-16
编辑:Stephen23
2016-2-16
>> 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'})

另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!