Error: The size of X must match the size of U or the number of columns of U.
6 次查看(过去 30 天)
显示 更早的评论
What's the problem with this code? when I run it the following Error occurs:
Error using quiver
The size of X must match the size of U or the number of columns of U.
syms x y;
f=input('enter function: ','s');
f = symfun(eval(f), [x y]);
u=-diff(f,x);
v=-diff(f,y);
[X,Y]=meshgrid(-10:.5:10);
quiver(X,Y,u,v);
2 个评论
采纳的回答
Stephen23
2017-5-14
编辑:Stephen23
2017-5-14
There is no need to make the code slower and more complicated by using syms and eval. Exactly as I wrote in my answer to your related question, simple numeric calculations will do everything you need:
% str = input('enter a function in x and y: ','s');
str = 'log(x^2+y^2)';
fun = str2func(['@(x,y)',str]);
[X,Y] = meshgrid(-10:.51:10);
[DX,DY] = gradient(arrayfun(fun,X,Y));
quiver(X,Y,DX,DY,5);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176183/image.png)
3 个评论
Star Strider
2017-5-14
This was a previous attempt by OP (in another Question) that apparently did not work.
OP is using another approach here, so it is best to follow that approach.
更多回答(1 个)
Star Strider
2017-5-14
Remember that ‘u’ and ‘v’ are also functions of (x,y), so use them as ‘u(x,y)’ and ‘v(x,y)’. With one change (changing the step in the meshgrid vector to 0.51 to prevent ‘division by zero’ errors), this works:
syms x y;
% f=input('enter function: ','s');
f = 'log(x^2+y^2)';
f = symfun(eval(f), [x y]);
u=-diff(f,x);
v=-diff(f,y);
[X,Y]=meshgrid(-10:.51:10);
quiver(X,Y,u(X,Y),v(X,Y),5);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!