Rearrange Variables in an equation that are changing everytime

I want to get one variable in terms of the other variables in the equation. For example
syms a b
eqn = ('a + 2*b = 1')
v_a = solve(eqn, a)
v_b = solve(eqn, b)
This works fine
My issue is that the variables and the number of variables can change in each iteration.
My variables alpha1, alpha2 and alpha3 are defined by sym and not syms. I need to do this as the no of alpha's is determined by the input data size. This case 3.
data = [-1 0 1;-1 +1 -1]';
alpha = sym('alpha',[1 3]); %output "[ alpha1, alpha2, alpha3]"
st = 'alpha*data(:,2)=0' %output should be "alpha2 - alpha1 - alpha3 = 0" but I dont get this. instead, its something else
v_2 = solve(st,alpha(1)) %doesnt work because the previous line is wrong
Thanks and appreciate any help

 Accepted Answer

This:
data = [-1 0 1;-1 +1 -1]';
alpha = sym('alpha',[1 3]);
advct = alpha * data(:,2)
v_2 = solve(advct == 0, alpha(1))
gives me these:
advct =
alpha2 - alpha1 - alpha3
v_2 =
alpha2 - alpha3
Is that the result you were hoping for?

6 Comments

Thanks. Yes, exactly. but the last line gives me an error. (see below) is there something that I am missing? Thanks
??? Error using ==> char Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169 vc = char(v);
Error in ==> solve at 67 [eqns,vars] = getEqns(varargin{:});
I do not understand the line you are referring to.
I also do not understand the error you are getting because I did not get a similar error. (Note that ‘alpha’ is also a built-in MATLAB function that sets transparency properties for objects in a current axes. See if renaming your ‘alpha’ variable to ‘alphavec’ or something eliminates the problem. Different MATLAB installations behave differently in such situations.)
thanks so much. I didnt realize that alpha was inbuilt. i changed it to alphavec. but I still get the same error. It is in the last line of the code (4th line) "Conversion to char from logical is not possible" What version of MATLAB are u using? Do you think its a version problem? I am using R2012b.
Thanks
(In the interim, Walter Roberson edited your question to format the code, so I could understand what you were referring to.)
I suggest:
st = (alpha*data(:,2)==0)
instead. (Note that I removed the single quotes. I suspect that specifying it as a string caused the problem.)
The expression I used here for ‘st’ worked for me, and yielded:
st =
alpha2 - alpha1 - alpha3 == 0
(I am using 2012a, so I doubt there is a version problem.)
Thanks Walter Roberson. I cannot see your editing though.
Removed single quotes. still gave same error. Following worked. I removed "st==0" and just gave "st" and it worked.
data = [-1 0 1;-1 +1 -1]'; alphavec = sym('alpha',[1 3]); st = alphavec*data(:,2); v_2 = solve(st,alpha(1));
Thanks so much again. I have just finished step 5 of 10 to finish the code and I am bound to run into more code challenges in future.
Thank you so much Star Strider. Thanks so much both of you as I learnt so much more than just the code
My pleasure! We are here to help.
And thank you again for accepting my answer!

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!