Why doesn't matlab recognize my variables?

10 views (last 30 days)
Espiritu Cia
Espiritu Cia on 12 May 2020
Edited: John D'Errico on 13 May 2020
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

Answers (2)

John D'Errico
John D'Errico on 13 May 2020
Edited: John D'Errico on 13 May 2020
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 on 12 May 2020
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 Comment
Espiritu Cia
Espiritu Cia on 13 May 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!