How to use for loop when have syms inside the loop

11 Ansichten (letzte 30 Tage)
Sabella Huang
Sabella Huang am 30 Mai 2022
Kommentiert: Torsten am 31 Mai 2022
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.

Antworten (1)

Torsten
Torsten am 30 Mai 2022
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 Kommentare
Sabella Huang
Sabella Huang am 31 Mai 2022
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 am 31 Mai 2022
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by