1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Solving Systems of Linear Equations (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Solving Systems of Linear Equations (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Solving Systems of Linear Equations (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Simple-Examples.html" rel="up" title="Simple Examples">
<link href="Integrating-Differential-Equations.html" rel="next" title="Integrating Differential Equations">
<link href="Matrix-Arithmetic.html" rel="prev" title="Matrix Arithmetic">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Solving-Systems-of-Linear-Equations">
<div class="nav-panel">
<p>
Next: <a href="Integrating-Differential-Equations.html" accesskey="n" rel="next">Integrating Differential Equations</a>, Previous: <a href="Matrix-Arithmetic.html" accesskey="p" rel="prev">Matrix Arithmetic</a>, Up: <a href="Simple-Examples.html" accesskey="u" rel="up">Simple Examples</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Solving-Systems-of-Linear-Equations-1"><span>1.2.4 Solving Systems of Linear Equations<a class="copiable-link" href="#Solving-Systems-of-Linear-Equations-1"> ¶</a></span></h4>
<p>Systems of linear equations are ubiquitous in numerical analysis.
To solve the set of linear equations <code class="code">A<var class="var">x</var> = b</code>,
use the left division operator, ‘<samp class="samp">\</samp>’:
</p>
<div class="example">
<pre class="example-preformatted"><var class="var">x</var> = A \ b
</pre></div>
<p>This is conceptually equivalent to
<code class="code">inv (A) * b</code>,
but avoids computing the inverse of a matrix directly.
</p>
<p>If the coefficient matrix is singular, Octave will print a warning
message and compute a minimum norm solution.
</p>
<p>A simple example comes from chemistry and the need to obtain balanced
chemical equations. Consider the burning of hydrogen and oxygen to
produce water.
</p>
<div class="example">
<pre class="example-preformatted">H2 + O2 --> H2O
</pre></div>
<p>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:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">x1*H2 + x2*O2 --> H2O
H: 2*x1 + 0*x2 --> 2
O: 0*x1 + 2*x2 --> 1
</pre></div></div>
<p>The solution in Octave is found in just three steps.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">octave:1> A = [ 2, 0; 0, 2 ];
octave:2> b = [ 2; 1 ];
octave:3> x = A \ b
</pre></div></div>
</div>
</body>
</html>
|