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);
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!