Newton-Raphson in Visual Basic 6

  • Thread starter Benx
  • Start date
  • Tags
    Visual
In summary: Best regards,EusIn summary, the conversation is about a programming issue in Visual Basic 6 involving the use of the Newton-Raphson method to resolve an equation. The program is running, but not producing the expected results. Participants suggest providing more details about the problem and offer advice on how to improve the program, including checking the function interpretation and the first derivative. Eventually, the issue is resolved by correcting the first derivative and the program successfully produces the desired result.
  • #1
Benx
7
0
Hello,
I am progamming in Visual Basic 6, need help to resolve equation using Newton-Raphson
method.
My program is running, but not properly!

Thank you.
 
Technology news on Phys.org
  • #2
Hi Ho!

I think you should specify your problem in detail first before we can help you.
What kind of problem do you have in resolving the equation? Parsing the equation? Or, what?


Eus
 
  • #3
"[URL Benx, today is your lucky day[/URL]
 
Last edited by a moderator:
  • #4
basePARTICLE - that code is for SECANT method - and, please no offense, but that's some old code ...

I think what Benx needs right now is exactly what Eus suggested - provide better details of his problem - that way we might be able to offer better help.
 
  • #5
Eus said:
Hi Ho!

I think you should specify your problem in detail first before we can help you.
What kind of problem do you have in resolving the equation? Parsing the equation? Or, what?


Eus

Hello,
I do not found the same result as example given in the book, i don't know were is the
failure.
I think is better to see the code. or the algorithms "function and it's derivative".


Benx
 
  • #6
I think is better to see the code. or the algorithms "function and it's derivative".

So, why didn't you just post the code?


Eus
 
  • #7
Eus said:
So, why didn't you just post the code?


Eus

Ok,
I will attach a vbproject to this message.
 

Attachments

  • NewtonIter.zip
    2.4 KB · Views: 572
  • #8
Code:
Attribute VB_Name = "Module1"
 Public Function NewtonExact(Root As Double, TOL As Double, maxIter As Integer, params() As Double) As Integer

  Dim iter As Integer
  Dim diff As Double


  Do
    diff = MyFxMu(Root, params()) / MyDerivMu(Root, params())
    Root = Root - diff
    iter = iter + 1
  Loop While (iter <= maxIter) And (Abs(diff) > TOL)

  If Abs(diff) <= TOL Then
    NewtonExact = True
  Else
    NewtonExact = False
  End If

End Function
Function MyDerivMu(X As Double, params() As Double) As Double
    ' First Derivative f'(µ)
    
    term1 = params(0) / (params(3) - X)
    term2 = params(1) / (params(4) - X)
    term3 = params(2) / (params(5) - X)
       
    MyDerivMu = (2 / (params(3) - X)) * term1 * term1 + (2 / (params(4) - X)) * term2 * term2 + (2 / (params(5) - X)) * term3 * term3
End Function
Function MyFxMu(X As Double, params() As Double) As Double
     'Function f(µ)
    term1 = params(0) / (params(3) - X)
    term2 = params(1) / (params(4) - X)
    term3 = params(2) / (params(5) - X)
    
MyFxMu = (term1 * term1 + term2 * term2 + term3 * term3) - 1
End Function
 
  • #9
Hmm. I didn't see anything technically wrong.

Benx, what are the values for the parameters (ie, your array params) ?

Also, some question to think about:
1. Have you graphed this out to see the behavoir near the root?
2. Do you have a reasonably accurate initial quess to root?
3. Do the iterates ever come close to some of the params values (in which case, we may have trouble due to sinularities?
 
  • #10
Newton-Raphson

Hello,

You can see algorithm, available in Text file of project.
So, I want to know if the algorithm is implemented properly.

Best regards
Benx
 
  • #11
Newton-Raphson

TheoMcCloskey said:
Hmm. I didn't see anything technically wrong.

Benx, what are the values for the parameters (ie, your array params) ?

values are available in procedure Sub NewtonExacte_Click() in form code.
params(0 to 5)

Best regards
Benx
 
  • #12
Like I said, I didn't see anything technically wrong.

I did read the txt file, but the expressions for F and F' are not too clear (looks like the lack of proper use of parens). I can only assume you interpreted the function "F" correctly.

...values are available in procedure Sub NewtonExacte_Click() in form code.
params(0 to 5)...

OK then, as I suggested, plot this function out and you'll see that the root is not where you think it is.

In fact, this function (as you transcribed) is multi-valued and also has a series of three poles (quess where) and may be very ill-form for a Newton search.

Are you sure you interpreted the function "F" correctly?

In fact, your function (as stated) can be manipulated to a search of the zero of a cubic polynomial.

Thus, give the original problem statement a review and make sure you have the correct interpretation of the function "F"
 
  • #13
Hi Ho!

Your mistake is in the first derivative of your function.

If
[tex]
F(\mu)=\left ( \frac{b1}{a1-\mu} \right )^2 + \left ( \frac{b2}{a2-\mu} \right )^2 + \left ( \frac{b3}{a3-\mu} \right )^2 - 1
[/tex]

Then
[tex]
F'(\mu)=2\left ( \frac{b1}{a1-\mu}\right ) \left ( \frac{a1 - \mu + b1}{(a1 - \mu)^2} \right ) + 2\left ( \frac{b2}{a2-\mu}\right ) \left ( \frac{a2 - \mu + b2}{(a2 - \mu)^2} \right ) + 2\left ( \frac{b3}{a3-\mu}\right ) \left ( \frac{a3 - \mu + b3}{(a3 - \mu)^2} \right )
[/tex]

Since the quotient rule states:
[tex]
\left ( \frac{u}{v} \right )'=\frac{u'\ v - u\ v'}{v^2}
[/tex]

If the initial guess is 0.0, the program should find the root, which is at x = 2.566477775, more or less at its 33th iteration.

Good luck!


Eus
 
  • #14
I need to correct myself --<groan> as I analyzed the wrong F. In my computations, I forgot to square the indiviual terms of [itex]b_i / ({a_i-\mu})[/itex]. Sorry for the confusion.

However, once I correct F, I do see a root near zero (between -.24 and -.22), a large positive pole around 1, and another zero near 2.5 ( this probably being the root Eus discussed).

But, I don't agree with Eus's derivative. If we have a term such as

[tex]g(\mu) = \Large(\frac{b_1}{a_1-\mu}\Large)^2[/tex]

and if I let

[tex]v(\mu)=a_1-\mu[/tex]

then

[tex]g(v(\mu))=\big(\frac{b_1}{v}\big)^2=\Large(b_1v^{-1}\Large)^2=b_1^2 v^{-2}[/tex]

and

[tex]\frac{dg}{d\mu} = \frac{dg}{dv}\frac{dv}{d\mu}= -2\,b_1^2\,v^{-3}\,\frac{dv}{d\mu}= -2\,b_1^2\,v^{-3}\,(-1) = 2\Large(\frac{b_1}{v}\Large)^2\frac{1}{v}[/tex]

Thus, I don't think the derivative is wrong, unless I missing something (which I proved capable of doing many times).

Again, for the given function F and the given derivative, the procedure does converge in abot 5 iterations to the root near zero ( Root = -0.236799382041445 ) but not to the root stated in the problem statement.
 
  • #15
Hi Ho!

Isn't that if
[tex]
g(\mu)=\left ( \frac{b_{1}}{a_{1}-\mu} \right )^2
[/tex]
and
[tex]
v(\mu)=a_{1}-\mu
[/tex]
then
[tex]
g(v(\mu))=\left ( \frac{b_{1}}{a_{1}-v(\mu)} \right )^2
[/tex]

[tex]
g(v(\mu))=\left ( \frac{b_{1}}{a_{1}-(a_{1}-\mu)} \right )^2
[/tex]

[tex]
g(v(\mu))=\left ( \frac{b_{1}}{a_{1}-a_{1}+\mu} \right )^2
[/tex]

[tex]
g(v(\mu))=\left ( \frac{b_{1}}{\mu} \right )^2
[/tex]
?


Eus
 
  • #16
TheoMcCloskey said:
Hmm. I didn't see anything technically wrong.

Benx, what are the values for the parameters (ie, your array params) ?

Also, some question to think about:
1. Have you graphed this out to see the behavoir near the root?
2. Do you have a reasonably accurate initial quess to root?
3. Do the iterates ever come close to some of the params values (in which case, we may have trouble due to sinularities?

Hello,
There is no error on derivative function!
Newton-Raphson is not indicated for this case.
first we compute u_bound an l_bound:

//Get upper bound on mu
u_bound1 = a1-fabs(b1);
u_bound2 = a2-fabs(b2);
u_bound3 = a3-fabs(b3);
u_bound = u_bound1;
if (u_bound2 < u_bound) u_bound=u_bound2;
if (u_bound3 < u_bound) u_bound=u_bound3;
l_bound1 = a1-1.73205080757*fabs(b1);

//get lower bound on mu
l_bound2 = a2-1.73205080757*fabs(b2);
l_bound3 = a3-1.73205080757*fabs(b3);
l_bound = l_bound1;
if (l_bound2 < l_bound) l_bound = l_bound2;
if (l_bound3 < l_bound) l_bound = l_bound3;

Then we use rtsafe algorithm "it's combined Newton-bisection method".
at the third iteration we found:
µ=-0.000109354
 
  • #17
wow ... that's real interesting. Using your values of params

Code:
 params(0) = -0.404279 'b1
  params(1) = -0.767506 'b2
  params(2) = -1.013014 'b3
  params(3) = 0.629811  'a1
  params(4) = 1.000785  'a2
  params(5) = 1.369404  'a3

and your definition of F(x)

Code:
Function MyFxMu(X As Double, params() As Double) As Double
     'Function f(µ)
    term1 = params(0) / (params(3) - X)
    term2 = params(1) / (params(4) - X)
    term3 = params(2) / (params(5) - X)
    
    MyFxMu = (term1 * term1 + term2 * term2 + term3 * term3) - 1
End Function

... I am just not getting your results, either graphically or iteratively. I am getting, as stated before, x aprrox = -.236 both iteratively and graphically.
 
  • #18
µ, is the smallest root of the sixth degree polynomial f(µ).
The general shape of the graph of this equation shows that the real roots will be
distributed as follows:
1 root < a1 < 2 (possible) roots < a2 < 2 (possible) roots < a3 < 1 root.
 

1. What is Newton-Raphson method in Visual Basic 6?

The Newton-Raphson method is a numerical technique used to find the roots of a function in Visual Basic 6. It is based on the iterative process of approximating the root of a function by using its derivative.

2. How does the Newton-Raphson method work in Visual Basic 6?

The Newton-Raphson method works by starting with an initial guess for the root of a function and then using the derivative of the function to refine the guess in each iteration until a satisfactory accuracy is achieved.

3. What are the advantages of using Newton-Raphson method in Visual Basic 6?

Some advantages of using the Newton-Raphson method in Visual Basic 6 include its fast convergence rate and its ability to handle complex and nonlinear functions. Additionally, it only requires the function and its derivative, which are often readily available in many mathematical problems.

4. What are the limitations of Newton-Raphson method in Visual Basic 6?

One limitation of the Newton-Raphson method in Visual Basic 6 is that it may fail to converge if the initial guess is too far from the actual root or if the function has multiple roots. It also requires the function to have a continuous derivative.

5. How can I implement the Newton-Raphson method in Visual Basic 6?

To implement the Newton-Raphson method in Visual Basic 6, you will need to define the function and its derivative, determine an initial guess for the root, and then use a loop to iteratively refine the guess until the desired accuracy is achieved. There are also built-in functions and libraries in Visual Basic 6 that can assist in implementing this method.

Similar threads

  • Programming and Computer Science
Replies
4
Views
323
Replies
16
Views
1K
  • Programming and Computer Science
Replies
0
Views
232
  • General Math
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
641
  • Programming and Computer Science
Replies
3
Views
679
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
2
Replies
37
Views
2K
  • Mechanical Engineering
Replies
4
Views
866
  • Programming and Computer Science
Replies
16
Views
1K
Back
Top