对于循环给出相同的输出

5 views (last 30 days)
Mohsen Nashel
Mohsen Nashel on 12 May 2020
评论: Mohsen Nashel2020年5月13日
我对此代码有一个问题,即当循环到达输入的第四列时,它给出相同的输出,循环给出相同的结果(如果不应该)。对于我的地狱,我不知道怎么了!它必须在循环中不在数字中!我在所有情况下都不景气地运行了它们,并在每种情况下都获得了独特的输出!
%% Defining Scenario Independent Paramters:
火箭的%
有效载荷质量%:
m_l = 1;%公斤
鳍片数量:
n = 3;
%Shell density:
rho_s = 2700;%kg/m^3
%推进剂密度:
rho_p = 1772;%kg/m^3
壳的工作压力:
sig_s = 60*10^6;%pa
%重力:
g=9.81;%m/s^2
%For air:
%atmorspheric tem_perature在海平面上:
T_atm=298;
百分比的空气热比:
伽马= 1.4;
%Density of air at sea level:
rho=1.225;
海平面压力的%:
P_a=101325;%pa
%%My 9 scenarios that are defined the text data are:
%最大高度,H_MAX = 0.3048*[20000 20000 20000 2000 2000 10000 30000 10000 30000];%m
%Normalized max acceleration, a_max= [10 10 10 5 20 10 10 5 20]
% Static margin , SM = [1 2 3 2 2 2 2 1 3]
%%通过9个secenarios计算d_max和l_max
Data=readmatrix('Data');每个Col为1个测试%
d_max =零(1,9);
l_max =零(1,9);
为了循环= 1:9
h_max=Data(1,loop);
a_max = data(2,loop);
SM=Data(3,loop);
R_max=1+a_max;
W_eq=sqrt((h_max*g)/(((log(R_max)/2)*(log(R_max)-2))+((R_max-1)/R_max)));
t_bmax =(r_max-1)*w_eq/(g*r_max);
M_eq=W_eq/sqrt(gamma*287*T_atm);
P_c=P_a*(1+(((gamma-1)/2)*M_eq^2))^(gamma/(gamma-1));
P_0_a=P_c/P_a;
最佳L,D的%迭代
% Creating Length and Diameter Limiatation for Iteration
l = linspace(0.0001,4,5000);%m
D=linspace(0.0001,2,5000);%m
%Iteration through all the parameters:
为了i = 1:长度(d);
为了j = 1:长度(l)
delta =(p_c/(2*sig_s))*d(i);壳的厚度百分比
m_n = delta*rho_s*pi*d(i)*(d(i)+sqrt(d(i)^2+(d(i)^2/4)));
M_f=((D(i)^2)/2)*delta*rho_s;
m_f_b =(pi*d(i)*rho_s*d(i)*delta);
m_s =(pi*d(i)*rho_s*l(j)*delta)+m_n+m_f+m_f_b;%%%%%%%%%%
M_p=(R_max-1)*(M_s+M_L);
l_p =(m_p/(pi*d(i)^2*rho_p/4));
如果l_p
火箭鼻压力中心%
x_n =(2/3)*d(i);%m
CN_n=2;
%Center of Pressure for Rocket Fin
a = d(i);%m
s=D(i);%m
b=0;
m=a-b;
fin_hyp=sqrt(2)*D(i);%m
x_f = d(i)+l(j);%m
delta_x_f =(((m*(a+2*b))/(3*(a+b)))+((1/6)*(a+b - (((a*b)/(a+b))));%m
X_f_dash=X_f+delta_X_f;%m
CN_f=(4*N*(s/D(i))^2)/(1+sqrt(1+((2*fin_hyp)/(a+b))^2));%m
k_fb=1+((D(i)/2)/(s+(D(i)/2)));%m
CN_fb=k_fb*CN_f;%m;
x_cp =(((cn_n*x_n)+(cn_fb*x_f_dash))/(cn_n+cn_fb);
%Center of Gravity for Rocket Cone:
X_ncg=2*D(i)/3;
m_n = delta*rho_s*pi*d(i)*(d(i)+sqrt(d(i)^2+(d(i)^2/4)));
m_c = m_l+m_n;
火箭鳍重心%
x_f_cg =(2*d(i)/3)+l(j)+d(i);
M_f=((D(i)^2)/2)*delta*rho_s;
%Center of Gravity for Rocket Tube 1
l_1 = l(j)+d(i)-l_p;
xl_1_cg =(l_1/2)+d(i);
M_L_1=pi*D(i)*L_1*delta*rho_s;
%Center of Gravity for Rocket Tube 2
L_2=L_p;
xl_2_cg =(l_2/2)+d(i)+l_1;
m_l_2 =(pi*d(i)*l_2*delta*rho_s)+m_p;
X_cg = ((M_c * X_ncg) + (M_L_1 * xL_1_cg) + (xL_2_cg * M_L_2) +(X_f_cg*M_f*3))/(M_c+M_L_1+M_L_2+(M_f*3));
cond(i,j)= x_cp-x_cg-(d(i)*sm);
如果cond(i,j)<0 || cond(i,j)>0.00001
继续
结尾
l_d(i,j)= l(j)/d(i);
lamda(i,j)=M_L/(M_s+M_p);
别的
继续
结尾
结尾
结尾
%Output
% Matrix indexing for the D_max and L_max
[m,i] = max(lamda,[],,'全部',,,,“线性”);
[row,col] = ind2sub(size(lamda),i);
d_max(1,loop)= d(row)
l_max(1,loop)= l(col)
结尾

接受的答案

Bjarke Skogstad Larsen
Bjarke Skogstad Larsen on 12 May 2020
Edited:Bjarke Skogstad Larsen on 12 May 2020
您需要初始化矩阵:l_d,lamda和cond。
Always initialize variables before a loop when you only set part of the variable at a time inside the loop, like
lamda(i,j)=M_L/(M_s+M_p);
需要这样的初始化:
lamda = nan(长度(d),长度(l));
为了ii=1:length(D)
I will include the full working code below (I changed i to ii and j to jj as I don't like using these variables in Matlab due to the possibility of complex values):
Clc
clear
%% Defining Scenario Independent Paramters:
火箭的%
有效载荷质量%:
m_l = 1;%公斤
鳍片数量:
n = 3;
%Shell density:
rho_s = 2700;%kg/m^3
%推进剂密度:
rho_p = 1772;%kg/m^3
壳的工作压力:
sig_s = 60*10^6;%pa
%重力:
g=9.81;%m/s^2
%For air:
%atmorspheric tem_perature在海平面上:
T_atm=298;
百分比的空气热比:
伽马= 1.4;
%Density of air at sea level:
rho=1.225;
海平面压力的%:
P_a=101325;%pa
%%My 9 scenarios that are defined the text data are:
%最大高度,H_MAX = 0.3048*[20000 20000 20000 2000 2000 10000 30000 10000 30000];%m
%Normalized max acceleration, a_max= [10 10 10 5 20 10 10 5 20]
% Static margin , SM = [1 2 3 2 2 2 2 1 3]
%%通过9个secenarios计算d_max和l_max
Data=readmatrix('Data');每个Col为1个测试%
d_max =零(1,9);
l_max =零(1,9);
为了循环= 1:9
disp(num2str(loop));
h_max=Data(1,loop);
a_max = data(2,loop);
SM=Data(3,loop);
R_max=1+a_max;
W_eq=sqrt((h_max*g)/(((log(R_max)/2)*(log(R_max)-2))+((R_max-1)/R_max)));
t_bmax =(r_max-1)*w_eq/(g*r_max);
M_eq=W_eq/sqrt(gamma*287*T_atm);
P_c=P_a*(1+(((gamma-1)/2)*M_eq^2))^(gamma/(gamma-1));
P_0_a=P_c/P_a;
最佳L,D的%迭代
% Creating Length and Diameter Limiatation for Iteration
l = linspace(0.0001,4,5000);%m
D=linspace(0.0001,2,5000);%m
%Iteration through all the parameters:
L_D = nan(length(D),length(L));
lamda = nan(长度(d),长度(l));
cond = nan(length(D),length(L));
为了ii=1:length(D)
为了JJ = 1:长度(L)
delta =(p_c/(2*sig_s))*d(ii);壳的厚度百分比
m_n = delta*rho_s*pi*d(ii)*(d(ii)+sqrt(d(ii)^2+(d(ii)^2/4)));
m_f =((d(ii)^2)/2)*delta*rho_s;
M_f_b=(pi*D(ii)*rho_s*D(ii)*delta);
M_s=(pi*D(ii)*rho_s*L(jj)*delta)+M_n+M_f+M_f_b;%%%%%%%%%%
M_p=(R_max-1)*(M_s+M_L);
L_p=(M_p/(pi*D(ii)^2*rho_p/4));
如果L_p
火箭鼻压力中心%
x_n =(2/3)*d(ii);%m
CN_n=2;
%Center of Pressure for Rocket Fin
a = d(ii);%m
s = d(ii);%m
b=0;
m=a-b;
fin_hyp=sqrt(2)*D(ii);%m
X_f=D(ii)+L(jj);%m
delta_x_f =(((m*(a+2*b))/(3*(a+b)))+((1/6)*(a+b - (((a*b)/(a+b))));%m
X_f_dash=X_f+delta_X_f;%m
cn_f =(4*n*(s/d(ii))^2)/(1+sqrt(1+((2*fin_hyp)/(a+b))^2)^2));%m
k_fb = 1+((d(ii)/2)/(s+(d(ii)/2)));%m
CN_fb=k_fb*CN_f;%m;
x_cp =(((cn_n*x_n)+(cn_fb*x_f_dash))/(cn_n+cn_fb);
%Center of Gravity for Rocket Cone:
x_ncg = 2*d(ii)/3;
m_n = delta*rho_s*pi*d(ii)*(d(ii)+sqrt(d(ii)^2+(d(ii)^2/4)));
m_c = m_l+m_n;
火箭鳍重心%
X_f_cg=(2*D(ii)/3)+L(jj)+D(ii);
m_f =((d(ii)^2)/2)*delta*rho_s;
%Center of Gravity for Rocket Tube 1
L_1 = L(JJ)+D(II)-L_P;
xL_1_cg=(L_1/2)+D(ii);
m_l_1 = pi*d(ii)*l_1*delta*rho_s;
%Center of Gravity for Rocket Tube 2
L_2=L_p;
XL_2_CG =(L_2/2)+D(II)+L_1;
M_L_2=(pi*D(ii)*L_2*delta*rho_s)+M_p;
X_cg = ((M_c * X_ncg) + (M_L_1 * xL_1_cg) + (xL_2_cg * M_L_2) +(X_f_cg*M_f*3))/(M_c+M_L_1+M_L_2+(M_f*3));
cond(ii,jj)= x_cp-x_cg-(d(ii)*sm);
如果cond(ii,jj)<0 ||cond(ii,jj)> 0.00001
继续
结尾
L_D(ii,jj)=L(jj)/D(ii);
lamda(ii,jj)= m_l/(m_s+m_p);
别的
继续
结尾
结尾
结尾
%Output
% Matrix indexing for the D_max and L_max
[m,i] = max(lamda,[],,'全部',,,,“线性”);
[row,col] = ind2sub(size(lamda),i);
d_max(1,loop)= d(row)
l_max(1,loop)= l(col)
结尾

更多答案(0)

标签

下载188bet金宝搏

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

Start Hunting!