MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by frank
on 21 May 2012

Hi; I have a loop which the variables and equations are calculated in this loop and the symbolic values and numbers of them are different in each loop. Also I should solve these equations together and get some other variables which I don't know the numbers of them because the numbers are calculated in the loop. how can I use the solve command? For example if I have: z=[a+b;a-b] s=[a;b] and I don't know the size of z and s matrices, what should I do? the below command doesn't work solve(z,'s')

## 8 Comments

## Walter Roberson (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80630

solve(z,s) perhaps ?

## frank (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80641

this command returns

a=0,b=0

so its not true

## Walter Roberson (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80644

That is a correct solution to [a+b;a-b]. Add the two parts together, getting the result 2*a = 0, and thus a=0; substitute this in to [a+b] to arrive at the conclusion that b=0 as well.

## frank (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80647

You are right.I made a mistake. but consider z=[a+b-2;a-b-2]

that command can not solve this and MATLAB returns:

Warning: 4 equations in 2 variables.

Warning: Explicit solution could not be found.

> In solve at 81

so =

[ empty sym ]

## Walter Roberson (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80703

zc = mat2cell(z);

sc = mat2cell(s);

solve(zc{:}, sc{:})

## frank (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80868

If I have z=[a+b-c,a-b-d] and s=[a;b] and I want get just a, b, what should I do?

when I use your suggested command for previous z and c, MATLAB returns:

In mat2cell at 54

Warning: Single input behavior is obsolete and will be removed in a

future release of MATLAB. Use C={X} instead.

## Walter Roberson (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80873

Try num2cell() instead of mat2cell()

## frank (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38981#comment_80887

Thank you so much for your help