How can i define a parameter with if condition?

4 ビュー (過去 30 日間)
Rajith
Rajith 2013 年 2 月 4 日
I want to define variables with if and switch conditions in simbiology. Is it possible to define using matlab code?
For example I have variable say
x = normrnd(140,104)
if x>440
y = 440;
elseif x<40
y = 40;
else
x=y;
end

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 4 日
編集済み: Azzi Abdelmalek 2013 年 2 月 4 日
You have to initialize y
y=0 %for example
%or maybe in the last else you wanted
y=x
then
x = normrnd(140,104)
y=x
if x>440
y = 440;
elseif x<40
y = 40;
end
%or without a for loop
x = normrnd(140,104)
y=40*(x<40)+440*(x>440)+x*(40<=x & x<=440)

その他の回答 (1 件)

Arthur Goldsipe
Arthur Goldsipe 2013 年 2 月 4 日
Hi,
The short answer is that SimBiology cannot directly use "if" or "switch", but you can use them indirectly by putting them in a helper function. For example, to use the sample code you provided, create a file named "myrand.m" with the following content:
function y = myrand
x = normrnd(140,104)
if x>440
y = 440;
elseif x<40
y = 40;
else
y = x; % I assume "x=y" was a typo
end
Then, to use this function to set the initial value of a species named "z" in a model stored in variable "m1", use the following command:
m1.addrule('z = myrand()', 'initialAssignment');
Further, as Azzi points out above, this particular example can be written without if or switch statements. Here's how I'd write the rule so that I didn't have to create a separate helper function:
m1.addrule('z = max(min(normrnd(140, 104), 440), 40)', 'initialAssignment');
  1 件のコメント
Rajith
Rajith 2013 年 2 月 15 日
Thanks for the answers. These help me a lot for the PBPK modelling.

サインインしてコメントする。

コミュニティ

その他の回答  SimBiology コミュニティ

カテゴリ

Help Center および File ExchangeExtend Modeling Environment についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by