solve return imaginary answer for a problem should have a real solution
Show older comments
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
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
on 20 Jan 2013
Accepted Answer
More Answers (3)
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?
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
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
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'
Categories
Find more on Common Operations 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!