For loop vector issue

4 次查看(过去 30 天)
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