using imaginary notation gives an error (-J)

Hi,
I am fairly new to matlab but used it years ago and have forgotten a lot.
I have downloaded an m file which should work but provides an error when encountering the following line of code.
u2=-J*u2.*q1; and there are two carrots underneath the "q1".
Can someone provide a suggestion as to how to interpret this error?
Thank You
Tom

댓글 수: 4

The imaginary unit is i, j, 1i, or 1j, not J:
-i
ans = 0.0000 - 1.0000i
-j
ans = 0.0000 - 1.0000i
-J
Unrecognized function or variable 'J'.
"Can someone provide a suggestion as to how to interpret this error"
You did not give us any error message, so interpreting it is a challenge. My guess is some size or type mismatch. The carets indicate its likely location.
What does the error message say?
From the perspective of the person who wrote the original code, why would they choose to upload an m-file online that does not work in the first place?
Moreover, for anyone already familiar with how imaginary numbers work in MATLAB, if j is intended to represent the imaginary unit, it seems very strange to write -j*u2.*q1 instead of using the conventional form -u2.*q1*1j, which avoids any potential ambiguity of j.
Finally, is the m-file a helper function that expects J to be computed beforehand or properly initialized? After all, the expression u2 = -J*u2.*q1 may indicate that u2 is being computed iteratively.
J = asin(2)
J = 1.5708 - 1.3170i
u2 = 1;
q1 = 3;
u2 = -J*u2.*q1
u2 = -4.7124 + 3.9509i
dpb
dpb 대략 2시간 전
편집: dpb 10분 전
I think the subject line of the Q? about complex numbers plus the fact the interpreter indicated it failed on the variable q1 a misinterpretation of the error here; as @Stephen23 notes, the builtin imaginary variable functions are lower case, not upper, so the J in "-J" is NOT sqrt(-1) unless J has previously been defined in the m-file to have aliased it (or it's a script, not a function and the same has been done in the workspace).. I would venture the likelihood of either of those is quite low.
Instead, it's highly likely it is q1 that isn't defined, but as others have noted, without the error message in context to see, it's all purely conjecture. Showing us the code itself would also likely have allowed to see what might be wrong.
Thank you for the reply and for everyone's help.
I did find that "q1" had been commented out, for some reason.
%q1=cos(2*pi*(0:19)*1q1/2)';
I uncommented out the q1 and the program is now working fine.

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

 채택된 답변

Steven Lord
Steven Lord 대략 5시간 전
If by "carrots" you mean an orange squiggle underline in the Editor representing a Code Analyzer warning, and if just the "q1" section of that line is underlined, I suspect the error is unrelated to the presence of J versus j.
If you look at the attached example, the q1 section of line 4 is underlined in the Editor.
dbtype example2184049.m
1 function [u2, q1] = example2184049 2 J = 2; 3 u2 = [3 4]; 4 u2 = -J*u2.*q1; 5 q1 = [5 6];
What is Code Analyzer flagging on that line?
issues = codeIssues('example2184049.m')
issues =
codeIssues with properties: Date: 12-Jun-2026 15:18:29 Release: "R2026a" Files: "/users/mss.system.rnghr/example2184049.m" CodeAnalyzerConfiguration: "active" Issues: [1×10 table] SuppressedIssues: [0×11 table] Issues table preview Location Severity Fixability Description CheckID LineStart LineEnd ColumnStart ColumnEnd FullFilename __________________ ________ __________ ______________________________________________ _______ _________ _______ ___________ _________ __________________________________________ "example2184049.m" warning manual "Variable might be used before it is defined." NODEF 4 4 13 14 "/users/mss.system.rnghr/example2184049.m"
The Description states that the variable might be used before it's defined. If you look more closely at the file you can see that the message is correct: q1 is used in the computation of u2 on line 4, but it's not defined until line 5. So if I run the file it throws an error.
example2184049
Unrecognized function or variable 'q1'.

Error in example2184049 (line 4)
u2 = -J*u2.*q1;
If that's the error you receive, the solution is to define q1 before the code that uses it. In this case, if you were to swap lines 4 and 5 in the example it would work.
Of course, if that's not what you mean by "carrots", please describe in more detail (or attach a picture of the "carrots") and give the full and exact text of the error message.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2025b

태그

질문:

대략 17시간 전

댓글:

대략 4시간 전

Community Treasure Hunt

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

Start Hunting!

Translated by