# How to access solution to linear system in PETSc?

I have just started with PETSc hence it might seem like a very stupid question but I couldn't find any answer in manual. After Calling KSPSolve, where can I access the soluition for my linear system? Example for a matrix:

$$\left(\begin{array}{ccc} 2 &-1 & 0\\ -1 &2&-1\\ 0&-1&2 \end{array}\right) \left(\begin{array}{c} x\\y\\z \end{array}\right)= \left(\begin{array}{c} 1\\0\\1 \end{array}\right)$$

I want to get vector $$\left(\begin{array}{c} 3/4\\1/2\\1/4 \end{array}\right)$$ back. The documentation for KSP Solve says x - the solution (this may be the same vector as b, then b will be overwritten with answer)

but I keep getting x vector as $$\left(\begin{array}{c} 1\\1\\1 \end{array}\right)$$

Is there something wrong in my understanding?

• The solution to that (nonsingular) linear system is $[1, 1, 1]^T$. I don't understand why you want to get a vector that is not a solution. Sep 13 '18 at 15:30
• Because I am that idiot who solved the equations wrong :| and assumed I am not able to get results Sep 13 '18 at 18:28

Before you solve the system in PETSc you store the matrix $A$ and RHS $b$. The message from PETSc says, that after the system is solved, the solution might be overwritten into $b$, where originally the RHS was stored. That's what it means.
$$\left(\begin{array}{ccc} 2 &-1 & 0\\ -1 &2&-1\\ 0&-1&2 \end{array}\right) \left(\begin{array}{c} x\\y\\z \end{array}\right)= \left(\begin{array}{c} 1\\0\\1 \end{array}\right)$$ the correct solution is indeed (as pointed out by @JedBrown) $$\left(\begin{array}{c} 1\\1\\1 \end{array}\right)$$
If you intend (for whatever good reason) to get $$\left(\begin{array}{c} 3/4\\1/2\\1/4 \end{array}\right)$$ that only means that you have a typo in either your matrix or RHS. Assuming that your matrix is correct, you will get your desired solution for the following system (changed RHS slightly): $$\left(\begin{array}{ccc} 2 &-1 & 0\\ -1 &2&-1\\ 0&-1&2 \end{array}\right) \left(\begin{array}{c} x\\y\\z \end{array}\right)= \left(\begin{array}{c} 1\\\color{red}{1/2}\\1 \end{array}\right)$$