如何在Matlab中求解正切、负余切函数与圆形三个图像的全部交点
求解三类曲线交点的实现方案
核心逻辑
首先先通过代数推导确定交点分类:
- 联立正切函数
y = tan(x)与负余切函数y = -cot(x),推导可得sin²x + cos²x = 0,实数域下无实根,因此二者不存在交点,也不存在同时落在三条曲线上的公共点 - 仅需分别求解正切函数与圆的交点、负余切函数与圆的交点两类即可
可直接运行的Matlab代码
% 定义交点零值方程 eq_tan_circle = @(x) x.^2 + tan(x).^2 - 16; % 正切与圆交点:x² + tan²x = 16 eq_cot_circle = @(x) x.^2 + cot(x).^2 - 16; % 负余切与圆交点:x² + cot²x = 16 % ---------------- 搜索正切与圆的所有交点 ---------------- % 按正切奇点(pi/2 + k*pi)拆分搜索区间,加小偏移避开奇点 tan_intervals = [ -5*pi/2, -2*pi - 1e-3; -2*pi + 1e-3, -3*pi/2 - 1e-3; -3*pi/2 + 1e-3, -pi - 1e-3; -pi + 1e-3, -pi/2 - 1e-3; -pi/2 + 1e-3, -1e-3; 1e-3, pi/2 - 1e-3; pi/2 + 1e-3, pi - 1e-3; pi + 1e-3, 3*pi/2 - 1e-3; 3*pi/2 + 1e-3, 2*pi - 1e-3; 2*pi + 1e-3, 5*pi/2 ]; tan_points = []; for i = 1:size(tan_intervals,1) a = tan_intervals(i,1); b = tan_intervals(i,2); % 区间两端符号相反说明存在根,调用fzero求解 if sign(eq_tan_circle(a)) ~= sign(eq_tan_circle(b)) x_root = fzero(eq_tan_circle, [a,b]); tan_points = [tan_points; x_root, tan(x_root)]; end end % ---------------- 搜索负余切与圆的所有交点 ---------------- % 按负余切奇点(k*pi)拆分搜索区间,加小偏移避开奇点 cot_intervals = [ -3*pi + 1e-3, -2*pi - 1e-3; -2*pi + 1e-3, -pi - 1e-3; -pi + 1e-3, -1e-3; 1e-3, pi - 1e-3; pi + 1e-3, 2*pi - 1e-3; 2*pi + 1e-3, 3*pi - 1e-3 ]; cot_points = []; for i = 1:size(cot_intervals,1) a = cot_intervals(i,1); b = cot_intervals(i,2); if sign(eq_cot_circle(a)) ~= sign(eq_cot_circle(b)) x_root = fzero(eq_cot_circle, [a,b]); cot_points = [cot_points; x_root, -cot(x_root)]; end end % ---------------- 输出结果 + 可视化验证 ---------------- disp('正切函数与圆的交点坐标(x,y):') disp(tan_points) disp('负余切函数与圆的交点坐标(x,y):') disp(cot_points) % 原有绘图代码保留,新增交点绘制 hold on scatter(tan_points(:,1), tan_points(:,2), 60, 'red', 'filled', 'DisplayName','正切-圆交点') scatter(cot_points(:,1), cot_points(:,2), 60, 'green', 'filled', 'DisplayName','负余切-圆交点') legend('Location','best')
注意说明
- 区间里加的
1e-3偏移是为了避开三角函数的奇点,防止求解报错,需要更高精度可以把偏移值改到1e-6 - 如果需要过滤重复交点,可以对结果坐标加去重逻辑
- 求解得到的
tan_points和cot_points就是所有交点的坐标,可直接调用做后续计算
内容的提问来源于stack exchange,提问作者Jungle Zee
相关产品推荐
相关产品推荐

