## Solving system of linear equations with boundaries, linsolve does not work

### Alexis Moreno (view profile)

on 28 Jan 2018 at 21:16
Latest activity Edited by John D'Errico

### John D'Errico (view profile)

on 28 Jan 2018 at 22:03

I have a matrix as follows:

first = [a1, -a1, 0; -a1, a1+a2, -a2; 0, -a2, a2]

second = [u1; u2; u3]

third = [f1;f2;f3]

where: [first] x [second] = [third]

We are given boundary conditions that do not match the linsolve, we know: u1, f2, and f3.

How can I solve this system of linear equations? Any help would be greatly appreciated

### John D'Errico (view profile)

on 28 Jan 2018 at 22:02
Edited by John D'Errico

### John D'Errico (view profile)

on 28 Jan 2018 at 22:03

Linsolve DOES work, IF you use it properly. If not, well, garbage in, garbage out.

Apparently you are telling us that {u1, f2, f3} are known? Therefore, I expect that the real unknowns are {u2, u3, f1}?

And what can you possibly mean by "boundary conditions"? Boundary conditions are meaningless in terms of a linear system of equations.

Are a1, a2 known values? Since you have not given them to us,, nor the values of the knowns, thus u1,f2,f3, I'll just solve it using symbolic tools.

```syms a1 a2
A = [a1, -a1, 0; -a1, a1+a2, -a2; 0, -a2, a2]
```

Now, you want to solve the linear system for the unknowns u2,u3,f1. We can use symbolic computations.

```syms u1 u2 u3 f1 f2 f3
sol = solve(A*[u1;u2;u3] == [f1;f2;f3],u2,u3,f1)
sol =
struct with fields:
```
```      u2: [1×1 sym]
u3: [1×1 sym]
f1: [1×1 sym]```
```sol.u2
ans =
(f2 + f3 + a1*u1)/a1
```
```sol.u3
ans =
(a1*f3 + a2*f2 + a2*f3 + a1*a2*u1)/(a1*a2)
```
```sol.f1
ans =
- f2 - f3
```

However, you COULD have used linsolve, IF you formulated and rearranged the system of equations properly. Be careful, because A is a singular matrix.

I still have no idea what you are thinking about in terms of boundary conditions. The solution requires no such thing, nor do they make sense here.