plot system of ODE in Octave

Viewed 23

I have a matrix of ODE which I want to solve and plot.

x
x = (sym 2×1 matrix)

  ⎡            -331⋅π⋅t                 -61⋅π⋅t  ⎤
  ⎢            ─────────                ──────── ⎥
  ⎢               2299               2   10835   ⎥
  ⎢ 689986800⋅ℯ            89076600⋅π ⋅ℯ         ⎥
  ⎢ ──────────────────── + ───────────────────── ⎥
  ⎢       1307333                 12173551       ⎥
  ⎢                                              ⎥
  ⎢             -331⋅π⋅t                 -61⋅π⋅t ⎥
  ⎢             ─────────                ────────⎥
  ⎢                2299                   10835  ⎥
  ⎢  206492400⋅ℯ            139429800⋅π⋅ℯ        ⎥
  ⎢- ──────────────────── + ─────────────────────⎥

How can I plot both x(1) and x(2) in Octave?

Code for reproduction:

pkg load symbolic;
A = [-0.4, 0.4; 0.05, -0.07];
[M, Lambda] = eig(A);
t = sym('t');
Phi = M * expm(Lambda*t)*inv(M);
x = Phi * [600; 0]
1 Answers

There are two main approaches.

Have a look at the documentation for @sym/function_handle and @sym/eval.

Assuming you have created on your workspace the following domain:

t = -10:0.1:10;

Then here are examples of each approach:

  • The function handle approach:

    f1 = function_handle( x(1) );
    f2 = function_handle( x(2) );
    plot( t, f1(t) ); hold on;
    plot( t, f2(t) ); hold off
    
  • The eval approach:

    plot( t, eval( x(1) ) ); hold on
    plot( t, eval( x(2) ) ); hold off
    

The function_handle approach converts your expression to a regular numerical function, that you can pass arguments to (in this case, a valid 't').

The eval approach detects that your symbolic expression contains the variable "t", looks for "t" on your workspace, substitutes whatever's on your workspace into the symbolic expression, and then numerically evaluates the result.

Out of the two, the function handle approach is probably safest :)

Related