这个答案几乎适用于任何可以在MATLAB中表达为隐式曲面/曲线的问题.我将在椭圆上演示它.
精简版:
A = [5 4; 4 5]
c = [1; 2]
elFunc = @(A11,A22,A12,A21,c1,c2,x,y) (c1-x).*(A11*(c1-x)+A21*(c2-y))+(c2-y).*(A12*(c1-x)+A22*(c2-y))-1
ezplot(@(x,y) elFunc(A(1,1),A(2,2),A(1,2),A(2,1),c(1),c(2),x,y), [0 2 0 4])
Run Code Online (Sandbox Code Playgroud)
长版:
椭圆可以以其最一般的形式(对于任何维度)隐式写入
(x-c)'A(x-c) = 1 or (x-c)'A(x-c)-1 = 0
Run Code Online (Sandbox Code Playgroud)
其中x,c在R ^ n中,A是nxn矩阵.
为了将其转换为MATLAB可以使用的形式,我们可以使用符号工具箱.对于2D椭圆,我们写道:
syms A11 A12 A21 A22 c1 c2 x y real
impl = ([x y]-[c1 c2])*[A11 A12; A21 A22]*([x;y]-[c1;c2])-1
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
(c1 - x)*(A11*(c1 - x) + A21*(c2 - y)) + (c2 - y)*(A12*(c1 - x) + A22*(c2 - y)) - 1
Run Code Online (Sandbox Code Playgroud)
我们不再需要符号工具箱了,所以我们只需复制字符串,通过添加点运算符版本对其进行矢量化并将其转换为函数
elFunc = @(A11,A22,A12,A21,c1,c2,x,y) (c1-x).*(A11*(c1-x)+A21*(c2-y))+(c2-y).*(A12*(c1-x)+A22*(c2-y))-1
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用ezplot绘制曲线.ezplot假设当你给它一个函数句柄时,它需要求解func = 0所以我们的曲线已经由隐式格式的elFunc描述.我们要做的就是定义我们希望ezplot尝试绘制曲线的域.以下示例演示了结果:
A = [5 4; 4 5]
c = [1; 2]
elFunc = @(A11,A22,A12,A21,c1,c2,x,y) (c1-x).*(A11*(c1-x)+A21*(c2-y))+(c2-y).*(A12*(c1-x)+A22*(c2-y))-1
ezplot(@(x,y) elFunc(A(1,1),A(2,2),A(1,2),A(2,1),c(1),c(2),x,y), [0 2 0 4])
Run Code Online (Sandbox Code Playgroud)
