# How to use for loop when have syms inside the loop

2 ビュー (過去 30 日間)
Sabella Huang 2022 年 5 月 30 日
コメント済み: Torsten 2022 年 5 月 31 日
Hello guys,
I want to ask about, how to use for loop when I have syms and diff inside looping. Here is my code that I tried to solve. Thank you
Xmax = 1410.34;
conc = [50 100 150]*1e-9;
ka = 3.46e3;
kd = 1.46e-4;
for i = 1:length(conc)
syms t x(t)
eqn = diff(x,t) == (ka*conc(:,i)*Xmax)-(ka*conc(:,i))*x-kd*x;
cond = x(0) == 0;
X_ass(t) = dsolve(eqn,cond);
X_ass = matlabFunction(X_ass);
t = linspace(0, 1800, 600)';
signal(:,i) = [X_ass(t)];
end
When I used this code, MATLAB give me warning this:
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function
body must be sym expression.

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

### 回答 (1 件)

Torsten 2022 年 5 月 30 日
Try
syms t x(t)
Xmax = 1410.34;
conc = [50 100 150]*1e-9;
ka = 3.46e3;
kd = 1.46e-4;
for i = 1:length(conc)
eqn = diff(x,t) == (ka*conc(1,i)*Xmax)-(ka*conc(1,i))*x-kd*x;
cond = x(0) == 0;
X_ass(t) = dsolve(eqn,cond);
X_ass = matlabFunction(X_ass);
T = linspace(0, 1800, 600)';
signal(:,i) = X_ass(T);
end
##### 2 件のコメントなしを表示なしを非表示
Sabella Huang 2022 年 5 月 31 日
still cannot work sir. Its appear "Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression."
Torsten 2022 年 5 月 31 日
Try
syms t x(t) c
Xmax = 1410.34;
conc = [50 100 150]*1e-9;
ka = 3.46e3;
kd = 1.46e-4;
T = linspace(0, 1800, 600)';
eqn = diff(x,t) == (ka*c*Xmax)-(ka*c)*x-kd*x;
cond = x(0)==0;
X_ass(t) = dsolve(eqn,cond);
for i = 1:numel(conc)
X_ass_fun = matlabFunction(subs(X_ass,c,conc(i)));
signal(:,i) = X_ass_fun(T);
end

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

### カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by