Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Conjugate gradient method
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Example code in [[Julia (programming language)]]==== <syntaxhighlight lang="julia" line="1" start="1"> """ conjugate_gradient!(A, b, x) Return the solution to `A * x = b` using the conjugate gradient method. """ function conjugate_gradient!( A::AbstractMatrix, b::AbstractVector, x::AbstractVector; tol=eps(eltype(b)) ) # Initialize residual vector residual = b - A * x # Initialize search direction vector search_direction = copy(residual) # Compute initial squared residual norm norm(x) = sqrt(sum(x.^2)) old_resid_norm = norm(residual) # Iterate until convergence while old_resid_norm > tol A_search_direction = A * search_direction step_size = old_resid_norm^2 / (search_direction' * A_search_direction) # Update solution @. x = x + step_size * search_direction # Update residual @. residual = residual - step_size * A_search_direction new_resid_norm = norm(residual) # Update search direction vector @. search_direction = residual + (new_resid_norm / old_resid_norm)^2 * search_direction # Update squared residual norm for next iteration old_resid_norm = new_resid_norm end return x end </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)