MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   How to enforce element-wise condition in matrix operations? (https://www.labfans.com/bbs/showthread.php?t=22066)

poster 2019-11-24 21:00

How to enforce element-wise condition in matrix operations?
 
<p>I am trying to use <code>\</code> for Euler's backward method:</p>

<p><a href="https://i.stack.imgur.com/XpXL3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XpXL3.png" alt="enter image description here"></a></p>

<p>where <code>dt</code> and <code>lambda</code> are constant numbers like:</p>

<pre class="lang-matlab prettyprint-override"><code>dt = 0.01
lambda = 0.5
</code></pre>

<p>and <code>L</code> is a sparse square matrix with the same size as <code>x_old</code> and <code>x_new</code></p>

<p>Here is what I have tried:</p>

<pre class="lang-matlab prettyprint-override"><code>x_new = (speye(size(x_old,2))- dt * lambda * L) \ x_old;
</code></pre>

<p>I had three questions:</p>

<ol>
<li>How to avoid the division in the elements of L that are 0?</li>
<li>Is it OK to move <code>(speye(size(x_old,2))- dt * lambda * L)</code> to the
other side, or should I maintain the formula's structure?.</li>
<li>Am I supposed to use <code>.*</code> or <code>.\</code> anywhere?</li>
</ol>

<p>For the first question, I tried this but didn't work, because of the the matrix dimensions:</p>

<pre class="lang-matlab prettyprint-override"><code>x_new = (speye(size(x_old,2))- dt * lambda * L(L~=0)) \ x_old;
</code></pre>

<p>For example, for these matrices:</p>

<pre class="lang-matlab prettyprint-override"><code>full(L) = [4.6188 1.1547 1.1547 1.1547 1.1547;
1.1547 2.3094 0.5774 0 0.5774;
1.1547 0.5774 2.3094 0.5774 0;
1.1547 0 0.5774 2.3094 0.5774;
1.1547 0.5774 0 0.5774 2.3094]

x_old = [-0.4000 0 0.5000;
0.1000 -0.5000 0.5000;
0.1000 0 1.0000;
0.1000 0.5000 0.5000;
0.1000 0 0]
</code></pre>

<p>the result is like this:</p>

<pre class="lang-matlab prettyprint-override"><code>x_new = [-0.407106857205753 8.52491160610484e-19 0.523921051079662;
0.0993707696612247 -0.505840945396493 0.511891327878594;
0.0993707696612247 3.20963195216710e-19 1.01773227327509;
0.0993707696612247 0.505840945396493 0.511891327878594;
0.0993707696612247 5.22659462097013e-19 0.00605038248210024]
</code></pre>

<p>which in command window looks like this:</p>

<pre class="lang-matlab prettyprint-override"><code>x_new =

-0.4071 0.0000 0.5239
0.0994 -0.5058 0.5119
0.0994 0.0000 1.0177
0.0994 0.5058 0.5119
0.0994 0.0000 0.0061
</code></pre>



[url=https://stackoverflow.com/questions/59017828/how-to-enforce-element-wise-condition-in-matrix-operations]More...[/url]


所有时间均为北京时间。现在的时间是 19:53

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.