# KSPBuildResidual
Builds the residual in a vector provided. 
## Synopsis
```
#include "petscksp.h" 
#include "petscmat.h" 
PetscErrorCode KSPBuildResidual(KSP ksp, Vec t, Vec v, Vec *V)
```
Collective


## Input Parameter

- ***ksp -*** iterative context obtained from `KSPCreate()`



## Output Parameters

- ***v -*** optional location to stash residual.  If v is not provided,
then a location is generated.
- ***t -*** work vector.  If not provided then one is generated.
- ***V -*** the residual



## Note
Regardless of whether or not v is provided, the residual is
returned in V.




## See Also
 [](chapter_ksp), `KSP`, `KSPBuildSolution()`

## Level
advanced

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/interface/itfunc.c.html#KSPBuildResidual">src/ksp/ksp/interface/itfunc.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex42.c.html">src/ksp/ksp/tutorials/ex42.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex47.c.html">src/ts/tutorials/ex47.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/cg/cg.c.html#KSPBuildResidual_CG">KSPBuildResidual_CG in src/ksp/ksp/impls/cg/cg.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gcr/gcr.c.html#KSPBuildResidual_GCR">KSPBuildResidual_GCR in src/ksp/ksp/impls/gcr/gcr.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/rich/rich.c.html#KSPBuildResidual_Richardson">KSPBuildResidual_Richardson in src/ksp/ksp/impls/rich/rich.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/ksp/ksp/interface/itfunc.c)


[Index of all KSP routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
