How to create for loop
2 views (last 30 days)
Show older comments
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)
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)
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)
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)
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
0 Comments
Accepted Answer
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)
Ct1 = ilaplace(Ct1pf)
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)
Ct2 = ilaplace(Ct2pf)
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)
Ct3 = ilaplace(Ct3pf)
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)
Ct4 = ilaplace(Ct4pf)
% 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.
.
10 Comments
More Answers (1)
See Also
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!