# Expm versus ode

**URL:** <https://scilab.discourse.group/t/expm-versus-ode/802>\
**Category:** Modeling and simulation\
**Created:** [February 2, 2025, 2:42pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802 "2025-02-02T14:42:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Jens](https://scilab.discourse.group/u/Jens)\
**Post date:** [February 2, 2025, 2:42pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802/1 "2025-02-02T14:42:18Z")

</div>

Hello Scilab community,  
The following script is to solve the homogeneous ODE z'=Az with initial value z\_0. arkode does what I expect, expm does not. What has to be changed with the expm way?

```auto
//ode_vs_expm.sce
clear
clc()
mode(0)
funcprot(0);
format('v',8) // Crashtrigger      
 
function z_=dz(t,z)
  z_=A*z
endfunction

A =[-0.005 -5.; 32 -63.];//sytem matrix
z0=[1;0];//initial values
n=1000;
t=linspace(0,10,n);

detA=det(A)
eig=spec(A)
sol= arkode(dz,t,z0,method="ERK_8",rtol=1.D-8);//DGl.-Solver
//sol = cvode(!z_,t,z0);//DGl.-Solver
[z,z_] = sol(t);
close(winsid())
subplot(1,2,1)
plot(t,z(1,:),'r')
plot(t,z(2,:),'b')
gca.x_location='origin';
xtitle('arkode');gca().title.font_size=4;

for k=1:n
  zexp(:,k)=expm(A*t(k))*z0;
end
subplot(1,2,2)
plot(t,zexp(1,:),'r')
plot(t,zexp(2,:),'b')
xtitle('expm');gca().title.font_size=4;
gca.x_location='origin';

```

Regards Jens

---

<div class="post-metadata">

**Author:** ![Jens](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Jens](https://scilab.discourse.group/u/Jens)\
**Post date:** [February 3, 2025, 10:02am UTC](https://scilab.discourse.group/t/expm-versus-ode/802/2 "2025-02-03T10:02:15Z")

</div>

Comparison  
Scilab:  
 → expm([-0.005 -5.;32 -63.]\*10)  
ans =  
-0.02633 0.04967  
-0.31788 0.59944

Octave:

> > expm([-0.005 -5.;32 -63.]\*10)  
> > ans =  
> > 3.0377e-12 -2.5170e-13  
> > 1.6109e-12 -1.3348e-13

Octave meets my expectation.

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [February 3, 2025, 10:38am UTC](https://scilab.discourse.group/t/expm-versus-ode/802/3 "2025-02-03T10:38:42Z")

</div>

Hello.

What do you want to do exactly ? Compute the matrix exponential of a matrix (by itself), or compute the solution of an homogeneous system of linear differential equations ? Using the former to do the latter has always been a bad idea, and using an ODE solver is the best approach. Using the matrix exponential is eventually OK if you need to compute the solution at regularly spaced time points, e.g. t\_k=kh, in that case you just have to compute e^{Ah} then compute iteratively z\_(t\_{k+1})=e^{Ah}z\_(t\_{k}) . But computing precisely e^{At} for an arbitrary t depends on the method that is used in the software your are using. Scilab uses Padé aproximants (as other similar software), but the implementation is different that the one used e.g. in Octave or SciPy.

S.

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [February 3, 2025, 2:37pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802/4 "2025-02-03T14:37:19Z")

</div>

BTW, which version of Scilab are you using (and on which platform/OS) ? With Scilab 2025 on macOS I have the following result:

```scilab
--> expm([-0.005 -5.;32 -63.]*10)

 ans = [2x2 double]

   3.038D-12 -2.517D-13
   1.611D-12 -1.335D-13

```

which is almost the same as Octave’s result.

S.

---

<div class="post-metadata">

**Author:** ![Jens](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Jens](https://scilab.discourse.group/u/Jens)\
**Post date:** [February 3, 2025, 3:20pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802/5 "2025-02-03T15:20:18Z")

</div>

I use Version 2024.0.0 under Windows 10.

My focus is on the ODE, regarding expm as an elegant tool only. If its implementation like in Octave or Matlab does not entail other disadvantages I would appreciate it in Scilab too.

Your advice which leads to  
n=1000;  
t=linspace(0,10,n);  
h=t(2)-t(1);  
zexp=z0  
eAh=expm(A \* h)  
for k=2:n  
zexp(:,k)=eAh \* zexp(:,k-1);  
end  
works perfectly, but at the end of the day nobody would be happy if (as analogy) the scalar e-function had to used in a comparable manner. If expm stays as it is I would suggest a caveat in the documentation.

Thanks a lot for your helpful response!  
Regards  
Jens

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [February 3, 2025, 3:30pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802/6 "2025-02-03T15:30:09Z")

</div>

Please consider my previous post, there seems to be a problem with expm() in Scilab 2024.0.0, which has been fixed since 2024.1.0 version :

 ![Screenshot 2025-02-03 at 16.28.21](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/32eba96963175e45de2dbc216be5e8fd29c7d576.png)

Please use latest version of Scilab.

S.

---

<div class="post-metadata">

**Author:** ![Jens](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Jens](https://scilab.discourse.group/u/Jens)\
**Post date:** [February 3, 2025, 3:47pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802/7 "2025-02-03T15:47:02Z")

</div>

I will use 2024.1.0 version. So my expectation of expm was not too optimistic.

```auto
And for code I will use Preformatted text in the future.

```

Regards  
Jens

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [February 3, 2025, 3:51pm UTC](https://scilab.discourse.group/t/expm-versus-ode/802/8 "2025-02-03T15:51:34Z")

</div>

> [@Jens](#):
>
> ```auto
> And for code I will use Preformatted text in the future.
> 
> ```

Thanks. Otherwise single and double quotes (among other characters) are rendered as different unicode characters not recognized by Scilab.

S.
