Go to Post 'We're back to square one! and while we're at it, let's redesign square one.' - Bethie42 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 06-03-2006, 20:40
sanddrag sanddrag is offline
On to my 16th year in FRC
FRC #0696 (Circuit Breakers)
Team Role: Teacher
 
Join Date: Jul 2002
Rookie Year: 2002
Location: Glendale, CA
Posts: 8,509
sanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond reputesanddrag has a reputation beyond repute
Visual Basic. Using a loop to solve the cosine function.

I need to do this http://upload.wikimedia.org/math/5/b...69cad0c4a3.png in Visual Basic.

Attached is what I have done so far. I can't get it to work right though. I am not a programmer and I'm really struggling with this. I think the problem is coming from instead of (2*n)factorial my program is doing (n factorial)*2. I don't know how to fix it.

It should be noted that the "n" in that equation corresponds to "Counter" in my program and "n" in my program represents the number of terms of the series (since we can't have it going ifinitely).

Hopefully someone can help. Thanks.
Attached Files
File Type: xls Vbcosine.xls (22.5 KB, 516 views)
__________________
Teacher/Engineer/Machinist - Team 696 Circuit Breakers, 2011 - Present
Mentor/Engineer/Machinist, Team 968 RAWC, 2007-2010
Technical Mentor, Team 696 Circuit Breakers, 2005-2007
Student Mechanical Leader and Driver, Team 696 Circuit Breakers, 2002-2004
  #2   Spotlight this post!  
Unread 06-03-2006, 21:56
BotLobsta BotLobsta is offline
Registered User
FRC #1477
 
Join Date: Jan 2006
Location: Texas
Posts: 4
BotLobsta is an unknown quantity at this point
Re: Visual Basic. Using a loop to solve the cosine function.

One thing that I noticed is that in the factorial loop, you dont want to start at n-1, you want to start at FactNum - 1. What you are doing now in that nested for loop is (n-1)!*FactNum. This will only give you the correct value when Counter = n/2.
  #3   Spotlight this post!  
Unread 10-03-2006, 11:45
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Visual Basic. Using a loop to solve the cosine function.

That's a taylor series. Depending on how accurate you need it, one or two terms should be enough. For example, if you can make it accurate enough to 90 degrees, you can then use other tricks to determine the cosines.

For instance: 1-(x^2)/2 + (x^4)/24 - (x^6)/720 is accurate to about PI radians (90 degrees). You can check by graphing it next to a cosine function. Since the cosine function after PI radians is simply a mirror image of the cosine function before PI radians, you can use that to avoid having to determine the actual value beyond PI. After 2*PI is just repeats, so you can use that to determine cosines for values beyond 2*PI. Cosine is mirrored about the y-axis, so you know cos(-x) = cos(x), which means negatives aren't a problem.

So now, you can make a full cosine function
Code:
Function cos(radians As Double)
Dim answer As Double
'MsgBox ("cos input: " & radians)

If radians >= 0 And radians < 3.14159 Then
    answer = 1 - (radians * radians) / 2 + (radians * radians * radians * radians) / 24 - (radians * radians * radians * radians * radians * radians) / 720 + (radians * radians * radians * radians * radians * radians * radians * radians) / 40320
ElseIf radians >= 3.14159 And radians < 2 * 3.14159 Then
       answer = cos((2 * 3.14159) - radians)
ElseIf radians >= 2 * 3.14159 Then
    answer = cos(radians - 2 * 3.14159)
ElseIf radians < 0 Then
    answer = cos(0-radians)
End If

cos = answer

End Function
This is a pretty slow way of doing cosine, but it gets the job done. Also, the recursive calls can be eliminated, but then it would get somewhat ugly. Attached is a graph from excel showing the difference between what the above function outputs and what the actual cosine function outputs. It's quite close.
Attached Thumbnails
Click image for larger version

Name:	approx.PNG
Views:	94
Size:	10.6 KB
ID:	4102  

Last edited by Bongle : 10-03-2006 at 11:48.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop time for OperatorControl function? Debug blows... Chris_Elston Programming 10 13-02-2006 14:42
How do I declare a string and pass it from a Visual Basic GUI to a C++ DLL? complete Programming 0 14-01-2006 17:35
TTL port to a serial port on a demo board ImmortalAres Programming 16 09-07-2005 23:44
RoboEmu2(code simulator)--now with C! rbayer Programming 23 17-02-2005 09:17
heres the code. y this not working omega Programming 16 31-03-2004 15:18


All times are GMT -5. The time now is 13:24.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi