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 CenterFile Exchange에서 Handle Classes에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by