How to create for loop

10 次查看(过去 30 天)
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
Star Strider 2023-3-2
编辑:Star Strider 2023-3-2
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.
.
  10 个评论
Katherine
Katherine 2023-3-3
@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
Star Strider 2023-3-3
As always, my pleasure!

请先登录,再进行评论。

更多回答(1 个)

Torsten
Torsten 2023-3-2
编辑:Torsten 2023-3-2
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.

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by