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'})
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172883/image.png)
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!