using a 'for' loop to create a vector

조회 수: 50 (최근 30일)
Mikhos
Mikhos 2013년 3월 19일
I need to put the roots of the equation shown (f) into a vector of one column. I have managed to go some way with this however i keep getting the following error:
_Attempted to access x(6,1); index must be a positive integer or logical.
Error in sec_C1 (line 24)
x((a+0.01)*100,1)= (fzero(f,[0,0.1])_
How can I get this to work, and is there a better way to obtain the vector?
w =0.001;
x = ones(1,61)
for a = 0:0.01:0.6
g = @(v) (2048*((1+v).^4) + 128*((1+v).^2).*a.^6 + a.^12 + 16*(1+v).*sqrt(16384*((1+v).^6) + 2048*((1+v).^4).*a.^6 + 80*((1+v).^2).*a.^12 + a.^18)).^(1/3);
r =@(v) (1/(16*(1+v))).*( a.^4 + (a.^8)./g(v) + g(v) );
alpha= @(v) (a./r(v));
A= @(v) 2*pi*(r(v).^2)*(1+sqrt(1-alpha(v).^2));
epsilon = @(v) 1/2*(A(v)/(4*pi-pi*a.^2)-1);
f= @(v) (1-sqrt(1-alpha(v).^2)).*epsilon(v) + (epsilon(v).^2) - w;
format long % shows more d.p
%find roots
x((a+0.01)*100,1)= (fzero(f,[0,0.1]))
end

채택된 답변

Kye Taylor
Kye Taylor 2013년 3월 19일
You are parametrizing your for-loop using a vector of nonintegers, then trying to index into the variable x. I would instead parametrize the for-loop with a vector of integers, then use those integers to index into both x and the vector a. For example,
Instead of doing this
for a = 0:0.01:0.6
x((a+0.01)*100,1) = a
end
I would try
a = 0:0.01:0.6;
for j = 1:length(a)
x(j,1) = a(j);
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Breaks, Knots, and Sites에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by