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!