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:

 Accepted Answer

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 Comments

Thank you! This was very helpful!
As always, my pleasure!
How do I get lines to show up on the graph? I tried the code and the graphs come up but there are no lines.
I am not certain what the problem could be, since it works in R2022b.
Does it throw any errors or warnings? (It is not possible to use matlabFunction to create anonymous functions or function files from those expressions, since there is no way to determine the root results. I already tried that.)
There are no errors or warnings. I do have an older version though R2019b so maybe that is the issue.
That could be. I no longer have R2019b installed on any of my computers, so I can’t experiment with it.
You can copy the code, paste it to a Comment here, change it (if necessary), and run it here. Then, just right-click on the image to save it as a .png image on your computer. (It may be necessary to Submit the Comment first.) That is not a perfect solution, however it may be the only option (other than upgrading to R2022b).
.
How would I output a table instead of a graph?
@Gabby — Get the results from the plot, then (since they have different numbers of elements in each plot) create a table for each of them —
% 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)
hf1 = fplot(Ct1, [0 20]);
t1 = hf1.XData.';
y1 = hf1.YData.';
grid
title('Ct1')
subplot(4,1,2)
hf2 = fplot(Ct2, [0 20]);
t2 = hf2.XData.';
y2 = hf2.YData.';
grid
title('Ct2')
subplot(4,1,3)
hf3 = fplot(Ct2, [0 20]);
t3 = hf3.XData.';
y3 = hf3.YData.';
grid
title('Ct3')
subplot(4,1,4)
hf4 = fplot(Ct4, [0 20]);
t4 = hf4.XData.';
y4 = hf4.YData.';
grid
title('Ct4')
Results1 = table(t1,y1)
Results1 = 101×2 table
t1 y1 ________ _________ 0 0 0.013035 0.0005031 0.026069 0.0019864 0.039104 0.0044118 0.052138 0.0077423 0.065173 0.011942 0.078207 0.016975 0.091242 0.022808 0.10428 0.029408 0.11731 0.036741 0.13035 0.044776 0.15641 0.06283 0.18248 0.083329 0.20855 0.10604 0.26069 0.15725 0.31283 0.21478
Results2 = table(t2,y2)
Results2 = 239×2 table
t2 y2 _________ ___________ 0 -4.4409e-16 0.0065173 0.00025319 0.013035 0.0010062 0.019552 0.0022493 0.026069 0.0039728 0.032586 0.0061674 0.039104 0.0088235 0.045621 0.011932 0.052138 0.015484 0.058655 0.01947 0.065173 0.023882 0.078207 0.033945 0.091242 0.045606 0.10428 0.058796 0.11731 0.073448 0.13035 0.089497
Results3 = table(t3,y3)
Results3 = 239×2 table
t3 y3 _________ ___________ 0 -4.4409e-16 0.0065173 0.00025319 0.013035 0.0010062 0.019552 0.0022493 0.026069 0.0039728 0.032586 0.0061674 0.039104 0.0088235 0.045621 0.011932 0.052138 0.015484 0.058655 0.01947 0.065173 0.023882 0.078207 0.033945 0.091242 0.045606 0.10428 0.058796 0.11731 0.073448 0.13035 0.089497
Results4 = table(t4,y4)
Results4 = 2030×2 table
t4 y4 _________ ___________ 0 -2.2204e-16 0.0032586 0.00012701 0.0065173 0.00050639 0.0097759 0.0011357 0.013035 0.0020124 0.016293 0.0031342 0.019552 0.0044986 0.02281 0.0061032 0.026069 0.0079456 0.029328 0.010023 0.032586 0.012334 0.035845 0.014876 0.039104 0.017646 0.045621 0.023863 0.052138 0.030965 0.058655 0.038935
That is likely the only way to do it.
.
@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
As always, my pleasure!

Sign in to comment.

More Answers (1)

Torsten
Torsten on 2 Mar 2023
Edited: Torsten on 2 Mar 2023

0 votes

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.

Community Treasure Hunt

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

Start Hunting!