Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:57:54+00:00 2026-06-09T22:57:54+00:00

I wrote this piece of code that should make what is described here: Conjugate

  • 0

I wrote this piece of code that should make what is described here:

Conjugate Gradient from wikipedia

but after some iterations the variable denomAlpha goes to zero and so I get a NAN on alpha. So what is wrong with my algorithm?

import Jama.Matrix;

public class ConjugateGrad {

    private static final int MAX_IT = 20;
    private static final int MAX_SIZE = 50;

    public static void main(String[] args)  {
        Matrix A = Matrix.random(MAX_SIZE, MAX_SIZE);
        Matrix b = Matrix.random(MAX_SIZE, 1);

        double[][] d = new double[MAX_SIZE][1];
        for(int ii=0;ii<MAX_SIZE;ii++) {
            d[ii][0] =0;
        }

        Matrix x = Matrix.constructWithCopy(d);
        Matrix r = b.minus(A.times(x));
        Matrix p = r;
        Matrix rTrasp_r = r.transpose().times(p);

        for (int i = 0; i < MAX_IT; i++) {
            Matrix denomAlpha = p.transpose().times(A.times(p));
            double numeratorAlpha = rTrasp_r.getArray()[0][0];
            double Alpha = numeratorAlpha / denomAlpha.getArray()[0][0];
            x = x.plus(p.times(Alpha)); 
            r = r.minus(A.times(p));  
            Matrix rNew = r.transpose().times(r); 
            if (Math.sqrt(rNew.getArray()[0][0]) <1.0e-6) {
                break;
            }
            double Beta = rNew.getArray()[0][0] / rTrasp_r.getArray()[0][0];
            p = r.plus(p.times(Beta));
            rTrasp_r = rNew;           
        }
    }
}

it same that with those parameters :

double[][] matrixA = {{4,1},{1,3}};
Matrix A = Matrix.constructWithCopy(matrixA);    
double[][] vectorb = {{1},{2}};
Matrix b = Matrix.constructWithCopy(vectorb);
double[][] d = {{2},{1}};
Matrix x = Matrix.constructWithCopy(d);

at first step of the algorithm things are good
but at second step not…

r: -8.0, -3.0
Alpha: 0.22054380664652568
Beta:   12.67123287671233
x:  0.2356495468277946, 0.33836858006042303, 

Second step :

Alpha: 0.0337280177221555
Beta:  159.11259655226627
x:  -2.2726985108925097, -0.47156587291133856, 

Ok, I have found one Error:

r = r.minus(A.times(p).times(Alpha));  

Now it works:

r: -8.0, -3.0, 
Alpha: 0.22054380664652568
rNew: 0.6403099643121183, 
Beta: 0.008771369374138607
p:  -0.3511377223647101, 0.7229306048685207, 
x:  0.2356495468277946, 0.33836858006042303, 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:57:55+00:00Added an answer on June 9, 2026 at 10:57 pm

    Sorry for the hack answer but… using the numerical example params from the Wikipedia article and outputting the matrices to terminal at each step could find the discrepancy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this piece of code that is supposed to redirect something written on
I have a program I wrote in Windows with this piece of code that
I just wrote this piece of code to represent this error that is killing
I've wrote this simple piece of code. And I have a slight problem with
I wrote this code for zoom in/out . it works but even with one
I am working on a piece of code that writes data from 4 different
I wrote small piece of code which should toggle reporting box on my page.
Recently I found a piece of code that creates an instance of TButton from
I have a piece of code (.NET4 C#) that should run in tight loop
I wrote a piece of code in java script that tracks the activities of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.