Encoding conditional assignments in SimBiology
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Arthur Goldsipe
il 15 Lug 2024
1 voto
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 Commenti
琛
il 15 Feb 2025
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 18 Feb 2025
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.
Categorie
Scopri di più su Scan Parameter Ranges in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!