DOUBLE cannot convert the input expression into a double array

This is the code. Could anyone run it and give me some suggestion about the error 'DOUBLE cannot convert the input expression into a double array' ? Cheers!
if true % DD =
1.2000 0.6000 1.6000 0.5000 1.3000 1.4000 100.0000
1.2000 0.6000 1.6000 0.5000 1.3000 1.6000 100.0000
1.2000 0.6000 1.6000 0.5000 1.3000 1.8000 100.0000
1.2000 0.8000 1.6000 0.7000 1.2000 1.4000 100.0000
1.2000 0.6000 1.4000 0.7000 1.2000 1.4000 100.0000
1.3000 0.6000 1.6000 0.7000 1.3000 1.4000 100.0000
1.3000 0.6000 1.6000 0.7000 1.3000 1.4000 100.0000
1.4000 0.4000 1.6000 0.6000 1.5000 1.4000 100.0000
1.4000 1.6000 0.4000 0.8000 1.1000 1.2000 100.0000
1.4000 1.8000 0.4000 0.8000 1.1000 1.2000 100.0000
1.4000 2.0000 0.4000 0.8000 1.1000 1.2000 100.0000
1.2000 2.2000 0.3000 0.8000 1.1000 1.2000 100.0000
1.5000 1.6000 0.4000 0.6000 1.1000 1.3000 100.0000
1.5000 1.3000 0.4000 0.6000 1.5000 1.3000 100.0000
1.2000 2.0000 0.7000 0.8000 1.1000 1.2000 100.0000
1.1000 2.0000 0.7000 0.8000 1.1000 1.2000 100.0000
0.5000 1.3000 1.5000 1.4000 0.6000 0.8000 100.0000
0.4000 1.5000 1.5000 1.4000 0.6000 0.8000 100.0000
1.1000 0.5000 1.5000 0.4000 1.2000 1.5000 100.0000
1.1000 0.5000 1.5000 0.4000 1.2000 1.7000 100.0000
1.1000 0.5000 1.5000 0.6000 1.1000 1.3000 100.0000
1.3000 0.3000 1.5000 0.5000 1.4000 1.3000 100.0000
1.1000 0.5000 1.6000 0.4000 1.2000 1.5000 100.0000
1.1000 0.5000 1.7000 0.4000 1.2000 1.5000 100.0000
1.1000 0.6000 1.5000 0.4000 1.2000 1.5000 100.0000
1.3000 0.5000 1.5000 0.4000 1.2000 1.5000 100.0000
1.1000 0.5000 1.6000 0.4000 1.2000 1.5000 100.0000
1.1000 0.8000 1.6000 0.7000 1.2000 1.5000 100.0000
1.1000 0.5000 1.5000 0.4000 1.4000 1.7000 100.0000
1.1000 0.6000 1.5000 0.4000 1.2000 1.7000 100.0000
1.1000 0.5000 1.5000 0.4000 1.2000 1.7000 100.0000
1.1000 0.5000 1.5000 0.4000 1.2000 1.7000 100.0000
1.1000 0.5000 1.5000 0.8000 1.2000 1.7000 100.0000
1.1000 0.5000 1.5000 0.4000 1.2000 1.7000 100.0000
1.1000 0.9000 1.5000 0.4000 1.2000 1.7000 100.0000
1.8000 0.5000 1.5000 0.4000 1.2000 1.7000 100.0000
0.9000 0.5000 1.5000 0.8000 1.2000 1.3000 100.0000
1.1000 0.6000 1.5000 0.8000 1.2000 1.7000 100.0000
0.8000 0.5000 1.8000 0.8000 1.2000 1.7000 100.0000
rn=size(DD,1);
sub=[0.25,0.25,0.25,0.08]
p1=sub(1);
p2=sub(2);
p3=sub(3);
r=sub(4);
eutotal=zeros(rn,5);
eu=zeros(1,rn);
for i=1:rn;
d1=DD(i,1:3)-1;
d2=DD(i,4:6)-1;
edw=DD(i,7);
syms x;
%f=@(x)
f=p1*(d1(1,1)-d2(1,1))*exp(-r*x*(d1(1,1)-d2(1,1)))...
+p2*(d1(1,2)-d2(1,2))*exp(-r*x*(d1(1,2)-d2(1,2)))...
+p3*(d1(1,3)-d2(1,3))*exp(-r*x*(d1(1,3)-d2(1,3)));
y=solve(f,x)
isempty(y);
if isempty(y)
PF(1,1)=10;
else
PF=double(y);
end
end
end

2 个评论

After the solve(), please show use "y", and show class() and size() of all the names that are mentioned in y (that is, the ones that would be affected by the subs())
It shows y = 125*log(z1) + 250*pi*k*i but doesnt give any information about z1,pi,k. I think it is becasue of that they are produced after the code is run... Can you have a look at the whole code I comment below?

请先登录,再进行评论。

 采纳的回答

Okay, I think I understand what is going on.
Sometimes MuPAD (the symbolic engine) creates expressions along the lines of
... * z1 .... where z1 = ...
That is, it extracts a complicated sub-expression and gives it a name, and then at the end says "where" and gives a definition for the sub-expression.
Unfortunately when this happens, the MATLAB interface drops the clause defining the subexpression, leaving a z* variable that did not exist in the original expression. This is, of course, a bug. And it seems to happen more in more recent versions (including in R2012a)
If you start up a MuPAD notepad with the "mupad" command, and solve() the expression inside it, you will see MuPAD's fuller answer.
In the case of the "f" you show above, general solution is
125*ln(RootOf(7*z^14-2*z^5-7,z))
where RootOf(7*z^14-2*z^5-7,z) means all of the values of "z" such that 7*z^14-2*z^5-7 comes out as 0 (the "roots" of the expression).

10 个评论

Hi. Thanks! Is there anyway that let matlab just gives me the explicit solution?
If you start up a MuPAD notepad with the "mupad" command, and solve() the expression inside it, you will see MuPAD's fuller answer.
Could you give me more details about this? I am not quite clear about how to do this.
At the MATLAB command prompt, give the command
mupad
It will open a new notebook window that is running MuPAD directly. In that window, enter the command
solve(exp(-(2*x)/125)/20 + (7*exp(-(7*x)/125))/40 - (7*exp((7*x)/125))/40,x);
and see what output it gives you.
Hi Thanks! But I need to call this function in another function. I am not just wanting to solving for only this one equation...
solve(exp(-(2*x)/125)/20 + (7*exp(-(7*x)/125))/40 - (7*exp((7*x)/125))/40,x);
Anyway can let matlab just give the explicit solution to all the equations? The coefficient of these equations is decided by the input of this function. And when it is called, I don't know what is the input...
You could try using matlabFunction(), but I suspect the same problem will occur. It is a bug in the MATLAB to MuPAD interface.
You could experiment with generate C code within MuPAD; see http://www.mathworks.com/help/symbolic/generate-c-or-fortran-code.html
If I want to slove a function, is that mean I must to use MuPAD interface? If so and matlab has a bug in MuPAD then it is a really huge bug.... All I want to is sloving a function in the easiest way.
If you want the analytic solution then you need to use solve(), through MATLAB (which will call MuPAD for you), or more directly by calling to MuPAD, or by buying MAPLE and installing it as an alternative symbolic toolbox.
When you use solve() from MATLAB and it calls MuPAD for you, MATLAB does not correctly retrieve the full solution: this is a bug in the MATLAB interface to MuPAD. I do not know of any way to correct that problem, but if you open a technical support case they might be able to tell you a way.
If you use a MuPAD notebook, you can get the full solution there. I believe there are ways within MuPAD to export solutions in forms that MATLAB can read. This route would not usually be convenient.
There is a possibility that if you were to use
feval(symengine, 'solve', exp(-(2*x)/125)/20 + (7*exp(-(7*x)/125))/40 - (7*exp((7*x)/125))/40, x)
that you might get the complete solution. I make no promise about that.
What are you going to do with the explicit solution? You take it and promptly apply double() to it, which converts it into a numeric solution instead of a formula. Why not just go for a numeric solution anyhow?
The general form of the explicit solution is:
RootOf(exp(z) * p3 * d1(1,3) - exp(z) * p3 * d2(1,3) + p1 * exp(z * (d1(1,1) - d2(1,1)) / (d1(1,3) - d2(1,3))) * d1(1,1) - p1 * exp(z * (d1(1,1) - d2(1,1)) / (d1(1,3) - d2(1,3))) * d2(1,1) + p2 * exp(z * (d1(1,2) - d2(1,2)) / (d1(1,3) - d2(1,3))) * d1(1,2) - p2 * exp(z * (d1(1,2) - d2(1,2)) / (d1(1,3) - d2(1,3))) * d2(1,2), z) / r / (-d1(1,3) + d2(1,3));
RootOf( expression, z ) means the values of z that make the expression 0 -- the roots of the expression. There is no known closed form solution for the roots of expressions in that particular form, so unless you wish to do some kind of further symbolic analysis on the expression, you are going to have to convert to numeric form anyhow, so you might as well have used numeric form to start with.
Why not just go for a numeric solution anyhow?
I am just going for a numerical solution...I just don't how to do that. But now I know I can use RootOf( expression, z ). Also what will happens if there not solution for expression? I am passing coefficients through fmincon routine so I don't know what these coefficients will be. If there is not solution I need to assign the value of 0 to that particular variable. So I need a way, just like isempty(y)==0 to deal with situation! Thanks!
[x, fval, exitflag] = fzero( @(x) exp(-(2*x)/125)/20 + (7*exp(-(7*x)/125))/40 - (7*exp((7*x)/125))/40, 1, optimiset('Display', 'off'));
if exitflag < 0
PF = 0;
else
PF = x;
end
And it makes my program much much faster!

请先登录,再进行评论。

更多回答(1 个)

Try
subs(y)
Example
y=solve('x+1');
whos y
r=subs(y)
whos r
Also
if isempty(y)==1 is the same as if isempty(y)

15 个评论

In case I understand something not right. Here is my modified code based on your suggestion:)
if true
% f=simplify(p1*d1(1,1)*exp(-r*x*d1(1,1))+p2*d1(1,2)*exp(-r*x*d1(1,2))+p3*d1(1,3)*exp(-r*x*d1(1,3)));
y=solve(f,x);
whos y
if isempty(y)
PF(1,1)=0;
else
PF(1,1)=subs(y);
whos PF(1,1)
end
end
No
whos y
% will show you if y is sym or double
Post the error message and the corresponding code
Can you post just the lines that are causing errors?
Is this what you mean? When I click sym.subs>getNames at 248 it leads me to the corrosponding codes
if iscell(X)
s = cellfun(@(x)[char(x) ','],X,'UniformOutput',false);
s = [s{:}];
s(end) = [];
One example of this case, the value of y is
y =
125*log(z1) + 250*pi*k*i
How can I convert this to a normal double value?
syms z1 k i
y=125*log(z1) + 250*pi*k*i
%to convert this to double you should affect values to your sym variables
z1=1
k=1
i=1
result=subs(y)
Thanks. But I think this might not be right. How do you know if z1=1 k=1 i=1 or not?
xueqi, what are
class(z1)
class(k)
size(z1)
size(k)
and experiment with
subs(y, z1, 1)
subs(y, k, 0)
to see if those give errors or reasonable-looking values
Could you show the value of "f" before the solve ?
f =
exp(-(2*x)/125)/20 + (7*exp(-(7*x)/125))/40 - (7*exp((7*x)/125))/40
OK. Sorry to be chaos. But when I run this code in my office pc, there is no problem at all. And also this error is not always showing in my own laptop. I just have no idea what is happening... I don.t know it is because I am using 2010 in my own laptop but 2012 in my office pc, or this error just shows randomly!

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by