How to make table in loop

1 次查看(过去 30 天)
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);

采纳的回答

C B
C B 2021-10-2
编辑:C B 2021-10-2
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 个评论
Firas Sidqi
Firas Sidqi 2021-10-2
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 [].

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by