Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why my result is complex?

Subject: Why my result is complex?

From: Grzegorz Knor

Date: 31 Aug, 2010 21:29:04

Message: 1 of 3

Hi,
I have the following code:
-----------------------------------------
function test_ode
del = pi;
g = 0.6;
T=1/(del*(1-g));
t = 0:.01:T;

[tt yy] = ode45(@test_fun,t,0)

function dy = test_fun(t,y)
dy = del*(1-y).^g;
end
end
------------------------------------------

Thi code solve the equation:
dy/dt = del*(1-y)^g
Which analytical solution is:
y(t) = 1-((1-g)(del*t-c1))^(1/(1-g))
Having regard to the initial condition y(0) = 0:
y(t) = 1-(del*t*(g-1)+1)^(1/(1-g))

Now supose that
del = pi;
g = 0.6;
and time is:
T=1/(del*(1-g));
t = 0:.01:T;

For this vector t the analytical solution is real. So why the output from ode45 is complex?

Best wishes
Grzegorz

Subject: Why my result is complex?

From: Roger Stafford

Date: 31 Aug, 2010 23:18:06

Message: 2 of 3

"Grzegorz Knor" <gknor@o2.pl> wrote in message <i5js70$mgo$1@fred.mathworks.com>...
> Hi,
> I have the following code:
> -----------------------------------------
> function test_ode
> del = pi;
> g = 0.6;
> T=1/(del*(1-g));
> t = 0:.01:T;
>
> [tt yy] = ode45(@test_fun,t,0)
>
> function dy = test_fun(t,y)
> dy = del*(1-y).^g;
> end
> end
> ------------------------------------------
>
> Thi code solve the equation:
> dy/dt = del*(1-y)^g
> Which analytical solution is:
> y(t) = 1-((1-g)(del*t-c1))^(1/(1-g))
> Having regard to the initial condition y(0) = 0:
> y(t) = 1-(del*t*(g-1)+1)^(1/(1-g))
>
> Now supose that
> del = pi;
> g = 0.6;
> and time is:
> T=1/(del*(1-g));
> t = 0:.01:T;
>
> For this vector t the analytical solution is real. So why the output from ode45 is complex?
>
> Best wishes
> Grzegorz
- - - - - - - - - - -
  You are expecting too much of ode45. As y approaches one, as it must do at the endpoint T you have defined, it causes 1-y to approach zero. Only a tiny error in the computed y at this point can cause 1-y to go into the negative range and then you will get complex values for the next y. You had better stop a little before T if you don't want to run into that kind of trouble.

  As you can see in your analytic solution, the point where y becomes one is a point of singularity and infinitely many branches in the complex plane can be followed from there. That should give you a clue that this region is to be avoided in what you are doing.

Roger Stafford

Subject: Why my result is complex?

From: Grzegorz Knor

Date: 1 Sep, 2010 06:26:04

Message: 3 of 3

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i5k2je$am8$1@fred.mathworks.com>...
> You are expecting too much of ode45. As y approaches one, as it must do at the endpoint T you have defined, it causes 1-y to approach zero. Only a tiny error in the computed y at this point can cause 1-y to go into the negative range and then you will get complex values for the next y. You had better stop a little before T if you don't want to run into that kind of trouble.
>
> As you can see in your analytic solution, the point where y becomes one is a point of singularity and infinitely many branches in the complex plane can be followed from there. That should give you a clue that this region is to be avoided in what you are doing.
>
> Roger Stafford

Thank you Roger for your reply :) I understand it now.
best regards

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us