For example i have 5 point like this,
(1,1) (2,-1) (3,2) (4,-2) (5,2)
Now,
How can I do this??
To fit a polynom to given datapoints you can use polyfit(x,y,n)
where x
is a vector with points for x
, y
is a vector with points for y and n
is the degree of the polynom. See example at Mathworks polyfit documentation
In your case:
x=[1,2,3,4,5];
y=[1,-1,-2,-2,2];
n=3;
p = polyfit(x,y,n)
And then to plot, taken from example
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
Or, to make a prettier plot of the polynomial (instead of above plot)
xx=0:0.1:5;
yy = erf(xx);
f = polyval(p,xx);
plot(x,y,'o',xx,f,'-')
If you are not sure what a good fit would be and want to try out different fit, use the curve fitting toolbox, cftool
. You will need to create two vectors with x
and y
coordinates and then you can play around with cftool
.
Another option would be to use interp1
function for interpolation. See matlab documentation for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With