Why doesn't matlab recognize my variables?
12 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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.
0 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!