Subscript indices must either be real positive integers or logicals.
1 view (last 30 days)
Show older comments
Can someone please explain this error. Here is my code and function.
bD = [0.1, 1.0, 10, 100];
R = (1:1.25:5);
c = R;
a = 1/2*bD;
b = abs(sqrt(c.^2-a.^2));
theta = abs(atan(b./a));
[F] = ratio_rp_bf(R,theta,bD);
function [F] = ratio_rp_bf(R,theta,bD)
F(bD,R) = (16/3).*(1./bD).*(1./R).^3.*abs(sqrt((1./R).^4-2.*(1./R).^2.*cos(2.*theta)+1));
0 Comments
Accepted Answer
Image Analyst
on 15 Feb 2013
Edited: Image Analyst
on 15 Feb 2013
R takes on fractional values and thus can't be an index into an array. You don't need to put indexes for F in your function. Just do:
F = (16/3).*(1./bD).*(1./R).^3.*abs(sqrt((1./R).^4-2.*(1./R).^2.*cos(2.*theta)+1));
and it will make F a 1 by 4 array automatically. It will take the first values from all 3 arrays (bD, R, and theta), and then the second values, and then the third values, and then the last values. Is that what you want?
3 Comments
Image Analyst
on 15 Feb 2013
Edited: Image Analyst
on 15 Feb 2013
So you have a 2D array where bD is independent, but R and theta indexes are matched (both 2, both 3, or both 4)? Then have the function look like this:
function F = ratio_rp_bf(R,theta,bD)
for r = 1 : length(R)
for b = 1 : length(bD)
F(b, r) = (16/3).*(1./bD(b)).*(1./R(r)).^3.*abs(sqrt((1./R(r)).^4-2.*(1./R(r)).^2.*cos(2.*theta(r))+1));
end
end
And call it like this in the main program:
F = ratio_rp_bf(R,theta,bD)
In the command window:
F =
106.5332 5.5303 1.2148 0.4980
10.6533 0.5530 0.1215 0.0498
1.0653 0.0553 0.0121 0.0050
0.1065 0.0055 0.0012 0.0005
More Answers (1)
Walter Roberson
on 15 Feb 2013
R includes numbers with fractions, but inside ratio_rp_bf, you are trying to assign to F(bD,R) which is an attempt to assign use R as a subscript.
If you are trying to define a function f(x,y) by using an assignment
f(x,y) = <something>
then the only time you can do that is if you are using R2012a or later and you have already defined the function as symbolic
syms f(x,y)
MATLAB does not use assignment to f(x,y) as the notation for creating functions.
My guess as to what you want to do would be
function F = ratio_rp_bf(R,theta,bD)
[Rmat, bDmat, thetamat] = ndgrid(R, bD, theta);
F = (16/3).*(1./bDmat).*(1./Rmat).^3.*abs(sqrt((1./Rmat).^4-2.*(1./Rmat).^2.*cos(2.*thetamat)+1));
end
Notice that this will return a 3D array, as your R, theta, and bD are all vectors.
Possibly you want to use R and theta linked together, always using R(K), theta(K) together? But that you want bD to be an independent axis?
10 Comments
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!