使用Matlab中的标准绘图函数绘制符号方程

时间:2015-04-30 00:31:58

标签: matlab plot symbolic-math

为了获得流体行为的图形表示,通常的做法是绘制流线图。

对于给定的具有速度分量的二维流体,u = K x且v = -K y(其中K是常数,例如:K = 5),流线方程可以是得到的流速场分量积分如下:

流线方程:∫dx/ u =∫dy/ v

求解的方程式如下:A = B + C(其中A是第一个积分的解,B是第二个积分的解,C是积分常数)。

一旦我们实现了这一点,我们就可以通过简单地为C赋值来开始绘制流线图,例如:C = 1,并绘制得到的等式。这会产生一个流线,所以为了获得更多的流线,你需要迭代这最后一步,每次都分配不同的C值。

我已成功绘制了此特定流程的流线,方法是让matlab integrate符号化,并使用ezplot生成图形,如下所示:

syms x y

K = 5; %Constant.

u = K*x; %Velocity component in x direction.
v = -K*y; %Velocity component in y direction.

A = int(1/u,x); %First integral.
B = int(1/v,y); %Second integral.

for C = -10:0.1:10; %Loop. C is assigned a different value in each iteration.
    eqn = A == B + C; %Solved streamline equation.
    ezplot(eqn,[-1,1]); %Plot streamline.
    hold on;
end

axis equal;
axis([-1 1 -1 1]);

结果如下:

问题在于,流ezplot的某些特定区域不够准确,并且不能很好地处理奇点(渐近线等)。这就是标准"数字" plot似乎是可取的,以获得更好的视觉输出。

这里的挑战是将符号流线解决方案转换为与标准plot函数兼容的显式表达式。

我尝试这样做,使用subssolve但没有成功(Matlab抛出错误)。

syms x y

K = 5; %Constant.

u = K*x; %Velocity component in x direction.
v = -K*y; %Velocity component in y direction.

A = int(1/u,x); %First integral.
B = int(1/v,y); %Second integral.

X = -1:0.1:1; %Array of x values for plotting.

for C = -10:0.1:10; %Loop. C is assigned a different value in each iteration.
    eqn = A == B + C; %Solved streamline equation.
    Y = subs(solve(eqn,y),x,X); %Explicit streamline expression for Y.
    plot(X,Y); %Standard plot call.
    hold on;
end

这是命令窗口中显示的错误:

Error using mupadmex
Error in MuPAD command: Division by zero.
[_power]

Evaluating: symobj::trysubs

Error in sym/subs>mupadsubs (line 139)
G =
mupadmex('symobj::fullsubs',F.s,X2,Y2);

Error in sym/subs (line 124)
G = mupadsubs(F,X,Y);

Error in Flow_Streamlines (line 18)
Y = subs(solve(eqn,y),x,X); %Explicit
streamline expression for Y.

那么,应该怎么做呢?

1 个答案:

答案 0 :(得分:0)

由于您多次使用subsmatlabFunction效率更高。您可以使用C作为参数,并根据yx求解C。然后for循环非常快:

syms x y

K = 5; %Constant.

u = K*x; %Velocity component in x direction.
v = -K*y; %Velocity component in y direction.

A = int(1/u,x); %First integral.
B = int(1/v,y); %Second integral.

X = -1:0.1:1; %Array of x values for plotting.

syms C % C is treated as a parameter
eqn = A == B + C; %Solved streamline equation.

% Now solve the eqn for y, and make it into a function of `x` and `C`
Y=matlabFunction(solve(eqn,y),'vars',{'x','C'})

for C = -10:0.1:10; %Loop. C is assigned a different value in each iteration.
    plot(X,Y(X,C)); %Standard plot call, but using the function for `Y`
    hold on;
end