How to use for loop when have syms inside the loop

1 次查看(过去 30 天)
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
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
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
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 CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by