Solving a Matrix of Independent Equations At the Same Time

1 view (last 30 days)
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 Comments
S H
S H on 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 on 15 Jan 2019
Edited: Stephen23 on 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.

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!