solving systems of linear equations
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I fail to run/solve the linear equ. (Ax=B) form as below: x1+x2=10; x1-x2=2; if u can help; % it is fine with x=A\B;
clc; clear all;
syms u
A=[1 1; 1 -1]
B= [10; 2]
for i=1:2
for j=1:2
E1=A(i,j)*u - B(i) end
[uarray(j)] =solve(E1)
end
uarray=double(uarray)
% I am getting=>[0,10],[0,-2] % I am getting=> 0 -2
0 commentaires
Réponses (1)
John D'Errico
le 3 Jan 2019
Modifié(e) : John D'Errico
le 3 Jan 2019
But what you did was not a correct solution to the linear system of equations.
The solution to the linear system of equations is most simply done via backslash. No symbolic solve is even necessary. I think you understood that from your comment, but are really looking to use the symbolic toolbox with solve.
A=[1 1; 1 -1];
B= [10; 2];
X = A\B
ans =
6
4
So you want to solve the linear system of equations: A*X = B. As you see, the solution is the vector [6;4]. Is that correct?
[A*X,B]
ans =
10 10
2 2
Yes, it is correct. Of course, you could have used solve to do the work too.
syms x1 x2
X = solve(A*[x1;x2] == B)
X =
struct with fields:
x1: [1×1 sym]
x2: [1×1 sym]
X.x1
ans =
6
X.x2
ans =
4
Or, if you want to use solve, but do so moredirectly, without converting the problem to one with matrix coefficients, we could do...
syms x1 x2
[x1,x2] = solve(x1+x2 == 10,x1-x2 == 2)
x1 =
6
x2 =
4
As you see by either approach, solve agrees with backslash. It even agrees with pencil and paper. If
x1 + x2 = 10
x1 - x2 = 2
Then just add the two equations together. We get
2*x1 = 12
Which clearly implies x1=6. Then you can go back and recover x2 from either of those equations. Thus...
6 + x2 = 10
So x2 = 4 drops out.
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!