Error with FOR loop and SQRT: In an assignment A(I) = B, the number of elements in B and I must be the same.

1 visualización (últimos 30 días)
Would someone be able to help explain why I am getting the error in the FOR loop with the SQRT function and how to fix it. I am getting the error in the second to third calculation of the loop for the equation "c" This is for an urgent assignment due ASAP. Thanks for the review!
My Code:
%Define theta Vector
for n=1:N
theta(n)=n*pi/(N+1);
end
theta=theta'
%Define y-axis
for i=1:N
y(i)=-(b/2)*cos(theta(i));
end
y=y'
% Wing chord vartion type ChordType, 1-elliptical, 2-rectangular, 3-tapered, 4-other
if ChordType==1
for i=1:N
c(i)=c*sqrt(1-(y(i)/(b/2))^2)
end
elseif ChordType==2
c(1:N)=c
end
c=c'
Output for when N=10, using a c0 =10*pi/4=7.85398 and b=1 with AR=10
y =
-3.7679
-3.3036
-2.5716
-1.6313
-0.5589
0.5589
1.6313
2.5716
3.3036
3.7679
c =
0.2817 0.1523
In Red Color output ERROR:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in PA1do_geom3 (line 43)
c(i)=c*sqrt(1-(y(i)/(b/2))^2)

Respuestas (1)

Mischa Kim
Mischa Kim el 18 de Jun. de 2015
Michael, in
for i=1:N
c(i)=c*sqrt(1-(y(i)/(b/2))^2)
end
c is a vector of varying size (growing), which you are trying to assign to a scalar, c(i). I assume you are trying to do something like
for i = 2:N
c(i) = c(i-1)*sqrt(1-(y(i)/(b/2))^2)
end
Note, that the indexing in MATLAB starts at 1 not 0. So your initial value c0 would be c(1).
Also, I recommend not using i or j as variables because they are also used as the imaginary unit in MATLAB.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by