Illegal use of reserved keyword "else".

조회 수: 9 (최근 30일)
Craig Johnson
Craig Johnson 2021년 12월 2일
편집: DGM 2021년 12월 2일
function q_P = ParticularSolution (K,M,f_0,t,w_R,roadflag)
Q=[[-K,-w_R*M,w_R*M,-K]
else roadflag==1
s_1==-f_0./K
s_2==0/K
q_P==s_1+s_2*t
elseif roadflag==2
q_P==s_1*sin(w_R*t)+s_2*cos(w_R*t)
s_1==s_2==F./Q
end
  댓글 수: 4
Chunru
Chunru 2021년 12월 2일
What are your edited code and the error message?
Craig Johnson
Craig Johnson 2021년 12월 2일
function q_P = ParticularSolution (K,M,f_0,t,w_R,roadflag)
Q=[[-K,-w_R*M,w_R*M,-K]
if s_1==-f_0./K
s_2==0/K
q_P==s_1+s_2*t
elseif roadflag==2
q_P==s_1*sin(w_R*t)+s_2*cos(w_R*t)
s_1==s_2==F./Q
end
end

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

답변 (2개)

KSSV
KSSV 2021년 12월 2일
편집: KSSV 2021년 12월 2일
function q_P = ParticularSolution (K,M,f_0,t,w_R,roadflag)
Q=[-K,-w_R*M,w_R*M,-K] ;
if roadflag==1
s_1==-f_0./K
s_2==0/K
q_P==s_1+s_2*t
else roadflag==2
q_P==s_1*sin(w_R*t)+s_2*cos(w_R*t)
s_1==s_2==F./Q
end
  댓글 수: 3
KSSV
KSSV 2021년 12월 2일
Where are you using it? Show us the full code which you are using and throwing error.
KSSV
KSSV 2021년 12월 2일
Editted the code....there is a typo error extra brace.

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


DGM
DGM 2021년 12월 2일
편집: DGM 2021년 12월 2일
Start with
function q_P = ParticularSolution (K,M,f_0,t,w_R,roadflag)
Q = [-K,-w_R*M,w_R*M,-K]; % fix imbalanced brackets
if roadflag==1 % start with if
s_1 = -f_0./K; % = is an assignment; == is a logical test
s_2 = 0/K; % this is just zero ??
q_P = s_1+s_2*t;
elseif roadflag==2
q_P = s_1*sin(w_R*t) + s_2*cos(w_R*t);
s_1 = s_2==(F./Q); % unused; F is undefined
end
end % close function scope
Observe that s_1 and s_2 are used in both cases, but are only defined in the first case. If these are indeed needed by both, move them outside the conditional structure.
Also, the size of the variables is likely to matter, but we don't know what they are. We know that Q is a vector. if F and s_2 are scalars, then
s_1 = s_2==(F./Q)
will return a logical vector. If either F or s_2 are vectors, their size would need to be compatible with that of Q. Again, since this assignment occurs after the assignment of the only output variable, the results are never used for anything. If it's to be moved before the assignment of q_P, then it remains to be seen how a logical vector would make conceptual sense in the calculation or whether the variable dimensions would be compatible.
Perhaps this is supposed to be an indexing operation, but I can only really guess at this point.
s_1 = s_2(s_2==(F./Q)) % ??

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by