Matlab solve function in for loops

Click For Summary

Discussion Overview

The discussion revolves around the use of the Matlab "solve" function within nested "for" loops to solve a matrix of equations. Participants are exploring issues related to symbolic computation, particularly with a matrix defined in terms of a single variable and the implications of that on indexing and solving equations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a need to solve for 'x' in a 2x2 matrix 'y' where each element is set to zero, leading to a matrix of solutions.
  • Another participant questions the definition of 'y' as a matrix of one variable and expresses uncertainty about the meaning of 'y(a,b)' in this context.
  • A participant clarifies that 'y' is a random matrix and relates the problem to a larger matrix involving voltage angle differences, indicating that the structure is similar but more complex.
  • One suggestion involves redefining 'y' to include multiple symbolic variables, but it leads to similar errors regarding invalid indexing.
  • Errors encountered include warnings about multiple equations in one variable and invalid index errors during evaluation, indicating challenges in the implementation of the solve function within loops.

Areas of Agreement / Disagreement

Participants express differing views on the structure of the matrix and the implications for solving equations. There is no consensus on how to resolve the issues presented, as multiple approaches are suggested but lead to errors.

Contextual Notes

Participants highlight limitations related to the definition of the matrix and the indexing used in the solve function, which may depend on the specific structure of the equations being solved. The discussion remains focused on the technical challenges without resolving the underlying issues.

NJP
Messages
3
Reaction score
0
Matlab "solve" function in "for" loops

I need to solve a similar problem like shown in the below codes for a larger matrix,
The 'x' here needs to be solved for each y(a,b). Each of this y(a,b) is equal to zero and 'x' will vary accordingly , so it will give a 2by 2 matrix for 'x' as well.

syms x;
y = [sin(x)+5 3*cos(x)-4
cos(x)-1 cos(x)-0.6];

C=zeros(2,2);
for a=1:2
for b=1:2

B(a,b)= solve('y(a,b)=0',x);
%B(a,b)=solve('y(a,b)=C(a,b)',x);
end
end

When I try to solve it without the loop it gives the following error,
(Warning: 4 equations in 1 variables.
Warning: Explicit solution could not be found.
> In solve at 81)
When I try with the loop it gives me the following error,
? Error using ==> mupadmex
Error in MuPAD command: Invalid index
  • ;

    during evaluation of 'matchNonSingletonLHS'

    Error in ==> sym.sym>sym.subsasgn at 1420
    C = mupadmex('mllib::subsasgn',A.s,B.s,inds{:});


    It will be great if someone can give me a clue to solve this problem.
 
Physics news on Phys.org


I don't often use the symbolic toolbox, but I think the issue is that you've defined y to be a 2x2 matrix of one variable (x).
I am not even sure what you think y(a,b) would mean in this case?
 


Thanks for the reply. Like I said y here is just a random matrix. My actual problem needs to be solved for voltage angle difference between busbars and contains a 96by10 matrix. However the the matrix I need to solve is quite similar to 'y'. But each element contains larger first order trigonometric equations.
 


Try this:

syms x,y,z,w;
y = [sin(x)+5 3*cos(y)-4
cos(z)-1 cos(w)-0.6];

C=zeros(2,2);
for a=1:2
for b=1:2

B(a,b)= solve('y(a,b)=0',x);
B(a,b)= solve('y(a,b)=0',y);
B(a,b)= solve('y(a,b)=0',z);
B(a,b)= solve('y(a,b)=0',w);
end
end
 


It gives me the below errors,
:(

?? Error using ==> mupadmex
Error in MuPAD command: Invalid index
  • ;

    during evaluation of 'matchNonSingletonLHS'

    Error in ==> sym.sym>sym.subsasgn at 1420
    C = mupadmex('mllib::subsasgn',A.s,B.s,inds{:});

    Error in ==> Untitled2 at 9
    B(a,b)= solve('y(a,b)=0',x);
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
3K