Octave – inizio – 3

CfT

Continuo da qui i primi esempi di Octave, seguendo il manuale, qui.

Soluzione di sistemi di equazioni lineari
Systems of linear equations are ubiquitous in numerical analysis. To solve the set of linear equations Ax = b, use the left division operator, ‘\’:

x = A \ b

This is conceptually equivalent to inv (A) * b, but avoids computing the inverse of a matrix directly.

If the coefficient matrix is singular, Octave will print a warning message and compute a minimum norm solution.

A simple example comes from chemistry and the need to obtain balanced chemical equations. Consider the burning of hydrogen and oxygen to produce water.

H2 + O2 → H2O

Nota: lo ottengo con AltGr+i
The equation above is not accurate. The Law of Conservation of Mass requires that the number of molecules of each type balance on the left- and right-hand sides of the equation. Writing the variable overall reaction with individual equations for hydrogen and oxygen one finds:

x1*H2 + x2*O2 → H2O
H: 2*x1 + 0*x2 → 2
O: 0*x1 + 2*x2 → 1

The solution in Octave is found in just three steps:

o20

Integrare equazioni differenziali
Octave has built-in functions for solving nonlinear differential equations of the form

o21

with the initial condition

o22

For Octave to integrate equations of this form, you must first provide a definition of the function f(x,t). This is straightforward, and may be accomplished by entering the function body directly on the command line. For example, the following commands define the right-hand side function for an interesting pair of nonlinear differential equations. Note that while you are entering a function, Octave responds with a different prompt, to indicate that it is waiting for you to complete your input.

o23

Given the initial condition

x0 = [1; 2];

and the set of output times as a column vector (note that the first output time corresponds to the initial condition given above)

t = linspace (0, 50, 200)';

it is easy to integrate the set of differential equations:

o24

oops! ma nel frame Spazio di lavoro ho

o25

Sono completamente all’oscuro di cosa sto trattando, il manuale dice che [t]he function lsode uses the Livermore Solver for Ordinary Differential Equations, described in A. C. Hindmarsh, ODEPACK, a Systematized Collection of ODE Solvers, in: Scientific Computing, R. S. Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pages 55–64.
La Wiki conosce lsode, e cita –indovina?– Octave 😀

Creare un output grafico
To display the solution of the previous example graphically, use the command

plot (t, x)

If you are using a graphical user interface, Octave will automatically create a separate window to display the plot.

o26

To save a plot once it has been displayed on the screen, use the print command. For example,

print -dpdf foo.pdf

yesss, produce come previsto il file foo.pdf, lamentandosi però:

o27

In alternativa alla creazione del PDF conviene salvare il plot; io l’ho salvato come .png, funziona; mi sa che si approfondirà a suo tempo.

Ecco la stessa cosa nella REPL:

o28

Uh! ecco una cosa che dovevo leggere: The command help print explains more options for the print command and provides a list of additional output file formats. No, il solito errore: sh: 1: most: not found, da fissare 😦

o29

Pausa 😀
:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: