Get coeffients of symbolic expression

I have a set of equations (derv_alpha) that I have arrived at by differentiating an expression and setting it to zero.
N=3; alphavec = sym('alpha',[1 N]); %N can take any value
derv_alpha = [ 0, 2*alphavec(3) - 2*alphavec(2) + 2, 2*alphavec(2) - 4*alphavec(3)];
Now I need to solve these simultaneous equations and come up with answers for alpha2, alpha3...alphaN. i.e. alphavec(2), alphavec(3)...alphavec(N).
I am trying to put this in matrix form A*x=b and solve for x. Is it possible? If so, HOW CAN I GET THE COEFFICIENTS OF EACH VARIABLE?
The solution will have to be irrespective of the position of the variable in the expression.
I did a quick search. collect() doesnt work. i dont think sym2poly can be used
OR, is there a better way to do it when working with symbols?
I am doing my first program using symbols this week and appreciate any help. Thanks again.

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 7 月 21 日

0 投票

Generally,
solve(derv_alpha, alphavec)
However, derv_alpha(1) is the constant 0 and not an expression in the alpha variables, so the set of three equations in derv_alpha, in three variables, is really only two equations in three variables, which you aren't going to be able to solve for. This is going to be a problem even if you express as A*x=b

6 件のコメント

Mech Princess
Mech Princess 2012 年 7 月 21 日
編集済み: Mech Princess 2012 年 7 月 21 日
alpha1 isnt zero. it needs to be solved for after alpha2 and alpha3. I editted the code to remove alpha1.
N=3; alphavec = sym('alpha',[1 N]); %N can take any value
derv_alpha = [2*alphavec(3) - 2*alphavec(2) + 2, 2*alphavec(2) - 4*alphavec(3)];
derv_alpha=simplify(derv_alpha);
alpha_sol=zeros(1:N);
alpha_sol(2:N)=solve(derv_alpha, alphavec(2:N))
last line gives this warning
"Warning: 4 equations in 2 variables." %where did it get 4 equations??
Here I want to solve alpha2 through alphaN. I will input alpha1 later.
Back to my earlier question - is there a way to get the coefficients?
Thanks again ps- i figured out how to format in the post :)
Star Strider
Star Strider 2012 年 7 月 21 日
編集済み: Star Strider 2012 年 7 月 21 日
I am now completely lost as to what you are doing. What coefficients do you want to get? The ‘coeffs’ function will give you the coefficients, however it has to know the variables or expressions you want the coefficients of.
Meanwhile:
% alpha_sol=zeros(1:N);
% alpha_sol(2:N)=solve(derv_alpha, alphavec(2:N))
alpha_sol_12 = solve(derv_alpha(1) == 0, alphavec(2))
alpha_sol_13 = solve(derv_alpha(1) == 0, alphavec(3))
alpha_sol_22 = solve(derv_alpha(2) == 0, alphavec(2))
alpha_sol_23 = solve(derv_alpha(2) == 0, alphavec(3))
These produce:
alpha_sol_12 =
alpha3 + 1
alpha_sol_13 =
alpha2 - 1
alpha_sol_22 =
2*alpha3
alpha_sol_23 =
alpha2/2
Is this what you want to do?
I initially put that into a loop, but the Symbolic Math Toolbox objected and I don't have the energy tonight to argue with it.
Mech Princess
Mech Princess 2012 年 7 月 21 日
編集済み: Mech Princess 2012 年 7 月 21 日
Thanks, but at this point, I want numerical solutions for the alpha's. in this case alpha2=2; alpha3=1;
derv_alpha=[ 2*alpha3 - 2*alpha2 + 2, 2*alpha2 - 4*alpha3]
The coefficients I want are for alpha2, alpha3 and constants. for equations
2*alpha3 - 2*alpha2 + 2==0
2*alpha2 - 4*alpha3 ==0
answer I am looking for would be
coeff_matrix=[-2 3 2; 2 -4 0]
So that I can solve for alpha2 and alpha3 by A*x=B form
alpha_solution = [-2 3;2 -4]\[2 0]; %something like this.
Thanks again and yes, acceptable that a fight with MATLAB at midnight isnt the best thing :)
Walter Roberson
Walter Roberson 2012 年 7 月 21 日
With regard to four equations: if your code read
derv_alpha = [2*alphavec(3) -2*alphavec(2) + 2, 2*alphavec(2) -4*alphavec(3)];
with no space after the '-', then the '-' would be interpreted as unary minus and the space would be interpreted as the end of the term in the list. [A - B] is one expression but [A -B] is two, A and -B . You can ensure your desired groupings by adding ()
Mech Princess
Mech Princess 2012 年 7 月 21 日
Thanks. It's an output from another line of code which is the partial derivative of an expression. The spaces are what MATLAB gives and frankly, I was wondering about it myself. Thanks
Mech Princess
Mech Princess 2012 年 7 月 21 日
does anyone know how to do this? Thanks

サインインしてコメントする。

カテゴリ

質問済み:

2012 年 7 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by