# Tag Info

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 ...

### 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 ...

### 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: ...
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 ...
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 ...

### 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 ...

### 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 ...
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. ...
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 ...

### Matrix multiplication accuracy Matlab vs Python

Here is R1, as computed in MATLAB: ...

### 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 ...
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 ...

### 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}...
Accepted

### Visualising Maxwell's equations using MATLAB

Here I have an example: ...

### Why is my MATLAB code for back-substitution slower than the backslash operator?

MATLAB's \ (aka mldivide) command does not blindly compute the inverse of the matrix. Instead, it uses one of several algorithms ...

### 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 ...

### 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, ...
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 ...
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 ...

### How to find all roots of equation in Matlab?

Before trying to find all of the roots of this function in MATLAB I think it's worth understanding that it has infinitely many roots due to the inclusion of the $\cos()$ term. Additionally, it is easy ...

### Approximate $h$ in $F(\theta)=\sin \theta \int_{-L}^{+L}h(z)e^{-ikz\cos \theta} \,dz$

This is a linear integral equation (because the right hand side is linear in $h$) and presumably the problem is ill-posed (for one, because of the multiplication by $\sin(\theta)$ but also because the ...

### 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 ...
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 ...
Accepted

### Is R or Matlab currently faster?

The benchmarks on the Julia website 1 2 include R and Matlab as competitors. Note that these are benchmarks focusing on testing the pure speed of the language, not the quality of the underlying ...
Accepted

### Projecting Finite Element solution onto new mesh

When you want to say that you want $u_2$ to be the best approximation of $u_1$ on mesh ${\cal T}_2$, then you have to define what you mean by "best". Let's assume you define it as that function that ...

### Incremental SVD implementation in MATLAB

Yes. Christopher Baker has implemented his incremental SVD method in a MATLAB package called IncPACK (archived on GitHub, within the imtsl project). It implements methods that are described in his ...

### Efficiency of an algorithm

The usual approach to testing optimization algorithms is to compare how many function evaluations they need to find the minimum (or get within a fixed tolerance $\varepsilon$ of the minimum). This is ...
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 ...