how to add values to an array from a loop

8 views (last 30 days)
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 Comment
Rik
Rik on 7 Jul 2017
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.

Sign in to comment.

Accepted Answer

Jan
Jan on 7 Jul 2017
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

More Answers (1)

tomer polsky
tomer polsky on 7 Jul 2017
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 .

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!