Plot graphing equation using standard plotting function in Matlab

To get a graphical representation of fluid behavior, it is common practice to draw streamlines.

For a given two-dimensional fluid with velocity components u = Kx and v = -Ky (where K is a constant, for example: K = 5), the streamline equation can be obtained by integrating the components of the flow velocity field as follows

Equation optimization: ∫dx / u = ∫dy / v

The solved equation looks like this: A = B + C (where A is the solution of the first integral, B is the solution of the second integral, C is the constant of integration).

Once we achieve this, we can start drawing the simplification line by simply assigning a value to C, for example: C = 1, and plotting the resulting equation. This will create a single line of optimization, so to get more of them, you need to repeat this last step, assigning a different C value each time.

I have successfully plotted the streamlines of this particular flow by allowing matlab to integrate the equation in symbolic form and using ezplot

to generate the graphic as follows:

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]);

      

This is the result:

The problem is that for some specific areas the flow is ezplot

not precise enough and does not handle features (asymptotes, etc.) very well. Therefore, the standard "numeric" plot

seems to be desirable in order to get the best visual result.

The challenge here is to convert the symbolic optimized solution to an explicit expression that is compatible with the standard function plot

.

I tried to do it like this using subs

and solve

without any success (Matlab throws an error).

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

      

This is the error that is displayed in the command window:

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.

      

So how do you do it?

+3


source to share


1 answer


As you use it subs

many times, it is matlabFunction

more efficient. You can use C

as a parameter and decide for y

both for x

and for C

. Then the loop for

is much faster:



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

      

0


source







All Articles