Error: The size of X must match the size of U or the number of columns of U.

조회 수: 7 (최근 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
KSSV
KSSV 2017년 5월 14일
Dimensions of X,Y and u,v should be same.
geometry geometry
geometry geometry 2017년 5월 14일
could you please fix the code? (I'm new to matlab) thanks;

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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);
  댓글 수: 3
Star Strider
Star Strider 2017년 5월 14일
This was a previous attempt by OP (in another Question) that apparently did not work.
See How to use gradient (link) for context.
OP is using another approach here, so it is best to follow that approach.
geometry geometry
geometry geometry 2017년 5월 15일
Thanks; sorry to make a similar post.
after you changed the name of that post I thought someone deleted that post! hence I made this post.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Star Strider
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);

카테고리

Help CenterFile Exchange에서 Vector Fields에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by