Effacer les filtres
Effacer les filtres

Solving a Matrix of Independent Equations At the Same Time

1 vue (au cours des 30 derniers jours)
S H
S H le 15 Jan 2019
Commenté : madhan ravi le 15 Jan 2019
Could you help me fix the syntax of the solve function in the following codes? There should be at least as many answers as the size of the data grid.
syms a b x
[a,b] = ndgrid(-2:2:2,-3:3:3);
equ = x == a.*x - 9.*b.*x.^2 + 128;
answers=solve(equ,x)
It is better to avoid "for loops" to increase the speed. The actual grid data and equations are much more complicated than what I am showing here that cannot be symbilically solved.
It is okay to keep the solutions in a 3 by 3 cell matrix where each cell contains the solutions.
  3 commentaires
S H
S H le 15 Jan 2019
If the speed of two codes with the same functionality are measured using tic and toc, the code with "for loop" is always slower that the code with vector calculation.
Stephen23
Stephen23 le 15 Jan 2019
Modifié(e) : Stephen23 le 15 Jan 2019
"If the speed of two codes with the same functionality are measured using tic and toc, the code with "for loop" is always slower that the code with vector calculation."
That is incorrect. If vectorized code requires creating large intermediate arrays it can very easily be much slower that looped code, if it is possible at all. Most probably this also depends on the available memory and the MATLAB version too.

Connectez-vous pour commenter.

Réponse acceptée

madhan ravi
madhan ravi le 15 Jan 2019
syms a b x
equ = x == a.*x - 9.*b.*x.^2 + 128;
answers=solve(equ,x);
[a,b] = ndgrid(-2:2:2,-3:3:3);
Answers=matlabFunction(answers);
Answers(a,b) % final
  6 commentaires
S H
S H le 15 Jan 2019
Amazing. Thank you.
madhan ravi
madhan ravi le 15 Jan 2019
Anytime :)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by