How can I find the intersection between a curve created using function 'polyfit' and a circle?

7 visualizzazioni (ultimi 30 giorni)
It is sought to find the intersection of a curve that is created using function 'polyfit' and a circle.
How can I find the intersection between a curve created using function 'polyfit' and a circle?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 6 Set 2021
Altough such a built-in function does not exist in MATLAB, one can leverage function 'fsolve' for this purpose, please see the following documentation page for more information accordingly,
In principle one needs to construct a nonlinear function that represents the subtraction of the polynomial function defined by polyfit and an explicit description of the circle, and subsequently find its root. Since the circle does not have a unique explicit representation, one would need to perform the intersection separately for the upper and the lower parts of the circle with respect to the y-axis. Given a center point of the circle 'pt' and a radius, an explicit representation of the upper half of the circle is represented by the following equation:
y_circ_pos = sqrt(r^2 - (x_circ_pos - pt(1)).^2) + pt(2);
 
where 'x_circ_pos' are the x-coordinates provided to the explicit function above. Using the example 'polyfit' function from our documentation page below,
and a circle centered at the third interpolation point from the sample data, one can leverage function 'fsolve' to compute the intersection of the curve defined by function 'polyfit' and the unit circle for the upper half of the circle, see the code snippet below,
%% Set up a linear space for the creation of the curve using function 'polyfit'
x = linspace(0,4*pi,10);
y = sin(x);
%% Use polyfit to fit a 7th-degree polynomial to the points.
global p r pt
p = polyfit(x,y,7);
%% Evaluate the polynomial on a finer grid and plot the results.
x1 = linspace(0,4*pi);
y1 = polyval(p,x1);
figure
plot(x,y,'o')
hold on
plot(x1,y1)
%% Create the upper half of the circle with respect to y-axis centered at the third interpolation point above
pt = [x(3), y(3)];
r = 1;
x_circ_pos = linspace(x(3) - r,x(3) + r, 100);
y_circ_pos = sqrt(r^2 - (x_circ_pos - pt(1)).^2) + pt(2);
plot(x_circ_pos,y_circ_pos,'-')
%% Find the intersection between the curve defined by function 'polyfit' and the circle
x0 = pt(1);
x = fsolve(@fun, x0);
y = polyval(p,x);
plot(x,y,'ro')
hold off
axis equal
%% Nonlinear function defining the intersection
function y = fun(x)
global p r pt
y = 0;
for i = 1:length(p)
y = y + p(i)*x^(length(p) - i);
end
y = y - sqrt(r^2 - (x - pt(1)).^2) - pt(2);
end
The results of the latter script are shown in the figure below where the intersection is highlighted using a red circle,
One can then also use the following function,
y_circ_pos = - sqrt(r^2 - (x_circ_pos - pt(1)).^2) + pt(2);
to obtain the intersection of the lower half of the circle with respect to y-axis and the curve created by means of function 'polyfit'.

Più risposte (0)

Categorie

Scopri di più su Animation in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Prodotti


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by