Matlab solve function in for loops

In summary: 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);endendIn summary, the user is trying to solve a problem that requires a 96x10 matrix, but is using a similar 2x2 matrix to y. Each element of y is equal to zero, but the user's actual problem contains larger first order trigonometric equations. When the user tries to solve the problem without the loop, it gives an error, and when the user tries to solve the problem with the loop, it gives
  • #1
NJP
3
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
  • #2


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?
 
  • #3


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


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
 
  • #5


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);
 

What is the syntax for using the "solve" function in Matlab for loops?

The syntax for using the "solve" function in Matlab for loops is: solve(equations, variables). This will return a symbolic solution for the given equations and variables.

What is the purpose of the "solve" function in Matlab for loops?

The "solve" function in Matlab for loops is used to find symbolic solutions to a system of equations. It can be helpful in solving mathematical problems or in creating more efficient and accurate code.

How can I use the "solve" function in a for loop to solve multiple equations?

The "solve" function can be used in a for loop by first creating a vector of equations and a vector of variables. Then, within the loop, the function can be called with the current equations and variables as inputs. The solutions can be stored in a vector or matrix for later use.

What happens if the "solve" function in a for loop cannot find a solution?

If the "solve" function in a for loop cannot find a solution, it will return an empty matrix. This can happen if the equations are inconsistent or if there are no real solutions to the system of equations.

Can I use the "solve" function in a for loop to solve non-linear equations?

Yes, the "solve" function in a for loop can be used to solve non-linear equations. However, it may not always be able to find a solution, especially for more complex systems of equations. In these cases, it may be necessary to use other methods for solving non-linear equations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top