Debugging equations. vectorized vs. for-loop

  • Thread starter Thread starter Pythagorean
  • Start date Start date
  • Tags Tags
    Debugging
Click For Summary

Discussion Overview

The discussion revolves around discrepancies in results obtained from two different implementations of a mathematical function in MATLAB: one using vectorized operations and the other using a for-loop. The focus is on debugging the equations to identify the source of the differing outputs.

Discussion Character

  • Debugging
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant reports that the vectorized implementation yields a result of a4 = 0.5300, while the for-loop implementation gives a4 = 0.4572, indicating a discrepancy.
  • Another participant questions the absence of element-wise operations (./ or .*) in the vectorized code, suggesting it could lead to dimension mismatch errors.
  • A participant explains that MATLAB handles the vector in the numerator without issue, noting that only the 1./() was necessary, and mentions attempts to add element-wise operations without resolving the discrepancy.
  • Further analysis by a participant points to the term "y(2,:)" as potentially problematic, suggesting that the issue may not be directly related to the vectorized code itself but rather to the values being used in the ODE.
  • Participants express uncertainty about whether they are comparing the correct instances of the variables involved in the calculations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the source of the discrepancy, and multiple competing views regarding the implementation and potential issues remain unresolved.

Contextual Notes

There is mention of possible dimension mismatch errors and the need for element-wise operations, but the specific assumptions and dependencies on variable definitions are not fully clarified. The discussion also highlights uncertainty regarding the timing of comparisons in the code.

Pythagorean
Science Advisor
Messages
4,430
Reaction score
327
I get different results from these two functions. I need a fresh pair of eyes to help me find the discrepancy. The old way I did it, with the for-loop, gave proper results (but is slower, I think).VECTORIZED (wrong):

Code:
Nss = .5*(1+tanh((y(1,:)-V3)/V4)); %N as t --> inf
tau = 1./(phi*cosh((y(1,:)-V3)./(2*V4)));

dy(:,2) = (Nss-y(2,:))./tau;

a4 = (Nss(1)-y(2,1))./tau(1);

save newodeset a4

vectorized result:
a4 = 0.5300

FOR-LOOP (right):
Code:
 Nss = .5*(1+tanh((y(1)-V3)/V4)); %N as t --> inf
tau = 1/(phi*cosh((y(1)-V3)/(2*V4)));
    
dy(2) = (Nss-y(2))/tau;

a4 = a4 = (Nss-y(2))/tau;
for-loop result:
a4 = 0.4572
 
Physics news on Phys.org
MATLAB, by the way.
 
On first glance, why no ./ or .* in line 1 of the vectorized? Don't you get a dimension mismatch error?
 
Matlab doesn't have a problem with the vector in the numerator, so onlly the 1./() was necessary. I added the . in the second line out of desperation. Upon your suggestion, I also tried adding the . in but I get the same result (it diverges to Inf/NaN)

I also further dissected the (Nss-y(2,:))./tau term

and found the problem to be in the "y(2,:)" which is the second variable in the ODE, so it's possibly not even anything to do with this code. All the other values in the expression remain the same in both codes... maybe I'm not catching the right time to compare, I don't know.

This is just an excerpt of my whole code. I'd hoped I'd narrowed it down, but not sure anymore.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
1K
  • · Replies 0 ·
Replies
0
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K