# How to create for loop

2 views (last 30 days)
Katherine on 2 Mar 2023
Commented: Star Strider on 3 Mar 2023
I am trying to write a code to determine C(t) as a function of time and tabulate C(t) for t=0-10 units of time for each Kc. I also need to plot C(t) on the same set of axis, as a function of t for each value of Kc.
I was trying to use a for loop to tabulate this is what I have so far. The for loop runs but does not give any actual values it just says "value of Ct1:"
Any help would be greatly apprectaied!
clear;
clc;
syms s t;
Kc1 = 3;
numerator1=[(Kc1*(1/3)) (Kc1*1)];
denominator1=[(1/6) 1 (11/6) (1+Kc1) 0];
roots1=roots(denominator1);
[r1, p1, k1]= residue(numerator1, denominator1);
Ct1= ilaplace((r1(1)/(s-p1(1)))+(r1(2)/(s-p1(2)))+(r1(3)/(s-p1(3)))+k1)
Ct1 = [ empty sym ]
Kc2 = 6;
numerator2=[(Kc2*(1/3)) (Kc2*1)];
denominator2=[(1/6) 1 (11/6) (1+Kc2) 0];
roots2=roots(denominator2);
[r2, p2, k2]= residue(numerator2, denominator2);
Ct2= ilaplace((r2(1)/(s-p2(1)))+(r2(2)/(s-p2(2)))+(r2(3)/(s-p2(3)))+k2)
Ct2 = [ empty sym ]
Kc3 = 9;
numerator3=[(Kc3*(1/3)) (Kc3*1)];
denominator3=[(1/6) 1 (11/6) (1+Kc3) 0];
roots3=roots(denominator3);
[r3, p3, k3]= residue(numerator3, denominator3);
Ct3= ilaplace((r3(1)/(s-p3(1)))+(r3(2)/(s-p3(2)))+(r3(3)/(s-p3(3)))+k3)
Ct3 = [ empty sym ]
Kc4 = 12;
numerator4=[(Kc4*(1/3)) (Kc4*1)];
denominator4=[(1/6) 1 (11/6) (1+Kc4) 0];
roots4=roots(denominator4);
[r4, p4, k4]= residue(numerator4, denominator4);
Ct4= ilaplace((r4(1)/(s-p4(1)))+(r4(2)/(s-p4(2)))+(r4(3)/(s-p4(3)))+k4)
Ct4 = [ empty sym ]
for Ct1 = 1:10
Ct1 = ilaplace((r1(1)/(s-p1(1)))+(r1(2)/(s-p1(2)))+(r1(3)/(s-p1(3)))+k1)
fprintf('value of Ct1: %d\n' , Ct1);
end
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:
Ct1 = [ empty sym ]
value of Ct1:

Star Strider on 2 Mar 2023
Edited: Star Strider on 2 Mar 2023
You need to convert the numeric vectors into polynomials in s before taking the inverse Laplace transform of them. Even then, the result may be challenging to work with, since the inversion involves taking the roots of a 3-degree polynomial in the denominator of the numerator terms, and none of them simplify.
However they can be plotted as functions of time —
% clear;
% clc;
syms s t
sympref('AbbreviateOutput',false); % Optional
Kc1 = 3;
numerator1=[(Kc1*(1/3)) (Kc1*1)];
denominator1=[(1/6) 1 (11/6) (1+Kc1) 0];
% roots1=roots(denominator1);
% [r1, p1, k1]= residue(numerator1, denominator1);
% Ct1= ilaplace((r1(1)/(s-p1(1)))+(r1(2)/(s-p1(2)))+(r1(3)/(s-p1(3)))+k1)
Ct1pf = simplify(partfrac(poly2sym(numerator1,s) / poly2sym(denominator1,s)), 500)
Ct1pf =
Ct1 = ilaplace(Ct1pf)
Ct1 =
Kc2 = 6;
numerator2=[(Kc2*(1/3)) (Kc2*1)];
denominator2=[(1/6) 1 (11/6) (1+Kc2) 0];
% roots2=roots(denominator2);
% [r2, p2, k2]= residue(numerator2, denominator2);
% Ct2= ilaplace((r2(1)/(s-p2(1)))+(r2(2)/(s-p2(2)))+(r2(3)/(s-p2(3)))+k2);
Ct2pf = simplify(partfrac(poly2sym(numerator2,s) / poly2sym(denominator2,s)), 500)
Ct2pf =
Ct2 = ilaplace(Ct2pf)
Ct2 =
Kc3 = 9;
numerator3=[(Kc3*(1/3)) (Kc3*1)];
denominator3=[(1/6) 1 (11/6) (1+Kc3) 0];
% roots3=roots(denominator3);
% [r3, p3, k3]= residue(numerator3, denominator3);
% Ct3= ilaplace((r3(1)/(s-p3(1)))+(r3(2)/(s-p3(2)))+(r3(3)/(s-p3(3)))+k3);
Ct3pf = simplify(partfrac(poly2sym(numerator3,s) / poly2sym(denominator3,s)), 500)
Ct3pf =
Ct3 = ilaplace(Ct3pf)
Ct3 =
Kc4 = 12;
numerator4=[(Kc4*(1/3)) (Kc4*1)];
denominator4=[(1/6) 1 (11/6) (1+Kc4) 0];
% roots4=roots(denominator4);
% [r4, p4, k4]= residue(numerator4, denominator4);
% Ct4= ilaplace((r4(1)/(s-p4(1)))+(r4(2)/(s-p4(2)))+(r4(3)/(s-p4(3)))+k4);
Ct4pf = simplify(partfrac(poly2sym(numerator4,s) / poly2sym(denominator4,s)), 500)
Ct4pf =
Ct4 = ilaplace(Ct4pf)
Ct4 =
% for Ct1 = 1:10
% Ct1 = ilaplace((r1(1)/(s-p1(1)))+(r1(2)/(s-p1(2)))+(r1(3)/(s-p1(3)))+k1);
% fprintf('value of Ct1: %d\n' , Ct1);
% end
figure
subplot(4,1,1)
fplot(Ct1, [0 20])
grid
title('Ct1')
subplot(4,1,2)
fplot(Ct2, [0 20])
grid
title('Ct2')
subplot(4,1,3)
fplot(Ct2, [0 20])
grid
title('Ct3')
subplot(4,1,4)
fplot(Ct4, [0 20])
grid
title('Ct4')
That is likely the best that can be done with these.
EDIT — Corrected typographical errors.
.
Katherine on 3 Mar 2023
@Star Strider It did work when I ran it as a comment so I will most likey have to update. Thank you for your help
Star Strider on 3 Mar 2023
As always, my pleasure!

### More Answers (1)

Torsten on 2 Mar 2023
Edited: Torsten on 2 Mar 2023
You see the reason (see above) ?
The degree of the denominator is bigger than the degree of the numerator. Thus k1,k2,k3 and k4 are empty.
And Ct1 will still be symbolic and depend on t. Thus printing it as a double will throw an error.