how to add values to an array from a loop
8 views (last 30 days)
Show older comments
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
Accepted Answer
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
0 Comments
More Answers (1)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!