using "solve" and getting rid of unknown parameters

I am trying to solve a system of equations whose answer will depend on arbitrary values.
For example, consider the following system of 2 equations and 3 unknowns:
syms x1 x2 x3
E=[x1+2*x2+x3;x3-x1+x2];
a=solve(E==0,x1,x2,x3)
[a.x1 a.x2 a.x3]
this gives me an answer that depends on an arbitrary value z.
I would like MATLAB to directly give me an answer (with no arbitrary values). So for instance, I would like to get an answer where the x1 value is positive. Is there a way to do that?
I am using "solve" in a subroutine, and after that, I would like to test the answer to see if it is a non negative array. But whenever the code reaches the answer, it tells me there is an error (obviously) because of the unknown parameter.

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 14일
편집: Azzi Abdelmalek 2013년 1월 14일

1 개 추천

You have 2 equations and 3 unknowns variables, which means there are infinite solutions. It's like your are solving
E=[x1+2*x2+z;z-x1+x2];
b=solve(E==0,x1,x2)

댓글 수: 6

Del
Del 2013년 1월 14일
편집: Del 2013년 1월 14일
Thanks for your answer, but I am aware there will be infinitely many solutions. However, what I would like MATLAB to give me among all those solutions, is one answer that satisfies specific conditions.
For instance, I would like one answer such that x1>=0. So, an example of an acceptable answer is:
[1 -2 3]
Then just replace z by a positive number, for example
z=3
x1=eval(a.x1)
x2=eval(a.x2)
x3=eval(a.x3)
Del
Del 2013년 1월 14일
I would like to do that, the only problem is, like I said, the equation that I am solving is in a subroutine, and I don't necessarily know how many arbitrary "parameters" it will have at each iteration or even, the names of the unknown parameters.
For instance, for the small example that I gave, I know that there is one arbitrary parameter, and the name is z, only because I typed the problem in the MATLAB command Window by itself. However, if it is a big problem, and I want to solve an equation as a part of the code, I will not know the names of the parameters involved.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 14일
편집: Azzi Abdelmalek 2013년 1월 14일
As long as there are infinite solutions, Matlab will generate those solutions, you have to add, clearly, the conditions that allows to choose some of them.
Del
Del 2013년 1월 14일
편집: Del 2013년 1월 14일
sure, my question, is what is the correct way in MATLAB " of adding , clearly, the conditions that allows to choose some of them."
There are your conditions, if for example you said that the first solution must be positive. The below code will choose one solution.
syms x1 x2 x3
E=[x1+2*x2+x3;x3-x1+x2];
a=solve(E==0,x1,x2,x3)
n= size(fieldnames(a),1)
z=solve(a.x1==1)
for k=1:n
sol(k)=eval(a.(sprintf('x%d',k)));
end

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2013년 1월 14일

0 개 추천

You might be able to use numeric::solve
That routine is not exposed at the MATLAB level so you would have to feval() or evalin() the symengine.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

질문:

Del
2013년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by