Recording solutions given by for loop as a vector.

2 ビュー (過去 30 日間)
Joe
Joe 2014 年 12 月 8 日
コメント済み: Guillaume 2014 年 12 月 8 日
So I have created a load of code that helps to plot the coefficient of lift across a 2D cambered wing. I have a bit of code that gives me a vector of numbers that directly impacts the coefficient. N = 20, 40, 80, 160, 320, 640. These N input values are run through a for loop showing how the output changes when the numbers double (20, 40, 80 etc.) The loop runs smoothly through all values, but I do not know how to record the values as a vector without upsetting the code already written. I will include the code below, bare in mind that it is a very experimental piece of code so there will be bits that look odd and such, ignore them, I simply need to know how to record the AREA output (one of the last lines of code inside the loop).
for N = [20,40,60,80,160,320,640];
GWE = (4.9736)./N;
x1 = [0:GWE:P*4];
x2 = [P*4:GWE:4];
x3 = cat(2,x1,x2);
x4 = [0:GWE:P*4];
x5 = [P*4:GWE:4];
x6 = cat(2,x4,x5);
Z1 = ((0.07*4)/(P.^2))*(((2*P)*(x4/4))-((x4/4).^2));
Z2 = ((0.07*4)/((1-P).^2))*((1-(2*P))+((2*P)*(x5/4))-((x5/4).^2));
Z3 = cat(2,Z1,Z2);
Y1 = (tmax/0.2)*((0.2969*((x3/c).^0.5))-(0.1281*(x3/4))-(0.3516*((x3/4).^2))+(0.2843*((x3/4).^3))-(0.1015*((x3/4).^4)));
dzdx = diff(Z3);
%To Stop The Loop From Not Syncing Together
TSTL = N - ((N*2)/10);
if TSTL > 127
TSTL = N - (((N*2)/10)-1);
end
if TSTL >512
TSTL = N - (((N*2)/10)-2);
end
x=x3(1,1:TSTL);
Y=Y1(1,1:TSTL);
Z=Z3(1,1:TSTL);
theta = atan(diff(Z3));
xup = x - (Y.*sin(theta));
yup = Z + (Y.*cos(theta));
xlow = x + (Y.*sin(theta));
ylow = Z - (Y.*cos(theta));
plot (x,Z, '-.r')
hold on
plot (x,0, 'g')
plot (xup,yup)
plot (xlow,ylow)
axis([-1 5 -1 1.5])
c = 4;
rhoinf = 1.2;
vinf = 80;
[gamma, xj] = vortex_lattice(dzdx, x, alpha, vinf, rhoinf, c);
figure
plot(xj, gamma);
Area = trapz(xj,gamma);
end

回答 (1 件)

Guillaume
Guillaume 2014 年 12 月 8 日
Replace the loop with:
NCoeffs = [20,40,60,80,160,320,640];
Area = zeros(size(NCoeffs));
for coeffidx = 1:numel(NCoeffs)
N = NCoeffs(coeffidx);
...
And the Area calculation with:
Area(coeffidx) = trapz(xj, gamma);
  2 件のコメント
Joe
Joe 2014 年 12 月 8 日
This worked great. My answers are now being stored as a vector as required but unfortunately they are all using the first N value (20), I can't seem to get them to use the corresponding [20, 40,80,160 etc...]
Guillaume
Guillaume 2014 年 12 月 8 日
The line
N = NCoeffs(coeffidx);
sets N to each of the values of NCoeffs. Did you forget the line?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by