How to solve the error: Input arguments must be convertible to floating-point numbers.

17 views (last 30 days)
Hi, I keep getting the error with this line of code:
scale = pi/6/B/max(max(JsNumMag));
I don't know how to fix it.
Additionally, the rest of the code is as follows:
%Known Parameters
u_0 = 4*pi*10^(-7);
e_0 = 8.85*10^(-12);
normal = [0,0,1];
EPSR = 1;
Eo = 1;
syms x y z t w B;
Ex = 0;
Ey = 0;
Ez = Eo*cos(w*t-B*(x+y));
E = [Ex, Ey, Ez];
H = Maxwell1stEqn(E,t,u_0,x,y,z);
T = 2*pi/w;
time = T/4;
B = w*sqrt(u_0*e_0);
w = 10^9;
for k = 1:3
Hc = subs(H(k), [t,z], [time, 0]);
Hnum(k) = Hc;
end;
Js = cross(normal, Hnum);
xi = -pi/B:pi/6/B:pi/B;
yi = -pi/B:pi/6/B:pi/B;
for i = 1:length(yi)
for m = 1:length(xi)
JsNum(i,m,:) = subs(Js, [x, y], [xi(m),yi(i)]);
JsNumMag(i,m) = sqrt(JsNum(i,m,1)^2 +JsNum(i,m,2)^2 + JsNum(i,m,3)^2);
end;
end;
scale = pi/6/B/max(max(JsNumMag));
figure(1)
for i = 1:length(yi)
for m = 1:length(xi)
quiver(xi(m),yi(i), JsNum(i,m,1)*scale, JsNum(i,m,2)*scale, 0, 'r');
hold on;
end;
end;
axis equal;
title('Surface Current Density Vector on a PEC Plane');
xlabel('x [m]');
ylabel('y [m]');
With the function Maxwell1stEquation being:
function H = Maxwell1stEqn(E, t, u_0,x,y,z)
H = -int(curl(E, [x, y, z]),t)/u_0;
return

Answers (1)

Walter Roberson
Walter Roberson on 14 Oct 2020
syms x y z t w B;
Symbolic variables.
Ez = Eo*cos(w*t-B*(x+y));
So Ez involves the symbolic variable B
E = [Ex, Ey, Ez];
H = Maxwell1stEqn(E,t,u_0,x,y,z);
and so do those.
B = w*sqrt(u_0*e_0);
You reassign B as the product of the symbolic variable w and a numeric value.
This has no effect on any expressions that already used B.
When you defined
syms B
then that was the equivalent of
B = sym('B')
which tells the Symbolic Engine to prepare a symbolic variable named B inside the symbolic engine, and return an internal reference to the symbolic variable, and store that reference in the MATLAB variable B. The MATLAB variable B is not the same as the symbolic variable B -- the MATLAB level is like a pointer into a different process (sort of.)
If you were to then do
A = B;
B = 1e-9;
then you would copy the reference information into A, and then you would change the MATLAB level variable B to be numeric, but the MATLAB variable A would hold the reference to the symbolic engine variable B. If you were to then ask to display the content of the MATLAB level variable A then MATLAB would take the reference information stored in A , pass it over to the symbolic engine, the symbolic engine would say, "Oh, the printable version of the symbolic variable B is 'B' " and return that. And that would happen even after you assigned B at the MATLAB level -- you can still refer to the symbolic B even though your MATLAB B is set to something different.
Consider for example
P = 1;
Q = P * 10;
P = 2;
then you would understand that in that numeric case, the value of P as of the time that Q was assigned to was copied and used to compute the numeric Q variable, and that there would be no "memory" left in Q of how the value was computed. And then when you change P, you would not expect that Q would suddenly change from evaluating to 10 into evaluating to 20: Q = P * 10 is not a formula for computing Q based upon the value of "current" value of P at the time Q is asked for.
Likewise, when you computed H in terms of the symbolic variable B and then you change the MATLAB variable B, then H is going to be left in terms of the symbolic variable B, and will not be updated to use the new value of B.
w = 10^9;
Same difficulty there. The B = w*sqrt(u_0*e_0); recorded the fact that w was symbolic variable w at that point, and when you change MATLAB w in the next line, B is left holding the symbolic w and does not suddenly use the numeric value assigned to w afterwards.
for k = 1:3
Hc = subs(H(k), [t,z], [time, 0]);
Hnum(k) = Hc;
end;
Those H(k) values use the symbolic B variable inside them, and you are not substituting anything for symbolic B in the subs(), so Hc is going to involve symbolic B and so will Hnum(k)
Js = cross(normal, Hnum);
Hnum involves symbolic B so Js is going to involve symbolic B
for i = 1:length(yi)
for m = 1:length(xi)
JsNum(i,m,:) = subs(Js, [x, y], [xi(m),yi(i)]);
JsNumMag(i,m) = sqrt(JsNum(i,m,1)^2 +JsNum(i,m,2)^2 + JsNum(i,m,3)^2);
end;
end;
Js involves symbolic B and you do not subs() for it, so JsNum is going to involve sybolic B as well. And since that is used in the next line, JsNumMag is going to involve symbolic B as well.
We now are at the point where JsNumMag is a 2D array whose elements involve the symbolic variable B
scale = pi/6/B/max(max(JsNumMag));
JsNumMag involves the symbolic variable B . max() will see that the input is not isnumeric() and will try to convert the input to double, using double(JsNumMag) internally. But you cannot convert JsNumMag to double because it involves the unresolved symbolic variable B
What you should learn from this is:
  • you should be careful about redefining a symbolic variable, especially if you have used the variable inside an expression and you are not just about to overwrite the expression with new content
  • If you have a symbolic variable in an expression, then instead of assigning to the variable name and hoping the change gets noticed, you should subs() the new value of the variable into the expression.
  • Most of the time, instead of defining an expression in terms of a symbolic variable, and then later substituting in a constant for the symbolic variable, you should just instead assign the constant to the variable before it gets used for anything else. This is not always the case: there are some cases where MATLAB can solve() or dsolve() an expression better with a symbolic expression in place instead of the actual value of the constant. For example, diff((x+1)^N,x) works better with syms N positve that is then substituted with numeric N: if you substitute in a numeric value such as diff((x+1)^(83/105),x) first, then MATLAB might decide that it needs to expand out (x+1)^83 into an exact expression first
  1 Comment
Madeleine Megg
Madeleine Megg on 14 Oct 2020
How do I use subs effectively, because I have to use symbolic variables otherwise an enormous amount of memory is needed to compute the function?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!