not showing all results from loop

7 次查看(过去 30 天)
saeed abdallah
saeed abdallah 2018-5-1
回答: Arjun 2024-8-21,8:19
i want to put my results in a table but when i do that the calculated vaule is 1*1 sym and only shows the table for the final vaule of the loop variable
Ae = sym('Ae');
cd = sym('cd');
cl = sym('cl');
clt = sym('clt');
clw = sym('clw');
ct = sym('ct');
K = 0.045;
Ve = 0;
a = 5.19;
aw0 = -2/57.3;
awr = 1/57.3;
cd0 = 0.03;
cm0 = -0.0711;
cw = 1.716;
g = 9.81;
h = 0.29;
h0 = -0.08;
ht = 2000.0976;
k = 0;
lt = 6.184;
m = 6300;
ro = 1.006;
s = 25.08;
st = 7.79;
ztw = 0.312;
de=0.279;
e0=2/57.3;
nt=1.5/57.3;
a1=3.2;
a2=2.414;
Vt=(st*lt)/(s*cw);
for v=100:15:250
vi(i)= v*0.515
vi=vi(i)'
eqn = [(((2*m*g)*sin(Ae+Ve)/(ro*s))/vi^2)-ct*cos(k)+cd*cos(Ae)-cl*sin(Ae)==0, ...
(((2*m*g)*cos(Ae+Ve)/(ro*s))/vi^2)-cl*cos(Ae)-cd*sin(Ae)-cd*sin(k)==0, ...
(cm0+((h-h0)*clw))-(Vt*clt)+ct*(ztw/cw)==0, ...
cl-clw-(clt*st/s)==0, ...
cd-cd0-K*cl^2==0, ...
clw-a*(Ae+awr-aw0)==0 ];
[Ae_,ct_,cd_,clt_,clw_,cl_] = vpasolve(eqn,Ae,ct,cd,clt,clw,cl);
display(v);display(Ae_);
display(ct_);
display(cd_); display(clt_);
display(clw_); display(cl_);
awi=Ae_+awr;ldi=clw_/cd_;
setae=Ve+awi-awr;
Ati=(awi*(1-de))+nt-e0-awr;
nei=(clt_/a2)-((a1/a2)*Ati);
Li=0.5*ro*s*cl_*(vi^2);
%
Ti=0.5*ro*s*ct_*(vi^2);Di=0.5*ro*s*cd_*(vi^2);
display(awi);
display(ldi);
display(setae);
display(Ati);
display(nei);
display(Li);
display(Ti);
display(Di);
table(v,Ae_,ct_,cd_,clt_,clw_,cl_)
end

回答(1 个)

Arjun
Arjun 2024-8-21,8:19
Hi,
As per my understanding, you are calculating some data inside for loop and you wish to keep accumulating it into a table. After reviewing the code, it seems like you are only displaying the values rather than storing them in the for loop.
Kindly follow the below mentioned steps to store the values in the table.
  1. Initialize an empty array before the loop to keep storing the results.
  2. Append the result of each iteration to this table.
  3. After the loop, convert the array to a table and display it.
Refer to the code below to show the values in the table.
% Define symbolic variables
Ae = sym('Ae');
cd = sym('cd');
cl = sym('cl');
clt = sym('clt');
clw = sym('clw');
ct = sym('ct');
% Define constants
K = 0.045;
Ve = 0;
a = 5.19;
aw0 = -2/57.3;
awr = 1/57.3;
cd0 = 0.03;
cm0 = -0.0711;
cw = 1.716;
g = 9.81;
h = 0.29;
h0 = -0.08;
ht = 2000.0976;
k = 0;
lt = 6.184;
m = 6300;
ro = 1.006;
s = 25.08;
st = 7.79;
ztw = 0.312;
de=0.279;
e0=2/57.3;
nt=1.5/57.3;
a1=3.2;
a2=2.414;
Vt=(st*lt)/(s*cw);
% Initialize an array to store results
results = [];
for v = 100:15:250
vi = v * 0.515;
eqn = [(((2*m*g)*sin(Ae+Ve)/(ro*s))/vi^2)-ct*cos(k)+cd*cos(Ae)-cl*sin(Ae)==0, ...
(((2*m*g)*cos(Ae+Ve)/(ro*s))/vi^2)-cl*cos(Ae)-cd*sin(Ae)-cd*sin(k)==0, ...
(cm0+((h-h0)*clw))-(Vt*clt)+ct*(ztw/cw)==0, ...
cl-clw-(clt*st/s)==0, ...
cd-cd0-K*cl^2==0, ...
clw-a*(Ae+awr-aw0)==0 ];
[Ae_, ct_, cd_, clt_, clw_, cl_] = vpasolve(eqn, Ae, ct, cd, clt, clw, cl);
awi = Ae_ + awr;
ldi = clw_ / cd_;
setae = Ve + awi - awr;
Ati = (awi * (1 - de)) + nt - e0 - awr;
nei = (clt_ / a2) - ((a1 / a2) * Ati);
Li = 0.5 * ro * s * cl_ * (vi^2);
Ti = 0.5 * ro * s * ct_ * (vi^2);
Di = 0.5 * ro * s * cd_ * (vi^2);
% Append results to the array
results = [results; v, double(Ae_), double(ct_), double(cd_), double(clt_), double(clw_), double(cl_), ...
double(awi), double(ldi), double(setae), double(Ati), double(nei), double(Li), double(Ti), double(Di)];
end
% Convert results to a table
resultsTable = array2table(results, 'VariableNames', {'v', 'Ae_', 'ct_', 'cd_', 'clt_', 'clw_', 'cl_', ...
'awi', 'ldi', 'setae', 'Ati', 'nei', 'Li', 'Ti', 'Di'});
% Display the table
disp(resultsTable);
Kindly refer to the following MathWorks documentation link to more about “array2table” function: https://www.mathworks.com/help/matlab/ref/array2table.html
I hope this will help!

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by