how to add values to an array from a loop

7 visualizzazioni (ultimi 30 giorni)
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 Commento
Rik
Rik il 7 Lug 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.

Accedi per commentare.

Risposta accettata

Jan
Jan il 7 Lug 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

Più risposte (1)

tomer polsky
tomer polsky il 7 Lug 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 .

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by