- `(a > 0)` evaluates to `1` if `a` is greater than `0`, and `0` otherwise.
- `(a <= 0)` evaluates to `1` if `a` is less than or equal to `0`, and `0` otherwise.
- When `a > 0`, `(a > 0) * (a + b)` will be `a + b` and `(a <= 0) * (a + c)` will be `0`.
- When `a <= 0`, `(a > 0) * (a + b)` will be `0` and `(a <= 0) * (a + c)` will be `a + c`.
Encoding conditional assignments in SimBiology
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
emjey
le 15 Juil 2024
Commenté : Arthur Goldsipe
il y a environ une heure
Is it possible to define conditional assignments in SimBiology, i.e.
if a > 0
param1 = a + b
else
param1 = a + c
end
I am aware of events but it is insuffient as it lacks the 'else' expression.
0 commentaires
Réponse acceptée
Ayush Singh
le 15 Juil 2024
Hey emjey,
In SimBiology, conditional assignments like the one you described are not directly supported in the same way as typical programming languages. However, you can achieve similar functionality using repeated assignment rules. Since events do not support an `else` clause directly, you will need to set up your model in a way that effectively mimics the conditional logic.
You can achieve this using repeated assignment rules with logical conditions. SimBiology allows you to define rules that continuously update the value of a parameter based on other parameters or species in the model.
You can follow below steps to achieve similar functionality
1. Define Parameters:
- Define the parameters 'a', 'b', 'c', and 'param1' in your model.
2. Create a Rule:
- Add a repeated assignment rule to your model that uses logical conditions.
Example:
Suppose you have parameters 'a', 'b', 'c', and 'param1'. You can define a repeated assignment rule for 'param1' using logical conditions as follows:
% Create a SimBiology model
model = sbiomodel('conditionalAssignmentModel');
% Add parameters
a = addparameter(model, 'a', 1);
b = addparameter(model, 'b', 2);
c = addparameter(model, 'c', 3);
param1 = addparameter(model, 'param1', 0);
% Add a repeated assignment rule
% The rule will use logical conditions to mimic the if-else structure
addrule(model, 'param1 = (a > 0) * (a + b) + (a <= 0) * (a + c)', 'repeatedAssignment');
% Simulate the model (optional)
cs = getconfigset(model);
cs.StopTime = 10;
simData = sbiosimulate(model);
% Plot the results (optional)
sbioplot(simData);
Below is the explanation for above
The rule `param1 = (a > 0) * (a + b) + (a <= 0) * (a + c)` works as follows:
Thus, `param1` will be assigned the value of `a + b` if `a > 0`, and `a + c` if `a <= 0`.
Hope it helps!
1 commentaire
琛
le 15 Fév 2025 à 13:12
After I tried, I found that such an operation can only be achieved after turning off the unit conversion function. I want to know, when my parameter is other units, such as grams, liters, etc., how should I operate?
Plus de réponses (1)
Arthur Goldsipe
le 15 Juil 2024
Hi emjey,
I see this problem solved in one of two ways:
1) Take advantage of true/false getting converted to 1/0 in mathematical operations. This is the approach suggested in the other answer. Another altenative for the rule expression is "param1 = a + b*(a>0) + c*(a<=0)". One disadvantage of this approach is that the rule expressions can easily get complicated and hard to debug, especially for more complicated "if, else if, else" expressions.
2) You can put any complicated logic, include if/else in a helper function that you call from the repeated assignment. That way, you can use if/else to write the code the most natural way. You ca find an example of that approach here. The main downside to this is that you must share this function file with anyone else who wants to use the model, since it isn't automatically bundled with the model or project file.
2 commentaires
琛
le 15 Fév 2025 à 13:13
After I tried, I found that such an operation can only be achieved after turning off the unit conversion function. I want to know, when my parameter is other units, such as grams, liters, etc., how should I operate?
Arthur Goldsipe
il y a environ une heure
I can't say for sure how to fix your problem without more details. But I'm guessing that the problem is that you need to make your expressions dimensionally consistent. For example, in the case of a>0 the value of 0 is dimensionless, so the entire expression is only dimensionally consistent if a is also dimensionless. (For example, if you set the units of a to dimensionless.) If a has differ units, a simple way to make the expression dimensionally consistent is to change the expression to a>0*a. So, a dimensionally consistent alternative for the rule would be param1 = a + b*(a>0*a) + c*(a<=0*a). More generally, you could define some other parameter d to have a value of 0 and the same units as a, and then change the expression to a>d.
If that doesn't answer your question, can you provide more details? Ideally, if you can provide the entire model I (or someone else) can show you how to update it.
Voir également
Catégories
En savoir plus sur Scan Parameter Ranges dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!