2 Replies - 10328 Views - Last Post: 26 November 2008 - 03:02 AM Rate Topic: -----

#1 eddie m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-November 08

newton raphson on visual basic

Posted 25 November 2008 - 12:11 PM

i am very new to VB :)

i am trying to get one root of a cubic equation using the newton raphson method however it is not working and will continuously return a value if just zero, i dont understand i have tried adding the math.sign function but to no avail an anyone offer any solutions to why my code is offering only zero as the answer?

heres the code

Public Class fmEQS
	Private Sub btnCALC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCALC.Click
		' Isolate a Root
		'Dim tbX3, tbX2, tbX1, tbX0 As Single
		Dim A, B, C, D As Single
		Dim x1, x2 As Single
		Dim s1, s2 As Single
		A = tbX3.Text
		B = tbX2.Text
		C = tbX1.Text
		D = tbX0.Text
		x2 = +100
		tbANSIJX1.Text = s1
		Do
			x1 = x2
			s1 = Math.Sign(x1 - (((A * (x1 ^ 3)) + (B * (x1 ^ 2)) + (C * x1) + D) / (((A ^ 3) * (x1 ^ 2)) + ((B ^ 2) * x1) + C)))
			x2 = x1 - 0.1
			s2 = Math.Sign(x2 - (((A * (x2 ^ 3)) + (B * (x2 ^ 2)) + (C * x2) + D) / (((A ^ 3) * (x2 ^ 2)) + ((B ^ 2) * x2) + C)))
		Loop While s1 = s2
	   
	End Sub



thank you

Ed

Is This A Good Question/Topic? 0
  • +

Replies To: newton raphson on visual basic

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: newton raphson on visual basic

Posted 25 November 2008 - 12:21 PM

moved to VB.NET
Was This Post Helpful? 0
  • +
  • -

#3 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: newton raphson on visual basic

Posted 26 November 2008 - 03:02 AM

Have not gone through your code BUT with newton raphson you can usually not test to see if somthing e.g. a1 = a2 to determine if you are done. The thing with almost all numeric methods is that you will have to decide when the error is SMALL ENOUGH.

So, to decide when you have an answer you'd have to do somthing along the lines of:

acceptedError = 0.000001
a1 = calculation
a2 = previous calculation
IF abs(a1 - a2) < acceptedError
THEN return answer and done.


/Jens

This post has been edited by jens: 26 November 2008 - 03:03 AM

Was This Post Helpful? 1

Page 1 of 1