How to make table in loop

1 view (last 30 days)
Firas Sidqi
Firas Sidqi on 2 Oct 2021
Commented: Firas Sidqi on 2 Oct 2021
If I have yhe code below, and I want the table to record all my iteration, how to do the table function? Since it's only displaying the last iteration only.
Input Lambda as 9 and Total Power as 450
clc
clear all
P=0;
kanan = input('Masukkan Lambda Awal, Lambda 1 : ')
kiri = 0;
Lmbd = 0;
P = 0;
a = [ 0.0025 8.4 225 ];
b = [ 0.0081 6.3 729 ];
c = [ 0.0025 7.5 400 ];
Total_P = input('Total Power, P : ')
FC1 = 0.8;
FC2 = 1.02;
FC3 = 0.9;
F1 = FC1*a;
F2 = FC2*b;
F3 = FC3*c;
Iterasi = 1;
Lmbd = kanan;
P1 = (Lmbd - F1(2)) / (F1(1)*2);
P2 = (Lmbd - F2(2)) / (F2(1)*2);
P3 = (Lmbd - F3(2)) / (F3(1)*2);
P = P1 + P2 + P3;
while abs(P - Total_P) >= 0.0001
Iterasi = Iterasi + 1;
Lmbd = abs((kanan + kiri) / 2);
P1 = (Lmbd - F1(2)) / (F1(1)*2);
P2 = (Lmbd - F2(2)) / (F2(1)*2);
P3 = (Lmbd - F3(2)) / (F3(1)*2);
P = P1 + P2 + P3;
if P > Total_P
kanan = Lmbd;
end
if P < Total_P
kiri = Lmbd;
end
end
% P1
% P2
% P3
T = table(Iterasi, P1, P2, P3, P, Lmbd);

Accepted Answer

C B
C B on 2 Oct 2021
Edited: C B on 2 Oct 2021
to record all iteration, you need to store them in loop.
As shown in below code.
For More information check Add Rows from Cell Array
clc
clear all
P=0;
TUpdated={};
kanan = input('Masukkan Lambda Awal, Lambda 1 : ')
kiri = 0;
Lmbd = 0;
P = 0;
a = [ 0.0025 8.4 225 ];
b = [ 0.0081 6.3 729 ];
c = [ 0.0025 7.5 400 ];
Total_P = input('Total Power, P : ')
FC1 = 0.8;
FC2 = 1.02;
FC3 = 0.9;
F1 = FC1*a;
F2 = FC2*b;
F3 = FC3*c;
Iterasi = 1;
Lmbd = kanan;
P1 = (Lmbd - F1(2)) / (F1(1)*2);
P2 = (Lmbd - F2(2)) / (F2(1)*2);
P3 = (Lmbd - F3(2)) / (F3(1)*2);
P = P1 + P2 + P3;
while abs(P - Total_P) >= 0.0001
Iterasi = Iterasi + 1;
Lmbd = abs((kanan + kiri) / 2);
P1 = (Lmbd - F1(2)) / (F1(1)*2);
P2 = (Lmbd - F2(2)) / (F2(1)*2);
P3 = (Lmbd - F3(2)) / (F3(1)*2);
P = P1 + P2 + P3;
if P > Total_P
kanan = Lmbd;
end
if P < Total_P
kiri = Lmbd;
end
TUpdated = [TUpdated; table(Iterasi, P1, P2, P3, P, Lmbd)];
end
TUpdated
  1 Comment
Firas Sidqi
Firas Sidqi on 2 Oct 2021
Thank you Mr. Bhavsar. It solve my question well. I need to learn more about how arrays work in Matlab. The one that impress me is how you store the table using array {}, not matrix [].

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!