loop for specific values

60 ビュー (過去 30 日間)
Alina Abdikadyr
Alina Abdikadyr 2023 年 2 月 13 日
編集済み: Dyuman Joshi 2024 年 2 月 27 日
Hello everyone!
Please, could you help me with a code.
I need to calculate for a given set of values V and P. So, my set is phi = [0;36.87;48.19;55.15;60]
My code is:
clear all; close all
W = 10000;
S = 40;
AR=7;
cd0 = 0.005;
k = 1 / pi / AR;
Psl=25000;
clalpha = 2*pi;
rho=1.225;
figure(1);hold on; xlabel('V');ylabel('P')
for phi = [0;36.87;48.19;55.15;60]
i=0;
for alpha = 1:0.25:12
i=i+1;
n(i)=1/cos(phi*pi/180);
cl(i) = clalpha * alpha * pi/180;
V(i) = sqrt(2*n(i)*W/rho/S/cl(i));
L(i) = 0.5 * rho * V(i) * V(i) * S * cl(i);
cd(i) = cd0 + k * cl(i) * cl(i);
D(i) = 0.5 * n(i)*rho * V(i) * V(i) * S * cd(i);
clcd(i) = cl(i)/cd(i);
p(i) = D(i)*V(i);
P_a(i)=Psl;
end
figure(1); plot(V,p)
hold on
plot(V,P_a)
end

回答 (2 件)

Dyuman Joshi
Dyuman Joshi 2023 年 2 月 13 日
編集済み: Dyuman Joshi 2024 年 2 月 27 日
From the for loop documentation - "To iterate over the values of a single column vector, first transpose it to create a row vector."
Thus, transpose the input such that it is provided as a row vector for the iterating values and your code will work.
Also, you can vectorize the inner for loop like I have shown -
W = 10000;
S = 40;
AR = 7;
cd0 = 0.005;
k = 1 / pi / AR;
Psl = 25000;
clalpha = 2*pi;
rho = 1.225;
figure(1);
hold on;
xlabel('V');ylabel('P');
%Bring out the constant values out of the loop
alpha = 1:0.25:12;
cl = clalpha*alpha*pi/180;
cd = cd0 + k * cl.^2;
clcd = cl./cd;
P_a = Psl*ones(size(alpha));
for phi = [0;36.87;48.19;55.15;60]'
% transpose^
n=1/cos(phi*pi/180);
V = sqrt(2.*n.*W./rho./S./cl);
L = 0.5*rho*V.^2*S.*cl;
D = 0.5.*n.*rho.*V.^2*S.*cd;
p = D.*V;
plot(V,p);
plot(V,P_a);
end

Robert
Robert 2023 年 11 月 28 日
編集済み: Robert 2023 年 11 月 28 日
@ geometry dash scratch
Hello, Here's the modified code with some improvements:
clear all; close all
W = 10000;
S = 40;
AR = 7;
cd0 = 0.005;
k = 1 / (pi * AR);
Psl = 25000;
clalpha = 2 * pi;
rho = 1.225;
figure(1);
hold on;
xlabel('V');
ylabel('P');
phi = [0; 36.87; 48.19; 55.15; 60];
for k = 1:numel(phi)
alpha_range = 1:0.25:12;
n = 1 / cos(phi(k) * pi / 180);
cl = clalpha * alpha_range * pi / 180;
V = sqrt(2 * n * W / (rho * S * cl));
L = 0.5 * rho * V.^2 * S .* cl;
cd = cd0 + k * cl.^2;
D = 0.5 * n * rho * V.^2 * S .* cd;
clcd = cl ./ cd;
p = D .* V;
P_a = Psl * ones(size(V));
figure(1);
plot(V, p);
hold on;
plot(V, P_a);
end

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by