Why doesn't matlab recognize my variables?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
John D'Errico
le 13 Mai 2020
Modifié(e) : John D'Errico
le 13 Mai 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.
0 commentaires
Ornit
le 12 Mai 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.
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!