Debugging equations. vectorized vs. for-loop

  • Thread starter Pythagorean
  • Start date
  • Tags
    Debugging
In summary, the conversation discusses two different methods of solving a problem in MATLAB. The vectorized method produces incorrect results, while the for-loop method produces the desired results but is slower. The conversation also mentions a potential issue with dimension mismatch and further investigation of the problem.
  • #1
Pythagorean
Gold Member
4,403
314
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
  • #2
MATLAB, by the way.
 
  • #3
On first glance, why no ./ or .* in line 1 of the vectorized? Don't you get a dimension mismatch error?
 
  • #4
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.
 
  • #5
As a scientist, it is important to carefully examine and understand the differences between vectorized and for-loop methods of debugging equations. While both methods may produce different results, it is important to identify the source of the discrepancy and determine which method is more accurate and efficient.

First, it is important to understand the differences between vectorized and for-loop methods. Vectorized methods operate on entire arrays or matrices at once, while for-loop methods operate on each element individually. This means that vectorized methods can be more efficient for large datasets, but may also introduce errors if the data is not properly formatted. On the other hand, for-loop methods may be slower but can be more precise when dealing with individual elements.

In this case, it seems that the for-loop method is producing the correct results while the vectorized method is giving incorrect results. This could be due to a formatting error in the vectorized code, or it could be a more fundamental issue with the equations themselves. It is important to carefully check the equations and ensure that they are being applied correctly in both methods.

One strategy for identifying the source of the discrepancy is to compare the output of each method for a small subset of the data. This can help to pinpoint where the error is occurring and provide clues for how to fix it. It may also be helpful to consult with a colleague or seek out additional resources to gain a fresh perspective on the problem.

In conclusion, as a scientist, it is important to carefully examine and understand the differences between vectorized and for-loop methods when debugging equations. By carefully analyzing the results and seeking out additional help if needed, it is possible to identify and resolve any discrepancies and ensure the accuracy and efficiency of the equations.
 

Related to Debugging equations. vectorized vs. for-loop

1. What is the difference between vectorized and for-loop debugging?

Vectorized debugging involves manipulating and examining entire arrays or vectors of data at once, while for-loop debugging involves iterating through each element of the array or vector separately. This means that for-loop debugging may be more time-consuming and less efficient, but can also provide more specific and granular information about each individual element.

2. Which method is better for debugging equations?

The best method for debugging equations depends on the specific situation and the type of equation being debugged. In general, vectorized debugging is more efficient and can handle larger amounts of data, while for-loop debugging may be more precise and useful for identifying specific errors in the equation.

3. When should I use vectorized debugging?

Vectorized debugging is typically more useful when working with large datasets or when performing complex mathematical operations on arrays or vectors. It can also be helpful for quickly identifying and fixing errors in equations that involve a large number of variables.

4. Can I use both vectorized and for-loop debugging together?

Yes, it is possible to use both vectorized and for-loop debugging in combination for certain types of equations. This approach may be especially useful for equations that involve a mix of complex and simple operations, as it allows for efficient debugging of the overall equation while also providing more detailed information about specific elements.

5. What are some common challenges in debugging equations?

Debugging equations can be challenging due to the complexity and interdependence of mathematical operations. Common challenges may include identifying and fixing errors in the logic of the equation, dealing with unexpected data types or formats, and optimizing the efficiency and accuracy of the equation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
924
  • Introductory Physics Homework Help
Replies
3
Views
219
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
5K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
323
  • Mechanical Engineering
Replies
2
Views
1K
Replies
1
Views
658
Replies
1
Views
1K
  • Special and General Relativity
Replies
11
Views
1K
Back
Top