1
votes

I used polyfit to find the best fit through my X and Y data.

p = polyfit(x,y,4)

After that I used polyval to make a line with the polyfit with other X data

a = [-5 : 0.1 : 15]

line = polyval(p,a)

When I plot this line and when I look at the data, I see that it has intersections with the x-axis. But there is not an exact y=0

My question is, how do I find the (there are 2) intersection points with the x-axis, or atleast the x where y is the closest to 0?

Thanks in advance!

1

1 Answers

2
votes

First, don't use line as a variable name, it is a MATLAB function you are shadowing and won't be able to access.

p = polyfit(x,y,4);
a = [-5 : 0.1 : 15];
b = polyval(p,a);

To get the intersection with the x axis, you are essentially looking for the roots of the polynomial, i.e. when y=0 and there is a function just for that:

r = roots(p);