solve return imaginary answer for a problem should have a real solution

4 次查看(过去 30 天)
Hi fellows,
I have written a code to solve a expotinal function, matlab returns an answer which seems like contain imaginary part. But I tried to plot the function and I think it indicates the function should has a real solution between 5 and 6. Could you help me find out what is the problem here? Really appreciated! The code is below (yes the loop seems unnecessary, because I just show a example so there is not need to let it do the whole loop).
%this is the maxmin when c2=0
sub=[0.3,0.3,0.3,0.08];
d1=[0.200000000000000,-0.400000000000000,0.600000000000000]
rn=size(d1,1)
lb1=sub(1);
lb2=sub(2);
lb3=sub(3);
r=sub(4);
for i=1:1
if (d1(i,1)==min(d1(i,:)))
p1=1-lb2-lb3;
p2=lb2;
p3=lb3;
elseif (d1(i,2)==min(d1(i,:)))
p1=lb1;
p2=1-lb1-lb3;
p3=lb3;
elseif (d1(i,2)==min(d1(i,:)))
p1=lb1;
p2=lb2;
p3=1-lb1-lb2;
end
syms x;
f=simplify(p1*d1(i,1)*exp(-r*x*d1(i,1))+p2*d1(i,2)*exp(-r*x*d1(i,2))+p3*d1(i,3)*exp(-r*x*d1(i,3)))
y=solve(f,x)
%x=-100:1:100;
%solve(0.06*exp(-0.016*x)-0.16*exp(0.032*x)+0.18*exp(-0.048*x),x)
%y=0.06*exp(-0.016*x)-0.16*exp(0.032*x)+0.18*exp(-0.048*x);
%plot(x,y)
end
  2 个评论
Walter Roberson
Walter Roberson 2013-1-20
Please show us what f comes out as before the solve() call, and also what y contains after the solve()
xueqi
xueqi 2013-1-20
f =
(3*exp((4*x)/125) - 8*exp((2*x)/25) + 9)/(50*exp((6*x)/125))
y =
(125*log(z1))/2 + 125*pi*k*i

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2013-1-20
It appears you are not being shown the complete solution. The general solution is of the form
(125/2)*ln(RootOf(-3*z^2+8*z^5-9, z))
Here RootOf(f(z),z) stands for the set of values of z such that f(z) becomes 0. Which is to say, the roots of a quintic. There is no general analytic solution for quintics.
The z1 that you are being shown in the solution is MuPAD's way of making the formula shorter, along the lines of saying,
"(125/2)*ln(z1) WHERE z1 = RootOf(-3*z^2+8*z^5-9, z)"
except that in R2010a, the interface between MuPAD and MATLAB does not know to show you the "WHERE z1 =" part.
What you should probably do is use
syms x real
and then in theory you should get the real root of the solution.

更多回答(3 个)

Matt J
Matt J 2013-1-20
编辑:Matt J 2013-1-20
Well, it does have a real solution doesn't it? It looks like for k=0, the solution returned to you is real. So what's the problem?
  1 个评论
xueqi
xueqi 2013-1-20
but according to to plot, it should has solution between 5 and 6. I prefer to an explicit anwers instead of this. To be honest I dont really understand the structure of the answers matlab returns...

请先登录,再进行评论。


Shashank Prasanna
Shashank Prasanna 2013-1-20
What version of MATLAB are you using? On MATLAB R2012a:
>> syms x
>> f = (3*exp((4*x)/125) - 8*exp((2*x)/25) + 9)/(50*exp((6*x)/125));
>> y=solve(f,x)
y =
5.6756938340216981622204109791808

Ankit Kumar Karmakar
I am facing the same problem on 2015a version, my pc is running on 32bit Win10 with 2Gb ram.
for testing purpose I made a simple coding
syms x;
a=2;
b=a+3;
c=a*b;
eq='4*x-((a-b)*(x-c)^4)+(c-x)';
f=solve(eq);
disp(f)
it is not even evaluating the values of a, b, c variables. Then the output is horrible.
RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[1]
RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[2]
RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[3]
RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[4]
Please tell us what to do
  1 个评论
Ankit Kumar Karmakar
then I tried real command on it, the output shows different value for f and x, but they should be equal.
output::::
Warning: The solutions are valid under the following conditions: in(RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 +
6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[1], 'real'); in(RootOf(b*z^4 - a*z^4 -
4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[2], 'real');
in(RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z + 4*a*c^3*z + 3*z + b*c^4 + c -
a*c^4, z)[3], 'real'); in(RootOf(b*z^4 - a*z^4 - 4*b*c*z^3 + 4*a*c*z^3 + 6*b*c^2*z^2 - 6*a*c^2*z^2 - 4*b*c^3*z +
4*a*c^3*z + 3*z + b*c^4 + c - a*c^4, z)[4], 'real'). To include parameters and conditions in the solution, specify the
'ReturnConditions' option.
> In solve>warnIfParams (line 517)
In solve (line 360)
In Untitled2 (line 6)
value of x
ans =
120
value of f
ans =
102
Code is as follows:
syms x real;
a=2;
b=a+3;
c=a*b;
eq='4*x-((a-b)*(x-c)^4)+(c-x)';
f=solve(eq,x);
disp ('value of x');
real 'x'
disp ('value of f');
real 'f'

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by