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

2 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 20 Jan 2013
Please show us what f comes out as before the solve() call, and also what y contains after the solve()
xueqi
xueqi on 20 Jan 2013
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

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jan 2013
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.

More Answers (3)

Matt J
Matt J on 20 Jan 2013
Edited: Matt J on 20 Jan 2013
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 Comment
xueqi
xueqi on 20 Jan 2013
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...

Sign in to comment.


Shashank Prasanna
Shashank Prasanna on 20 Jan 2013
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
Ankit Kumar Karmakar on 18 Nov 2018
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 Comment
Ankit Kumar Karmakar
Ankit Kumar Karmakar on 18 Nov 2018
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'

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!