For loop vector issue

4 Ansichten (letzte 30 Tage)
Christian Boaitey
Christian Boaitey am 26 Mär. 2022
Beantwortet: Christian Boaitey am 26 Mär. 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

Akzeptierte Antwort

Torsten
Torsten am 26 Mär. 2022
Bearbeitet: Torsten am 26 Mär. 2022
E = 0:0.01:w;
n = numel(E);
for i = 1:n
e = E(i);
...
Sbvec(i) = Sb;
end

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 26 Mär. 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 am 26 Mär. 2022
Both solutions have solved my problem, many thanks!

Kategorien

Mehr zu Handle Classes finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by