Iteration in MATLAB. Please save me!?


Question:
I am not too bad at programming, but this little annoyance in MATLAB is driving me crazy, and I need to finish this off asap (my advisor is leaving for holiday in 2 days!).

I am interested in summing up the resultant amplitude from a population of oscillators, each of which is a solution of the van der Pol equation with parameter u>0 with the following property: the time periods of the ensemble are Gaussian-ly distributed with mean T and SD s.

I wish to use MATLAB to evaluate this sum. I can evaluate the v.d.P. solution in a function, can generate random normally distributed numbers using randn() but somehow the eventual summing up which involves an iterative call to the ODE function from a main fucntion is not giving me the sum correctly. I have tried making the sum a global variable too. I could have posted the code too, as evidence that I am not askng you to do my homewoek, but my advisor will not like that.

Please help!!

Answer:
I'm not familiar with the van der Pol equation, and even less familiar with your problem, so I can only offer possibilities. But I think they're good ones. ;-)

For a start, much depends on whether you need to evaluate the solutions once only or repeatedly for each element in the ensemble. If once, you may be able to get away with assigning the time period randomly on the fly, but if you need to evaluate them repeatedly (perhaps to get values at many points) this won't work very well.

Also, remember the fundamental principle of MATLAB: Never iterate if you can use an array. Probably you can't use an array to do all your oscillators at once (though if you can, you definitely should!) - but I'll assume you can compute a whole vector of points for each oscillator in a single function call.

So with that in mind, for N oscillators and n data points per solution I'd propose something like

times = randn(1, N) * s + T;
sols = zeros(n, N);
xvals = (x0 : xstep : x1)';

for i = 1:N
sols(:, i) = van_der_pol(times(i), xvals);
end
sums = sum(sols, 2);

where van_der_pol should obviously be replaced by whatever you've called your M-file.

Bear in mind that it's about 4 years since I last used MATLAB and therefore the above code is untested, but it should be pretty close. Fortunately MathWorks have the MATLAB function reference available on the Web. ;-)
I once had a problem kind of like this, and I think that what fixed it was defining the basic function in a separate m file. Not exactly sure, though. Let me know if it works.
More Questions & Answers...
  • Masters in counseling?
  • Illegal Alien Loving anti War Libs what is your response to this?
  • Site advisor program?
  • 4 whoever who knoes about Mozilla Firefox or noes how to program it pls answer dis question...?
  • Is CNN a liberal news Network?
  • Teachers..interdisciplinary units...?
  • I am looking at becoming a financial advisor. Anybody have advice on career path/ best companies to work for?
  • Is DevRy good for online education?
  • The questions and answers post by the user, for information only, AnswersRoom.com does not guarantee the right
    Copyright © 2007 AnswersRoom.com -   Terms of Use -   Contact us

    Hot Topic