Subscript indices must either be real positive integers or logicals.

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));

 Accepted Answer

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

I was trying to compute F for each bD, R, and theta value. So the first value of each, then the first value of bD with the second of R and theta and so forth.
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
I'm trying to compute F and plot a contour 2x2 subplot, bD is independent, R and theta are dependent, for each R there is a corresponding theta.
bD(1) R(1) theta(1)
bD(1) R(2) theta(2)
...
bD(2) R(1) theta(1)
bd(2) R(2) theta(2)
...

Sign in to comment.

More Answers (1)

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

I was trying to compute F for each bD, R, and theta value. So the first value of each, then the first value of bD with the second of R and theta and so forth. Your suggestion works with what I am trying to do, but I also need to create a contour plot. Can I do that using mat?
Will meshgrid give the same result?
meshgrid() will transpose the outputs.
Are you trying to compute with
bD(1), R(1), theta(1)
bD(1), R(1), theta(2)
bD(1), R(1), theta(3)
...
bD(1), R(2), theta(1)
or are you trying to compute with
bD(1), R(1), theta(1)
bD(1), R(2), theta(2)
bD(1), R(3), theta(3)
...
bD(2), R(1), theta(1)
with the R index always the same as the theta index?
I'm trying to compute F and plot a contour 2x2 subplot, bD is independent, R and theta are dependent, for each R there is a corresponding theta.
bD(1) R(1) theta(1)
bD(1) R(2) theta(2)
...
bD(2) R(1) theta(1)
bd(2) R(2) theta(2)
...
The code I gave you does that. The indexes for R and theta are in lockstep, just like you showed.
Then go ahead and mark as "Answered"
One more thing. What is the best way to scale the x, y, and z-axes when plotting this in a 2x2 contour subplot? I'm trying to use XTick and just set the values, but then my contour won't plot.
Why not just specify xlim() and ylim()?
I tried and my contour won't show, so I thought if I just assigned the values it would work, but it doesn't.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!