how to add values to an array from a loop

14 ビュー (過去 30 日間)
tomer polsky
tomer polsky 2017 年 7 月 7 日
回答済み: tomer polsky 2017 年 7 月 7 日
hello i want to add value to my array from a loop how do i do it ? i want to get all the values . here is the code . i want to get array of all the alpha that giving me (17.99<X(2,:) && X(2,:)<18.0001) how do i do it ?
clc ;
clear all;
U=12;R=12.5;C=2200e-6;L1=1e-3;L2=1e-3;C1=470e-6;R1=0.5; %%values of the sepic model
A_a=[0 0 0 1/(C1);0 -1/(R*C) 0 0;0 0 -R1/(L1) 0;-1/(L2) 0 0 0];
B_aU=[0;0;(1/L1);0]*U;
A_b=[0 0 1/C1 0;0 -1/(R*C) 1/C -1/C;-1/L1 -1/L1 -(R1)/L1 0;0 1/L2 0 0];
B_bU=[0; 0 ;1/L1; 0]*U;
for i=1:20
alpha_array=zeros(i+1,1);
for alpha=0:0.0001:1
X=-(inv(A_a+alpha*A_b))*(B_aU+alpha*B_bU)
if(17.99<X(2,:) && X(2,:)<18.0001)
alpha_array(i+1,:)=alpha
disp(alpha)
end
end
end
  1 件のコメント
Rik
Rik 2017 年 7 月 7 日
Have a read here and here. It will greatly improve your chances of getting an answer.
Use the {}Code button to make your code readable.

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 7 月 7 日
The purpose of the loop over i is not clear. A meaningful comment would calrify this. Do not let the readers in the forum guess, what you want to achieve.
vAlpha = 0:0.0001:1;
iResult = 0;
Result = zeros(1, numel(vAlpha)); % Pre-allocate
for iAlpha = 1:numel(vAlpha)
X = -(A_a+alpha*A_b) \ (B_aU+alpha*B_bU); % Faster and more accurate than inv(X)*Y
if all(17.99 < X(2,:) && X(2,:) < 18.0001) % Or any()?
iResult = iResult + 1;
Result(iResult) = alpha;
end
end
Result = Result(1:iResult); % Crop unused elements

その他の回答 (1 件)

tomer polsky
tomer polsky 2017 年 7 月 7 日
ok my bad . i want to find all the alpha that satisfy this condition: X(2,:)=18 and put all the posbilities of alpha into an array .

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by