Illegal use of reserved keyword "else".

9 views (last 30 days)
Craig Johnson
Craig Johnson on 2 Dec 2021
Edited: DGM on 2 Dec 2021
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 Comments
Chunru
Chunru on 2 Dec 2021
What are your edited code and the error message?
Craig Johnson
Craig Johnson on 2 Dec 2021
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

Sign in to comment.

Answers (2)

KSSV
KSSV on 2 Dec 2021
Edited: KSSV on 2 Dec 2021
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 Comments
KSSV
KSSV on 2 Dec 2021
Where are you using it? Show us the full code which you are using and throwing error.
KSSV
KSSV on 2 Dec 2021
Editted the code....there is a typo error extra brace.

Sign in to comment.


DGM
DGM on 2 Dec 2021
Edited: DGM on 2 Dec 2021
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)) % ??

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!