Effacer les filtres
Effacer les filtres

If else if loop

7 vues (au cours des 30 derniers jours)
Nimish Iwale
Nimish Iwale le 16 Juin 2020
Commenté : Nimish Iwale le 16 Juin 2020
Hello all, I am trying to make a MATLAB function block in Simulink where in I need a specific spring force (Spring rate x Displacement) for a specific displacement signal. Following is the Simulink figure and the corresponding code in the block.
function Spring_Force = fcn(displacement)
if (0 <= displacement <= 15) % Stroke
Spring_Force = 10*displacement;
elseif (16 <= displacement <= 100) % Stroke
Spring_Force = 18*displacement;
else
Spring_Force = 14*displacement;
end
__________________________________________________________________________________________________________________________
The problem: The output is always the first statement i.e. - Spring_Force = 10*displacement; even if the displacement signal varies above 15 as specified. It does not consider the elseif and else statements at all. There is no error shown. If I re-write the first statement as Spring_Force = 0*displacement, the output is 0 for that input signal. Can anyone please help on this?
Thank you very much.

Réponses (1)

madhan ravi
madhan ravi le 16 Juin 2020
function Spring_Force = fcn(displacement)
Spring_Force = (0 <= displacement <= 15) .* 10*displacement + ...
(16 <= displacement <= 100) + 18*displacement + ...
(displacement > 100) + 1 * displacement;
end
  1 commentaire
Nimish Iwale
Nimish Iwale le 16 Juin 2020
Thank you very much.
Can you please tell me why have you used - '.*' for the 1st condition and '+' for the rest two?

Connectez-vous pour commenter.

Catégories

En savoir plus sur General Applications dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by