필터 지우기
필터 지우기

Why doesn't matlab recognize my variables?

조회 수: 4 (최근 30일)
Espiritu Cia
Espiritu Cia 2020년 5월 12일
편집: John D'Errico 2020년 5월 13일
clear;
[x y] = meshgrid( -0.4:0.02:0.4 , -0.4:0.02:0.4 );
Xei = -0.3 ; Xed = 0.3 ; Q = 5 e (- 5) ; Yv = 1 e (- 6) ;
epsilon0 = 8.854e(-12) ;
T = 4 * pi * epsilon0 ;
V = zeros(size(x));
for i= Xei:Xed
A = log (-(x-i)+(sqrt((x-i)^2)+(y + Yv)^2)) ;
V = (1 / T)* (Q / (Xei - Xed)) * A ;
Vf = V + V(i);
end
%Visualizacion
hold on
surf (x, y, Vf); shading interp;
xlabel('x[m]'); ylabel('y[m]'); zlabel('V[V]');
contour3(x,y,V,10,'r');
hold off ;
And why does that give me ans = 21
The script doesn't recognize any variables. AT ALL! :( theres only only 'ans' in the workspace

답변 (2개)

John D'Errico
John D'Errico 2020년 5월 13일
편집: John D'Errico 2020년 5월 13일
What is sketchy is the invalid MATLAB syntax you have created in multiple lines.
When you write things like this:
Q = 5 e (- 5)
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error.
To construct matrices, use brackets instead of parentheses.
It is invalid syntax. Therefore it does not execute the rest of the code. If you want to create the number 5e-5, that is how it should be written.
As well,
Yv = 1 e (- 6)
is invalid MATLAB syntax.
epsilon0 = 8.854e(-12) ;
Again, invalid syntax. That is not how you create the number 8.854e-12.
This next one is interesting:
for i= Xei:Xed
Since Xei is -0.3 and Xed is 0.3, the loop won't go very far, since the default loop index is 1.
But worse, then later on, you use i as an index into the array V. Since the value of i will be -0.3 on the only pass through the loop that will ever happen, you will get another error about an invalid subscript.
The next one is another case of invalid symtax. The space between log and the open paren will cause problems.
A = log (-(x-i)+(sqrt((x-i)^2)+(y + Yv)^2)) ;
Effectively, MATLAB does not recognize variables that were never validly created. When errors occur, these are FATAL errors, in the sense that MATLAB stops trying to execute code. When MATLAB stops executing code nothing is ever created. Therefore variables don't exist. It is not a question of recognition, but simply existence.

Ornit
Ornit 2020년 5월 12일
Use a different variable name, not Q
Use [x,y], not [x y]
Use Yv = 1E-6 format instead of the ones you use.
The index i is not an integer, therefore V(i) will crash, but this you can debug.
  댓글 수: 1
Espiritu Cia
Espiritu Cia 2020년 5월 13일
Thanks for the answer, although I tried it out with no luck. I sent the code to a friend who has the same version of MATLAB and she got errors, but the program succesfully recognized each variable.
There's maybe something sketchy about my program, and I have no clue on what's going on.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by