create equation from polyfit
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 16 de Feb. de 2016
Editada: Stephen23
el 16 de Feb. de 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?
Respuestas (1)
Stephen23
el 16 de Feb. de 2016
Editada: Stephen23
el 16 de Feb. de 2016
>> 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)
1 comentario
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!