create equation from polyfit
显示 更早的评论
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
回答(1 个)
>> 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'})

类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!