solving matrix equation '[A]*(x)=(b) with variables at the x and b vectors
5 views (last 30 days)
Show older comments
hello i dont know how to solve for ex A= [1 2;2 1]
x=[1;x]
b=[0;u]
the solution of this is x=-0.5, u=3/2 but how can i solve it by using Matlab? i will be greatfull to helpers
2 Comments
David Young
on 24 Nov 2014
It's awkward to use x both for a 2x1 vector and for a scalar. The question would be clearer if you used two different symbols - say x = [1; v].
In what way do you want to use MATLAB? The question is easy to solve on paper, so what kind of contribution do you think MATLAB will make?
Accepted Answer
Thorsten
on 24 Nov 2014
Edited: John D'Errico
on 24 Nov 2014
You have to convert the equation into a proper linear form.
First you multiply x with A to get
1 + 2x = 0
2 + 1x = u
Then you transform the equations such that all unknowns are on the left side
2x = -1
1x - u = -2
Finally you code this as matrix and vector and use Matlab's matrix left divide \ to solve M*c = [x ;u];
M = [ 2 0; 1 -1]; c = [-1; -2]
M\c
ans =
-0.5000
1.5000
2 Comments
John D'Errico
on 24 Nov 2014
Edited: John D'Errico
on 24 Nov 2014
1. Minor edits for readability.
2. You had the wrong c vector, by miscopying the sign of the constant term on the second equation. Repaired that, to yield a correct final solution.
Finally, since I'm too lazy to check your algebra, I checked using syms... (Paper? Pencil? What are those barbaric things?)
A = sym([1 2;2 1]);
syms x u
solve(A*[1;x] - [0;u],x,u)
ans =
x: [1x1 sym]
u: [1x1 sym]
xu = solve(A*[1;x] - [0;u],x,u);
xu.x
ans =
-1/2
xu.u
ans =
3/2
David Young
on 24 Nov 2014
Barbaric indeed, but in this case very fast. Since the first equation does not involve u, the solution drops out of the first two lines of the answer, and there's no need to recast it as a new matrix problem. But maybe it's a homework problem and merely getting the answer misses the point.
More Answers (2)
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!