Unable to convert expression into double array. Cannot use quiver.

3 次查看(过去 30 天)
I know the problem is I use a variable to assign the function but I don't know how to fix.
Hope anybody can help me.
Thank a lot.
Here is my code:
clc
close all
syms x y c
k = 5;
[x,y] = meshgrid(-3:1:3);
[P] = vector_inputP();
[Q] = (vector_inputQ(k));
kt = kttruongbaotoan(P,Q);
if kt == 1
F = tinhhamthe(P,Q);
F = expand(F);
disp(F);
[P] = vector_inputP();
[Q] = (vector_inputQ(k));
quiver(x,y,P,Q);
end
%% Question A
function [kt] = kttruongbaotoan(P,Q)
syms x y
if diff(P,y)==diff(Q,x)
fprintf('*>Truong vecto F = < Q , P > = ');
disp([Q,P]);
fprintf(' la truong bao toan. Ham the F(x,y) = ');
kt=1;
else
fprintf('*>Truong vecto F = < Q , P > = ');
disp([Q,P]);
fprintf('khong la truong bao toan\n');
kt=0;
end
end
%% Question B
function [F] = tinhhamthe(P, Q)
syms x y c
F1 = expand(int( P , x));
F2 = Q - diff(F1,y);
F2 = int(F2, y);
F = F2 + F1 + c;
end
%%
function [P] = vector_inputP(k)
syms x y
if k == 5
P = 2 * x - 3 * y;
elseif k == 6
P = exp(x) * cos(y);
elseif k == 7
P = exp(x) * sin(y);
elseif k == 8
P = 3 * x^2 - 2 * y^2;
elseif k == 9
P = y * exp(x) + sin(y);
elseif k == 10
P = x * y * cos(x*y) + sin(x*y);
elseif k == 11
P = log(y)+ 2 * x * y^3;
elseif k == 12
P = x * y * cosh(x*y) + sinh(x*y);
end
end
%%
function [Q] = vector_inputQ(k)
syms x y
if k == 5
Q = - 3*x + 4*y - 8;
elseif k == 6
Q = exp(x) * sin(y);
elseif k == 7
Q = exp(x) * cos(y);
elseif k == 8
Q = 4*x*y + 3;
elseif k == 9
Q = exp(x) + x * cos(y);
elseif k == 10
Q = x^2 * cos(x*y);
elseif k == 11
Q = 3 * x^2 * y^2 + x / y;
elseif k == 12
Q = x^2 * cosh(x*y);
end
end

回答(1 个)

VBBV
VBBV 2024-3-29
@Tai Nguyen, Use a different variable name for meshgrid outputs and use subs for symbolic expressions to replace with numeric arrays
syms x y c
k = 5;
[X,Y] = meshgrid(-3:1:3); % use a different variable name for meshgrid outputs
[P] = vector_inputP(k);
[Q] = (vector_inputQ(k));
kt = kttruongbaotoan(P,Q);
*>Truong vecto F = < Q , P > =
la truong bao toan. Ham the F(x,y) =
if kt == 1
F = tinhhamthe(P,Q);
F = expand(F);
disp(F);
[P] = vector_inputP(k);
[Q] = (vector_inputQ(k));
P = double(subs(P,{x,y},{X,Y}));
Q = double(subs(Q,{x,y},{X,Y}));
quiver(X,Y,P,Q);
end
%% Question A
function [kt] = kttruongbaotoan(P,Q)
syms x y
if diff(P,y)==diff(Q,x)
fprintf('*>Truong vecto F = < Q , P > = ');
disp([Q,P]);
fprintf(' la truong bao toan. Ham the F(x,y) = ');
kt=1;
else
fprintf('*>Truong vecto F = < Q , P > = ');
disp([Q,P]);
fprintf('khong la truong bao toan\n');
kt=0;
end
end
%% Question B
function [F] = tinhhamthe(P, Q)
syms x y c
F1 = expand(int( P , x));
F2 = Q - diff(F1,y);
F2 = int(F2, y);
F = F2 + F1 + c;
end
%%
function [P] = vector_inputP(k)
syms x y
if k == 5
P = 2 * x - 3 * y;
elseif k == 6
P = exp(x) * cos(y);
elseif k == 7
P = exp(x) * sin(y);
elseif k == 8
P = 3 * x^2 - 2 * y^2;
elseif k == 9
P = y * exp(x) + sin(y);
elseif k == 10
P = x * y * cos(x*y) + sin(x*y);
elseif k == 11
P = log(y)+ 2 * x * y^3;
elseif k == 12
P = x * y * cosh(x*y) + sinh(x*y);
end
end
%%
function [Q] = vector_inputQ(k)
syms x y
if k == 5
Q = - 3*x + 4*y - 8;
elseif k == 6
Q = exp(x) * sin(y);
elseif k == 7
Q = exp(x) * cos(y);
elseif k == 8
Q = 4*x*y + 3;
elseif k == 9
Q = exp(x) + x * cos(y);
elseif k == 10
Q = x^2 * cos(x*y);
elseif k == 11
Q = 3 * x^2 * y^2 + x / y;
elseif k == 12
Q = x^2 * cosh(x*y);
end
end

类别

Help CenterFile Exchange 中查找有关 Vector Fields 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by