943,549 Members | Top Members by Rank

Ad:
Nov 21st, 2008
0

Beginner help for visual basic

Expand Post »
Hi I'm completely new to using VB in excel, but could someone help me fix my program. I'm supposed to find the roots of the equation denoted as func using the newton raphson method. Currently I am not getting the correct roots and the range of values which can be input are very limited. Could someone help on how to fix this? Thanks!
Option Explicit
Function newton(guess)

Dim func As Double, deriv As Double, i As Double, x2 As Double, x As Double, dif As Double

x = guess

For i = 0 To 1000
    func = x ^ 2 * Sin(x) + x ^ 3 * Exp(x) - x * Cos(x) - 7
    deriv = (x ^ 2 - 1) * Cos(x) + 3 * x * Sin(x) + (x ^ 3 + 3 * x ^ 2) * Exp(x)
    x2 = (x - func) / deriv
    dif = x2 - x
    x = x2
    If Abs(dif) < 0.001 Then i = 1000
Next i

newton = x
    
End Function
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
helloan is offline Offline
5 posts
since May 2008
Nov 21st, 2008
0

Re: Beginner help for visual basic

what is the use of the for loop ?
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Nov 21st, 2008
0

Re: Beginner help for visual basic

Click to Expand / Collapse  Quote originally posted by debasisdas ...
what is the use of the for loop ?

That's old school Basic. For i = 0 to 1000 is telling the proggie to start counting at 0 and stop at 1000. The func... statement has the proggie calculating each number it sees. Next i is telling the proggie move forward to the next number in the sequence....

(i.e., Start counting at 0, perform calculations, move on to next number (1,2,3,4,5...1000) )

Since 'for' is 0 to 1000 - it automatically stops at 1000.

...at least that's what it used to mean on the 'Trash 80's' (Radio Shack TRS-80 Microcomputer - w/ or w/o monitor)

...yea... i know... I just dated mahsef...

Reputation Points: 12
Solved Threads: 0
Newbie Poster
BigSeckC is offline Offline
13 posts
since Nov 2008
Nov 21st, 2008
0

Re: Beginner help for visual basic

Click to Expand / Collapse  Quote originally posted by helloan ...
Hi I'm completely new to using VB in excel, but could someone help me fix my program. I'm supposed to find the roots of the equation denoted as func using the newton raphson method. Currently I am not getting the correct roots and the range of values which can be input are very limited. Could someone help on how to fix this? Thanks!
Option Explicit
Function newton(guess)

Dim func As Double, deriv As Double, i As Double, x2 As Double, x As Double, dif As Double

x = guess

For i = 0 To 1000
    func = x ^ 2 * Sin(x) + x ^ 3 * Exp(x) - x * Cos(x) - 7
    deriv = (x ^ 2 - 1) * Cos(x) + 3 * x * Sin(x) + (x ^ 3 + 3 * x ^ 2) * Exp(x)
    x2 = (x - func) / deriv
    dif = x2 - x
    x = x2
    If Abs(dif) < 0.001 Then i = 1000
Next i

newton = x
    
End Function

I'm new too - so if I'm wrong, i apologize now...

First off, are you using VBA? (Visual Basic for Applications) - I've read that there are some inherent differences between it and 'regular' VB...

That being said, and here's where my math fails me, when calculating Sin and Cos - is it written as Sin(#) / Cos(#)?

Standard math says that x(y) = multiplication - so, are the numbers being multiplied by accident?

*scratches head*

If I'm not mistaken, you'd have to specify PI in a program (pi=3.14) - so would the same hold true for Sin & Cos...prior to your calculations?
Reputation Points: 12
Solved Threads: 0
Newbie Poster
BigSeckC is offline Offline
13 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Beginner help for visual basic

ur logic is wrong..............i cant say bout the code ,but newton ralphson method............u ar using has the wrong logic..

x2=x-(func/deriv)
this is the actual logic of newton ralphson method.
Reputation Points: 10
Solved Threads: 5
Light Poster
arghasen is offline Offline
35 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: What's wrong with my "searching" code?
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: chm file and vb5





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC