Illegal use of reserved keyword "else".

9 次查看(过去 30 天)
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