Tag Info

Hot answers tagged matlab

66 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 ...
• 17.6k
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 ...
• 11.6k
21 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: ...
• 346
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 ...
• 1,097
17 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 ...
• 11.6k
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 ...
• 17.6k
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 ...
• 151
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. ...
• 50.7k
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 ...
• 1,198
14 votes

Matrix multiplication accuracy Matlab vs Python

Here is R1, as computed in MATLAB: ...
• 2,002
14 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 ...
• 8,696
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 ...
• 17.6k
12 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.4k
11 votes
Accepted

Visualising Maxwell's equations using MATLAB

Here I have an example: ...
• 8,006
11 votes

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 ...
• 4,521
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 ...
• 1,839
11 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, ...
• 17.6k
10 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 ...
• 861
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 ...
• 8,696
9 votes

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 ...
• 4,521
9 votes

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 ...
• 1,728
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 ...
• 11.4k
9 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 ...
• 4,316
8 votes
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 ...
• 8,696
8 votes
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 ...
• 50.7k
8 votes

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 ...
• 29.8k
8 votes

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 ...
• 50.7k
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 ...
• 11.4k
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. ...
• 17.6k

Only top scored, non community-wiki answers of a minimum length are eligible