how to add values to an array from a loop

조회 수: 11 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by