Filter löschen
Filter löschen

Illegal use of reserved keyword "else".

9 Ansichten (letzte 30 Tage)
Craig Johnson
Craig Johnson am 2 Dez. 2021
Bearbeitet: DGM am 2 Dez. 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 Kommentare
Chunru
Chunru am 2 Dez. 2021
What are your edited code and the error message?
Craig Johnson
Craig Johnson am 2 Dez. 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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


DGM
DGM am 2 Dez. 2021
Bearbeitet: DGM am 2 Dez. 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)) % ??

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by