using a 'for' loop to create a vector

178 views (last 30 days)
Mikhos
Mikhos on 19 Mar 2013
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

Accepted Answer

Kye Taylor
Kye Taylor on 19 Mar 2013
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!