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:
ODEXX difference with linspace and logspace.

Subject: ODEXX difference with linspace and logspace.

From: Nicolas

Date: 26 May, 2012 20:53:24

Message: 1 of 8

Hello guys.

Is it possible that I get difference results using a ODE solver for instance ODE45 with if I use different spacing method to the spatial vector?

Well that is happening to me:

I define.

x=linspace(a,b,n);

and use that for solve some system with an initial condition.

But if I use everything equal but

x=logspace(log10(a),log10(b),n);

Then I get a different result, I think that the results need to be the same, right? (At least, in terms of the shape of the resulting curve, or not?)

Please help.

Subject: ODEXX difference with linspace and logspace.

From: Roger Stafford

Date: 26 May, 2012 21:22:07

Message: 2 of 8

"Nicolas " <nikkolazo@gmail.com> wrote in message <jprfs4$smd$1@newscl01ah.mathworks.com>...
> Is it possible that I get difference results using a ODE solver for instance ODE45 with if I use different spacing method to the spatial vector?
> Well that is happening to me:
> I define.
> x=linspace(a,b,n);
> and use that for solve some system with an initial condition.
> But if I use everything equal but
> x=logspace(log10(a),log10(b),n);
> Then I get a different result, I think that the results need to be the same, right? (At least, in terms of the shape of the resulting curve, or not?)
- - - - - - - - - - - -
  If I understand what you are saying, you are imposing the same initial conditions for the dependent variable or variables but on different values of the independent variable in the two cases. If so and if your differential equations involve the independent variable, you will of course receive different results. There is nothing remarkable about that.

  However, you do need to give full details of what you are doing. Include the 'odefun' function (the differential equations,) the span of the independent variable, and the initial conditions, for each of the two cases, so we can be sure of what you are doing.

Roger Stafford

Subject: ODEXX difference with linspace and logspace.

From: Nicolas

Date: 26 May, 2012 21:46:07

Message: 3 of 8


> - - - - - - - - - - - -
> If I understand what you are saying, you are imposing the same initial conditions for the dependent variable or variables but on different values of the independent variable in the two cases. If so and if your differential equations involve the independent variable, you will of course receive different results. There is nothing remarkable about that.

>
> However, you do need to give full details of what you are doing. Include the 'odefun' function (the differential equations,) the span of the independent variable, and the initial conditions, for each of the two cases, so we can be sure of what you are doing.
>
> Roger Stafford

Thanks Roger,

May be I am expressing wrong.

ok so the code is too big, I write you an example (if you want the complete code I can put it here)

I have
t0=0
dt=1
tf=10
tspan=t0:dt:tf

Initial condition:
u=exp(-x)

with x=linspace(a,b,n) a=1 b=10 (for instance)

And I need du/dt=c*du/dx + d*d/dx (du/dx)

so I use ode45(@function which give me du/dx at t)

If I do the same thing but now defining
x=logspace(log10(a),log10(b),n)

I get different (time) results.

For instance at time t=6 I have different results using the different spacing.

(if you do plot(x,exp(-x) using the different spacing, you will get the same curve with the same values.)

Subject: ODEXX difference with linspace and logspace.

From: Nasser M. Abbasi

Date: 26 May, 2012 22:08:41

Message: 4 of 8

On 5/26/2012 3:53 PM, Nicolas wrote:
> Hello guys.
>

> I define.
>
> x=linspace(a,b,n);
>
> and use that for solve some system with an initial condition.
>
> But if I use everything equal but
>
> x=logspace(log10(a),log10(b),n);
>
> Then I get a different result,

No. It is the same solution. Just different sampling locations.

When you use log space, the sample locations changes. Near 'a' they
are close to each others, and near 'b' they are far apart.

Depending on the function you are solving, this can make
it look different.

ode45 under the cover will sample more points, but it returns the
values at the sample locations you specified by your x vector.

Hence, when you plot it, it might look a little different, simply
because the sample points are located on different places.

With more sample points, you'll see you'll get the same plot.

increase 'n' to see that.

When using linespace, the sample locations are:

--------------------------------------------
K>> a=1; b=100; n=10;
K>> linspace(a,b,n)
   1 12 23 34 45 56 67 78 89 100
------------------------------------------------

when using logspace:

-------------------------------------------------
K>> logspace(log10(a),log10(b),n)

  1.0000 1.6681 2.7826 4.6416 7.7426 12.9155 21.5443 35.9381 59.9484 100
---------------------------------------------

To see this better, you can add an output function
to ode45 and see that it samples many points below the
cover, but then it will return the value at the sample
locations you asked for.

So, there is nothing really magic here. You are looking at the
same solution, just different interpolation points used
by the plot function.

here is an example. I used dy/dx = -3*exp(-x)
and used the linear vs. log space.

----------------------------------------
function first_oder_ode

% SOLVE dy/dx = -3 exp(-x).
% initial conditions: y(0) = 0

a=1; b=100; n=10;
x1=linspace(a,b,n);
x2=logspace(log10(a),log10(b),n);
initial_y=0;

figure;

options = odeset('OutputFcn',@output);
[x,y1]=ode45( @rhs, x1, initial_y,options);
subplot(2,1,1);
plot(x,y1);
xlabel('linespace');

fprintf('---------------\n');

[x,y2]=ode45( @rhs, x2, initial_y,options);
subplot(2,1,2);
plot(x,y2);
xlabel('logspace');

ylabel('y');
     
end


function dydx=rhs(x,y)
         dydx = 3*exp(-x);
         fprintf('in dydy x=%f, y=%f\n',x,y);
end

function status=output(x,y,~)

         fprintf('in output: x=%f, y=%f\n',x,y);
         status = false;
end

------------------------------------------


--Nasser

Subject: ODEXX difference with linspace and logspace.

From: Nicolas

Date: 26 May, 2012 22:22:07

Message: 5 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jprk9l$hcs$1@speranza.aioe.org>...
> On 5/26/2012 3:53 PM, Nicolas wrote:
> > Hello guys.
> >
>
> > I define.
> >
> > x=linspace(a,b,n);
> >
> > and use that for solve some system with an initial condition.
> >
> > But if I use everything equal but
> >
> > x=logspace(log10(a),log10(b),n);
> >
> > Then I get a different result,
>
> No. It is the same solution. Just different sampling locations.
>
> When you use log space, the sample locations changes. Near 'a' they
> are close to each others, and near 'b' they are far apart.
>
> Depending on the function you are solving, this can make
> it look different.
>
> ode45 under the cover will sample more points, but it returns the
> values at the sample locations you specified by your x vector.
>
> Hence, when you plot it, it might look a little different, simply
> because the sample points are located on different places.
>
> With more sample points, you'll see you'll get the same plot.
>
> increase 'n' to see that.
>
> When using linespace, the sample locations are:
>
> --------------------------------------------
> K>> a=1; b=100; n=10;
> K>> linspace(a,b,n)
> 1 12 23 34 45 56 67 78 89 100
> ------------------------------------------------
>
> when using logspace:
>
> -------------------------------------------------
> K>> logspace(log10(a),log10(b),n)
>
> 1.0000 1.6681 2.7826 4.6416 7.7426 12.9155 21.5443 35.9381 59.9484 100
> ---------------------------------------------
>
> To see this better, you can add an output function
> to ode45 and see that it samples many points below the
> cover, but then it will return the value at the sample
> locations you asked for.
>
> So, there is nothing really magic here. You are looking at the
> same solution, just different interpolation points used
> by the plot function.
>.......


Thanks Nassier,
So the problem is in my code. Because I get different solutions.

I will show you:

http://s13.postimage.org/ofg1crnzr/problem.png


So, I need to see what is happening, but is not fault of the ODE solver (right?)
The problem is that, I don't know which one is the correct one.

Subject: ODEXX difference with linspace and logspace.

From: Nasser M. Abbasi

Date: 27 May, 2012 00:21:43

Message: 6 of 8

On 5/26/2012 5:22 PM, Nicolas wrote:

>
> Thanks Nassier,
> So the problem is in my code. Because I get different solutions.
>
> I will show you:
>
> http://s13.postimage.org/ofg1crnzr/problem.png
>
>
> So, I need to see what is happening, but is not fault of the ODE solver (right?)
> The problem is that, I don't know which one is the correct one.

I do not know what your code looks like. Unless you made a mistake
in initialization, then as I said before, the x-vector
will not make a difference to ode45 in terms of how it find
the solution as it uses it own adaptive step size internally.

It will make a difference in terms of what points it will
sample the solution at.

If your solutions changes much more rapidly near the end
of the domain, then with logspace, you'll not see the
solution as clearly as with the linear space samples, since
there are less sample points there in the logspace case.

Plot() can only use the data you give it.

Why not just use uniform x-spacing? Why do you need to use
log spacing for the x-vector?

--Nasser

Subject: ODEXX difference with linspace and logspace.

From: Roger Stafford

Date: 27 May, 2012 02:33:35

Message: 7 of 8

"Nicolas " <nikkolazo@gmail.com> wrote in message <jpriuv$b5i$1@newscl01ah.mathworks.com>...
> I have
> t0=0
> dt=1
> tf=10
> tspan=t0:dt:tf
>
> Initial condition:
> u=exp(-x)
>
> with x=linspace(a,b,n) a=1 b=10 (for instance)
>
> And I need du/dt=c*du/dx + d*d/dx (du/dx)
- - - - - - - - - -
  I don't understand the explanation you gave in your second article, Nicolas. For an initial condition you wrote:

"Initial condition:
u=exp(-x)"

That doesn't look like an initial condition to me. Which is your dependent variable, u or x, and what is its value at the initial value of t?

  Also you have written

 "du/dt=c*du/dx + d*d/dx (du/dx)"

for your ordinary differential equation. I see two independent variables here, t and x, where there should be only one. What role does x play here? Is it some kind of parameter? If so, why are you using a derivative taken with respect to it?

  What we should see is some kind of differential equation like

 dy/dt = f(t,y) ( or d^2y/dt^2 = f(t,y,dy/dt) )

or the like, where 'f' is some function you have defined and y is the dependent variable, and the initial values of y at the initial value of t should be explicitly given as a numerical value.

  How about making another try at the detailed explanation I asked you for? I don't think you will receive a satisfactory answer to your query until you do.

Roger Stafford

Subject: ODEXX difference with linspace and logspace.

From: Nicolas

Date: 27 May, 2012 20:49:06

Message: 8 of 8

 it.
>
> Why not just use uniform x-spacing? Why do you need to use
> log spacing for the x-vector?
>
 --Nasser

Thanks again Nasser.
I need the logspace just as an improvement to my code. The solution does change a lot in the beginning of the x array (near to zero).

Well I am using the MOL (Method of lines) to solve a diffusion equation:
http://tinyurl.com/6pw7z6o (the first one here)

So first I created a function file with deals with the derivates.
so at the end of that function I obtain

du/dx. And I pass that to the ode solver.


-----------------------------------------------------------
  What we should see is some kind of differential equation like

 dy/dt = f(t,y) ( or d^2y/dt^2 = f(t,y,dy/dt) )

or the like, where 'f' is some function you have defined and y is the dependent variable, and the initial values of y at the initial value of t should be explicitly given as a numerical value.

  How about making another try at the detailed explanation I asked you for? I don't think you will receive a satisfactory answer to your query until you do.

Roger Stafford
----------------------------------------------------------

So trying to answer to Roger, MOL allow me to treat the problem more simplier.

The equation (is more clear in the link above)

is of the form

du/dt=1/x d/dx (x^0.5 d/dx( nu x^0.5 u ) )

So I take the first derivative group (in this function file for ode45)
function odeforode45(u)

deriv1= d/dx( nu x^0.5 u )
%and I calculate that numerically of course (with a routine of finite differences, ds004)

%then I took that result and calculate the other group
deriv2= d/dx (x^0.5 deriv1 )

%finally i get the last group

dudx= 1/x * deriv2

call=call+1; %This is for the call of this function.
end

This way I am able to obtain the solution of the equation. (of course I use an initial condition for u. (u0=a*exp(b-x)^-1 )

In the first part of odeforode45 I put the BC condition u(1)=constant1 u(n)=constan2

and that is. (Is it more clear now? please ask me anything).


Well, I have another question now. Why ODE45 (in the solution) is not respecting my BC conditions?
 I am expecting that the solution of u(t) has u(1) and u(n) equal at every time step but this is not happening (look the blue curve at t=10 in the previous link, the first point is really high while the first point of the initial condition is the "constant1"). The "funny" thing is while the solver is running, the BC are correct, I save the values while the solver is running and I have u(1) and u(n) constant until the end. But in the solution is not.

What is happening ?

Thanks again

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