Effacer les filtres
Effacer les filtres

How to use for loop when have syms inside the loop

3 vues (au cours des 30 derniers jours)
Sabella Huang
Sabella Huang le 30 Mai 2022
Commenté : Torsten le 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.

Réponses (1)

Torsten
Torsten le 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 commentaires
Sabella Huang
Sabella Huang le 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 le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by