For loop vector issue

4 ビュー (過去 30 日間)
Christian Boaitey
Christian Boaitey 2022 年 3 月 26 日
回答済み: Christian Boaitey 2022 年 3 月 26 日
Hi all,
I am in the midst of programming some code for a university assignment, however, I'm having some trouble with my for loop.
I want to save the values from the for loop for the variable 'Sb'. However, when using 'Sb(e)', I get "Array indices must be positive integers or logical values". When running the code without 'Sb(e)' it runs fine but I just don't get the intermediate values in between. Much help appreciated!
clc
clear
%% Constants
Dp=125e-3;
Dm=45e-3;
Lp=1500e-3;
Lk=215e-3;
alpha=5;
g=9.81;
k=1.2336;
R=350.7;
T0=2322;
%% Part A
w=(Dp-Dm)/2;
Gc=((w*cosd(alpha)-Lk*sind(alpha))/(1-sind(alpha)));
gamma=sqrt(k)*(2/(k+1))^((k+1)/(2*k-1));
C=sqrt((R*T0)/gamma);
for e=0:0.01:w
if e<=Gc
Rv=(Dm/2)+Lk*tand(alpha)+e.*((1-e.*sind(alpha)/cosd(alpha)));
elseif e>=Gc
Rv=Dp/2;
end
Rm=(Dm/2)+e;
Sb=2*pi*Rm*(Lp-Lk-e*tand(alpha/2))+(Rv^2-Rm^2)*(pi/sind(alpha));
Sbvec(e)=Sb
end

採用された回答

Torsten
Torsten 2022 年 3 月 26 日
編集済み: Torsten 2022 年 3 月 26 日
E = 0:0.01:w;
n = numel(E);
for i = 1:n
e = E(i);
...
Sbvec(i) = Sb;
end

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 3 月 26 日
e_values = 0:0.01:w;
num_e = length(e_values);
Sbvec = zeros(num_e, 1);
for e_idx = 1 : num_e
e = e_values(e_idx);
if e<=Gc
Rv=(Dm/2)+Lk*tand(alpha)+e.*((1-e.*sind(alpha)/cosd(alpha)));
elseif e>=Gc
Rv=Dp/2;
else
error('comparison case not accounted for')
end
Rm=(Dm/2)+e;
Sb=2*pi*Rm*(Lp-Lk-e*tand(alpha/2))+(Rv^2-Rm^2)*(pi/sind(alpha));
Sbvec(e_idx)=Sb;
end

Christian Boaitey
Christian Boaitey 2022 年 3 月 26 日
Both solutions have solved my problem, many thanks!

カテゴリ

Help Center および File ExchangeHandle Classes についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by