Why do I receive "Array indices must be positive integers or logical values"?
1 view (last 30 days)
Show older comments
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)
0 Comments
Answers (2)
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
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
eps_e
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.
See Also
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!