Help With Error: "Attempted to access S_0(2); index out of bounds because numel(S_0)=1."

Hi, so I'm trying to get this part of the program running in matlab, and the error, "Attempted to access S_0(2); index out of bounds because numel(S_0)=1." comes up. here's what I have of the code so far. I'm pretty new to matlab, so I don't know all the functions. I'm trying to find the sum of S_0 without using the "sum" function. Please help asap I need this fast!
Here's the code:
% Inputs and Equations that Calculate the Equations for the Coefficient C_l
f = input('Please Enter the First Digit of the NACA Number'); % Input for the Max. Camber
s = input('Please Enter the Second Digit of the NACA Number'); % Input for the Position of Max. Camber
theta = input('Please Enter Angle of Attack in Degrees'); % Angle of Attack in Degrees
M = f/100; % Calculates the Maximum Camber of the Wing
P = s/10; % Calculates the Position of the Maximum Camber of the Wing
x = .5*(1+cos(theta)); % Distance
n = 180/(theta)+1; % Calculates the Number of Rectangles to be Used for the Integral
% Calculation of dy_c/dx for Values of Different x
if x>= 0 && x<=P;
dy_c1_dx = (M/P^2)*(2*P-2*x); % This is the Gradient if 0<=X and X<=P
elseif x>=P && x<=1;
dy_c2_dx = (M/(1-P)^2)*(2*P-2*x); % This is the Gradient if P<=X and X<=1
end
S_0 = 0
S_1 = 0
for i = 1:n
if x>=0 && x<= P;
S_0(i) = S_0(i)+(dy_c1_dx);
S_1(i) = S_1(i)+(dy_c1_dx)*cos(theta);
elseif x> P && x<=1;
S_0(i) = S_0(i)+(dy_c2_dx);
S_1(i) = S_1(i)+(dy_c2_dx)*cos(theta);
end
end

 採用された回答

Chandrasekhar
Chandrasekhar 2014 年 3 月 26 日

0 投票

In the code u have declared S_0 and S_1 as scalars and you are trying to access them as vectors in the for loop.S_0 and S_1 has only one element where as you are trying to access the second element of S_0 and S_1 which doesnt exist

4 件のコメント

Andrew
Andrew 2014 年 3 月 26 日
How can I fix this problem? Could you show me a possible solution? Sorry I'm still learning matlab
Try the below code
% Inputs and Equations that Calculate the Equations for the Coefficient C_l
f = input('Please Enter the First Digit of the NACA Number'); % Input for the Max. Camber
s = input('Please Enter the Second Digit of the NACA Number'); % Input for the Position of Max. Camber
theta = input('Please Enter Angle of Attack in Degrees'); % Angle of Attack in Degrees
M = f/100; % Calculates the Maximum Camber of the Wing
P = s/10; % Calculates the Position of the Maximum Camber of the Wing
x = .5*(1+cos(theta)); % Distance
n = 180/(theta)+1; % Calculates the Number of Rectangles to be Used for the Integral
% Calculation of dy_c/dx for Values of Different x
if x>= 0 && x<=P;
dy_c1_dx = (M/P^2)*(2*P-2*x); % This is the Gradient if 0<=X and X<=P
elseif x>=P && x<=1;
dy_c2_dx = (M/(1-P)^2)*(2*P-2*x); % This is the Gradient if P<=X and X<=1
end
sum1 = 0;
sum2 = 0;
for i = 1:n
if x>=0 && x<= P;
sum1 = sum1+(dy_c1_dx);
S_0(i) = sum1;
sum2 = sum2+(dy_c1_dx)*cos(theta);
S_1(i) = sum2;
elseif x> P && x<=1;
sum1 = sum1+ (dy_c2_dx);
S_0(i) = sum1;
sum2 = sum2 + (dy_c2_dx)*cos(theta);
S_1(i) = sum2;
end
end
Andrew
Andrew 2014 年 3 月 26 日
That worked! Thank you so much, now I can move on to the next step of the program! Thank you again!
Chandrasekhar
Chandrasekhar 2014 年 3 月 26 日
please accept the answer. thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeElementary Math についてさらに検索

質問済み:

2014 年 3 月 26 日

編集済み:

2014 年 3 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by