For loop vector issue

4 views (last 30 days)
Christian Boaitey
Christian Boaitey on 26 Mar 2022
Answered: Christian Boaitey on 26 Mar 2022
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

Accepted Answer

Torsten
Torsten on 26 Mar 2022
Edited: Torsten on 26 Mar 2022
E = 0:0.01:w;
n = numel(E);
for i = 1:n
e = E(i);
...
Sbvec(i) = Sb;
end

More Answers (2)

Walter Roberson
Walter Roberson on 26 Mar 2022
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 on 26 Mar 2022
Both solutions have solved my problem, many thanks!

Categories

Find more on Handle Classes in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!