Main Content

if

Specify conditional equations, assignments, annotations, and conditional sections in component files

Parent Section: equationsintermediates

Syntax

if Predicate1 
   Branch1
elseif Predicate2
   Branch2
else
   Branch3
end

Description

In the Simscape™ language, you can use if statements in a variety of contexts. For example, you can use if statements to:

  • Specify conditional equations

  • Make conditional assignments when declaring intermediates

  • Define component variants by using conditional sections in a component file

  • Define conditional visibility of component members using annotations

Depending on the context, the rules and restrictions can be different.

Conditional Equations

To specify conditional equations, use if statements within the equations section. An if statement starts with an if keyword and ends with an end keyword.

Every if requires an else. elseif branches are optional.

The if and elseif branches start with a predicate expression. If a predicate is true, the branch gets activated. When all predicates are false, the else branch gets activated.

The body of each branch can contain one or more equation expressions. The total number of equation expressions, their dimensionality, and their order must be the same for every branch of the if-elseif-else statement. However, this rule does not apply to the assert expressions, because they are not included in the expression count for the branch.

Conditional Sections

A conditional section is a top-level section guarded by an if clause. Conditional sections are parallel to other top-level sections of a component file, such as declaration or equations sections.

A conditional section starts with an if keyword and ends with an end keyword. It can have optional elseif and else branches. The body of each branch of a conditional section can contain declaration blocks, equations, structure sections, and so on, but cannot contain the setup function.

The if and elseif branches start with a predicate expression. If a predicate is true, the branch gets activated. When all predicates are false, the else branch (if present) gets activated. The compiled model includes elements (such as declarations, equations, and so on) from active branches only.

Unlike the if statements in the equations section, different branches of a conditional section can have different variables, different number of equations, and so on.

Examples

expand all

For a component where x and y are declared as 1x1 variables, specify the following piecewise equation:

y={xfor 1<= x<=1x2otherwise 

This equation, written in the Simscape language, looks like:

equations
  if x >= -1 && x <= 1
    y == x;
  else
    y == x^2;
  end
end

Another way to write this equation in the Simscape language is:

equations
  y == if x>=-1 && x<=1, x else x^2 end
end

This example hides inapplicable parameters from the block dialog box based on the control parameter value.

component MyPipe
  parameters
    circular  = true;             % Circular pipe?
    d_in      = { 0.01, 'm' };    % Pipe internal diameter
    area      = { 1e-4, 'm^2' };  % Noncircular pipe cross-sectional area
    D_h       = { 1.12e-2, 'm' }; % Noncircular pipe hydraulic diameter
  end
  if circular 
  % Hide inapplicable parameters
    annotations
       [area, D_h] : ExternalAccess=none;
    end
    equations
       % First set of equations, for circular pipe 
    end
  else
  % Hide inapplicable parameter
    annotations
       d_in : ExternalAccess=none;
    end
    equations
       % Second set of equations, for noncircular pipe 
    end
  end
  ... % Other parameters, variables, equations
end

This example shows a component containing two resistors. The resistors can be connected either in series or in parallel, depending on the value of the control parameter:

component TwoResistors
  nodes
     p = foundation.electrical.electrical; % +:left
     n = foundation.electrical.electrical; % -:right
  end
  parameters
    p1 = {1, 'Ohm'};   % Resistor 1
    p2 = {1, 'Ohm'};   % Resistor 2
    ct = 0;            % Connection type (0 - series, 1 - parallel)
  end
  components(ExternalAccess=observe)
    r1 = foundation.electrical.elements.resistor(R=p1);
    r2 = foundation.electrical.elements.resistor(R=p2);
  end
  if ct == 0      % linear connection
    connections
      connect(p, r1.p);
      connect(r1.n, r2.p);
      connect(r2.n, n);
    end
  else          % parallel connection
    connections
      connect(r1.p, r2.p, p);
      connect(r1.n, r2.n, n);
    end
  end
end

Version History

Introduced in R2008b