Why do I receive "Array indices must be positive integers or logical values"?

1 view (last 30 days)
clc; clear all; close all;
syms epsr_bar eps_r r
r = 0.001;
a = 1; f = 0.05;
n = 0.1;
eps2 = 1;
A = linspace(0,4,20);
for x = 1:length(A)
eps_r = A(x).*(r/a).^n;
cond = epsr_bar(0.001) == eps_r(0.001);
eqn = diff(epsr_bar(r),r) == (2.*eps_r.^2-eps_r.*epsr_bar-epsr_bar.^2)./(r.*eps_r);
ySol(r) = dsolve(eqn,cond);
eps_e(x) = eps2+((3*eps2*f).*((ySol-eps2)./(ySol+2*eps2)));
end
plot(A,eps_e)

Answers (2)

Image Analyst
Image Analyst on 12 Nov 2021

Walter Roberson
Walter Roberson on 17 Nov 2021
syms epsr_bar eps_r r
That makes epsr_bar into a symbolic scalar variable, not a function. It also creates eps_r and r as scalar symbolic variables.
r = 0.001;
That overwrites r with a numeric (non-symbolic) value.
cond = epsr_bar(0.001) == eps_r(0.001);
epsr_bar is a symbolic variable, not a symbolic function, so epsr_bar(0.001) is an attempt to index the symbolic variable at location 0.001, which is an invalid location.
  1 Comment
Walter Roberson
Walter Roberson on 17 Nov 2021
syms epsr_bar(r)
r0 = 0.001;
a = 1; f = 0.05;
n = 0.1;
eps2 = 1;
A = linspace(0,4,20);
num_a = length(A);
eps_e = cell(num_a, 1);
for x = 1:num_a
eps_r(r) = A(x).*(r/a).^n;
cond = epsr_bar(r0) == eps_r(r0);
eqn = diff(epsr_bar(r),r) == (2.*eps_r(r).^2-eps_r(r).*epsr_bar(r)-epsr_bar(r).^2)./(r.*eps_r(r));
ySol(r) = dsolve(eqn,cond);
eps_e{x} = eps2+((3*eps2*f).*((ySol(r)-eps2)./(ySol(r)+2*eps2)));
end
Warning: Unable to find symbolic solution.
eps_e
eps_e = 20×1 cell array
{0×0 sym } {[(3*((4*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((4*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1 ]} {[(3*((8*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((8*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1 ]} {[(3*((12*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((12*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((16*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((16*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((20*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((20*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((24*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((24*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((28*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((28*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((32*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((32*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((36*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((36*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((40*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((40*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((44*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((44*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((48*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((48*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((52*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((52*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((56*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((56*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]} {[(3*((60*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) - 1))/(20*((60*r^(1/10)*(921*(1/1000)^(921^(1/2)/10) + 29*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 29*(1/1000)^(921^(1/2)/10)*921^(1/2)))/(19*(921*(1/1000)^(921^(1/2)/10) + 31*921^(1/2)*r^(921^(1/2)/10) + 921*r^(921^(1/2)/10) - 31*(1/1000)^(921^(1/2)/10)*921^(1/2))) + 2)) + 1]}
But now what? You have constructed an array of functions of r, one for each value of A, but to plot them you need to know the range of r to plot over. You do not want to plot only at the fixed point r0 (0.001), as your cond forces the value at r0 to be something particular, and there would be no point in solving the differential equation if you only wanted to plot the fixed value.

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox 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!