68
votes
Accepted
why is A*v+B*v faster than (A+B)*v?
Except for code which does a significant number of floating-point operations on data that are held in cache, most floating-point intensive code is performance limited by memory bandwidth and cache ...
23
votes
More stable algorithm to calculate `sqrt(a^2 + b^2) - abs(a)` in MatLab
A manipulation that may help is the following. Assume for simplicity $a>0$. We have the identity
$$b^2 = (a^2+b^2)-a^2 = (\sqrt{a^2+b^2}-a)(\sqrt{a^2+b^2}+a),$$
hence
$$
\sqrt{a^2+b^2}-a = \frac{b^...
22
votes
why is A*v+B*v faster than (A+B)*v?
Your code is limited by memory bandwidth. For trivial math, it's often better to count memory accesses rather than flops. You'll get the following table:
...
21
votes
How much more work is it to code math models in Python, compared to working with Matlab?
Going from MATLAB to Python does introduce quite a bit of syntax overhead. One way to quantify it is the nice QuantEcon cheatsheet which showcases how there's a lot of extra "stuff" going on ...
18
votes
Accepted
What is the most efficient way to write 'for' loops in Matlab?
Short answer, you want to have the leftmost index on the innermost loop. In your example, the loop indices would go k, j, i and the array indices would be i, j, k. This has to do with how MATLAB ...
18
votes
Accepted
Why is RK45 used as the "default" method for non-stiff ODEs rather than a multistep one?
First, let's establish that they are a good choice. The SciMLBenchmarks are probably the most comprehensive that there are as of right now for modern methods. This uses the vast number of methods ...
17
votes
Accepted
More stable algorithm to calculate `sqrt(a^2 + b^2) - abs(a)` in MatLab
You can break down the domain of your function into three distinct cases:
$|a|\gg |b|$: In this case, $\sqrt{a^2+b^2} \approx |a|$ and a naive application of the formula will likely result in poor ...
16
votes
Why does the numerical solution of an ODE move away from an unstable equilibrium?
Note that $\pi/2$ is represented in double precision format in a way that is not exactly equal to $\pi/2$. It's only accurate to about 15 digits. Thus you're starting every so slightly away from the ...
15
votes
Why does Matlab's integral outperform integrate.quad in Scipy?
The question has two very different subquestions. I will address the first one only.
Matlab's version runs on average 24 times faster than my python
equivalent!
The second one is subjective. I ...
15
votes
Accepted
Is the exponential function, e^x, very expensive to compute in Matlab and harmful to my computer?
Computing the term $e^x$ is definitely significantly more expensive than computing a lower-order polynomial -- say $x^4$. But it may be ten to 100 times more expensive at most, not "crazy" expensive. ...
15
votes
Matrix multiplication accuracy Matlab vs Python
First, see Mark L. Stone's answers, which is completely correct. Second, realize that this is the reason why people told you to use relative errors in your numerical analysis class. :)
Third, the ...
15
votes
Accepted
Why does the numerical solution of an ODE move away from an unstable equilibrium?
I think the two main points have already been made by Brian and Ertxiem: your initial value is an unstable equilibrium and the fact that your numerical computations are never really exact provides the ...
14
votes
13
votes
Accepted
How much more work is it to code math models in Python, compared to working with Matlab?
There are libraries that you can use in Python that will give you all (or at least nearly all) of the functionality of MATLAB. For example, scipy.integrate.solve_ivp() supports a number of methods ...
12
votes
What is the most efficient way to write 'for' loops in Matlab?
A somewhat longer answer that explains why it's more efficient to have the left most index varying most rapidly. There are two key things that you need to understand.
First, MATLAB (and Fortran, ...
11
votes
What is “tolerance” in ODE45 in Matlab?
And, it is my understanding that the 4 and the 5 are for the order of the global and local error, respectively.
Your understanding is wrong.
The local error of a Runge–Kutta method of order $n$ is ...
11
votes
Accepted
What is the difference between MATLAB and FORTRAN?
When one uses a low–level programming language, e.g. C++ or FORTRAN, one essentially controls lots of things: how parameters are passed, how data structures are aligned in memory, what is the most ...
11
votes
Complex Eigenvalues using eig (Matlab)
You have an ill-conditioned eigenvalue problem. Consider a perturbation $\delta A$ in your matrix $A$—with double-precision floats this is $O(10^{-16})$. It turns the eigendecomposition from
$$ X^{-1}...
11
votes
Accepted
Generate random smooth 2D closed curves
Since your figure is a closed loop, its parametric curves $x(t)$ and $y(t)$ must be periodic functions. This suggests one way to generate such figures, by constructing random smooth periodic functions ...
10
votes
Accepted
How does gmres method iteration behave for this non-diagonalizable matrix?
Unfortunately, convergence of GMRES does not have a clear dependence on the distribution of eigenvalues.
It was proved by Greenbaum, Ptak and Strakos in 1996 that you can construct examples with an ...
9
votes
Nonlinear eigenvalue problem - MATLAB code
Given a nonlinear eigenvalue problem of the form $A(\lambda)x = 0$, reducing it to a real equation $\det(A(\lambda))=0$ is known to be a poor method for just the reason you've discovered yourself. The ...
8
votes
Accepted
Numerically computing the advection equation
I see several issues:
The DFT computed with fft puts the zero mode at the beginning of the array, and if you want to compute the derivative, it is necessary to ...
8
votes
Accepted
How to solve the problem without using symbolic computation
You can solve this numerically in Python without symbolic computation.
from __future__ import print_function, division
import numpy as np
from numpy import exp
from scipy.integrate import quad
from ...
8
votes
Accepted
Computational time not proportional to integration interval in ODE-solver?
Substantially edited, since the original poster changed his equation...
In general, the MATLAB (and Octave) ODE solvers dynamically adjust the step size as needed to maintain an accurate solution. ...
8
votes
Accepted
8
votes
What is the difference between MATLAB and FORTRAN?
In general, you will be much more productive writing software in a higher-level
language (e.g MATLAB) that has features useful in describing problems in your particular
domain (e.g. matrices in ...
8
votes
In Matlab, how can I be consistent with units?
Just simply by being consistent in all of my code?
Yes this is the only way. Matlab or any other programming language does not know about units. They only know about numbers.
As an example consider ...
8
votes
Accepted
In Matlab, how can I be consistent with units?
I would say that you have, mainly, two methods:
Being consistent in all your code, as already suggested in another answer. For that purpose, I always keep a table like this one with me, since it ...
8
votes
Numerical stability of higher order Zernike polynomials
A possible solution (suggested by @gammatester) is to use Jacobi polynomials. This circumvents the problem of catastrophic cancellation in adding the large polynomial coefficients by 'naive' ...
8
votes
Accepted
How to compute all the eigenvalues of a large sparse matrix using matlab?
"Get more RAM" may be one of your best options. :) Prices are reasonably low right now, and it's one of the best upgrades you can gift your computer anyway. 10k x 10k is borderline but still doable ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
matlab × 823ode × 91
pde × 75
finite-difference × 69
matrix × 67
numerics × 62
linear-algebra × 59
finite-element × 52
optimization × 38
python × 33
eigenvalues × 33
numerical-modelling × 32
sparse-matrix × 31
algorithms × 27
boundary-conditions × 27
nonlinear-equations × 27
plotting × 25
octave × 25
constrained-optimization × 24
computational-physics × 21
differential-equations × 21
linear-solver × 19
fluid-dynamics × 15
iterative-method × 15
performance × 14