<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258185</link>
    <title>MATLAB Central Newsreader - vectorizing a for loop in Euler's method (ODE solver)</title>
    <description>Feed for thread: vectorizing a for loop in Euler's method (ODE solver)</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 11 Aug 2009 02:28:05 -0400</pubDate>
      <title>vectorizing a for loop in Euler's method (ODE solver)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258185#672092</link>
      <author>Phil Isaac</author>
      <description>As an educational exercise, I'd like to know if the for loop in the following M-file code can be vectorized:&lt;br&gt;
&lt;br&gt;
%%%&lt;br&gt;
t0=0;&lt;br&gt;
y0=4;&lt;br&gt;
tMax = 5;&lt;br&gt;
steps= 20;&lt;br&gt;
rhsFunc = @(t,y)(t.* y.^2 .* sin(2*y.*t));&lt;br&gt;
y=zeros(1,steps+1);&lt;br&gt;
y(1) = y0;&lt;br&gt;
deltaT = (tMax-t0)/steps;&lt;br&gt;
t = t0:deltaT:tMax; &lt;br&gt;
for i = 1:steps&lt;br&gt;
&amp;nbsp;&amp;nbsp;y(i+1) = y(i) + rhsFunc(t(i),y(i))*deltaT;&lt;br&gt;
end&lt;br&gt;
plot(t,y);&lt;br&gt;
%%%&lt;br&gt;
&lt;br&gt;
This is an implementation of Euler's method, a simple first order ODE solver.&lt;br&gt;
&lt;br&gt;
I understand how to vectorize a loop using &quot;filter&quot; (or in some cases &quot;cumsum&quot; or &quot;cumprod&quot;) that creates a vector whose elements depend on the previous element, in the case where those elements depend *linearly* on the previous elements. That is explained clearly in Support - Code Vectorization Guide:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/support/tech-notes/1100/1109.shtml&quot;&gt;http://www.mathworks.com/support/tech-notes/1100/1109.shtml&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
In the example code that I have provided, I have purposely made &quot;rhsFunc&quot; a non-linear function of y. I am having trouble working out how to vectorize that loop in this case, or indeed in the general case where &quot;rhsFunc&quot; is any (smooth) non-linear function of two variables, in particular the variable y.&lt;br&gt;
&lt;br&gt;
Some additional comments:&lt;br&gt;
- I know about the ODE solvers ode23, ode45, etc. I'm more interested in how to vectorize, rather than the context.&lt;br&gt;
- The code I have provided already works fine. I have used a small number of steps, but if you change to &quot;steps = 20000;&quot;, the plot produced is not bad (compare with ode45) and still runs in good time (of course not as efficient as Runge-Kutta!), and so there may be no point in vectorizing at all. Again, I'm interested in the question of how to vectorize, rather than the context.&lt;br&gt;
&lt;br&gt;
Cheers,&lt;br&gt;
&lt;br&gt;
Phil</description>
    </item>
    <item>
      <pubDate>Tue, 11 Aug 2009 07:44:03 -0400</pubDate>
      <title>Re: vectorizing a for loop in Euler's method (ODE solver)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258185#672129</link>
      <author>Bruno Luong</author>
      <description>I have my doubt non-linear iterative can be vectorized.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
  </channel>
</rss>

