473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using loops to find a convergence point in calculating an integral

I am attempting to write a loop that will use rudimentary methods to solve an integral (as in the area under a curve) to a certain user-specified degree of accuracy by calculating the area of a changing number of rectangles that fit along that curve. This is the code I have so far:

Given a polynomial y=5x^5+x^4+2x^3-7x^2+x+2, and the start and end points 1, 5, the user inputs are:
x5=5, x4=1, x3=2, x2=-7, x=1, c=2, s=1, e=5, and cL is the convergence limit saying that it must be accurate to .01.

Expand|Select|Wrap|Line Numbers
  1. Function EC(x5, x4, x3, x2, x, c, s, e, cL)
  2. r = 10
  3.     Do
  4.         w = (e - s) / r
  5.         h = s + 0.5 * w
  6.             For n = 1 To r
  7.                 y = x5 * h ^ 5 + x4 * h ^ 4 + x3 * h ^ 3 + x2 * h ^ 2 + x * h + c
  8.                 area = y * w
  9.                 sum0 = sum0 + area
  10.                 h = h + w
  11.             Next n
  12.         r = r * 2
  13.         w = (e - s) / r
  14.         h = s + 0.5 * w
  15.             For p = 1 To r
  16.                 y = x5 * h ^ 5 + x4 * h ^ 4 + x3 * h ^ 3 + x2 * h ^ 2 + x * h + c
  17.                 area = y * w
  18.                 Sum = Sum + area
  19.                 h = h + w
  20.             Next p
  21.          r = r * 10
  22.     Loop Until Sum - sum0 <= cL
  23.  
  24.     EC = Sum
  25.  
  26. End Function
So far it seems to work properly if the condition is met the first time through the loop, but if it has to go through a second time, it enters an endless loop, and I don't know why. This also makes me think that at least my math is correct and I am not making a silly mistake there, which leads me to believe I am missing something on the coding side. I would really appreciate any help-this problem has been driving me crazy all weekend.

Thanks
Oct 25 '14 #1
3 1206
iam_clint
1,208 Expert 1GB
hmm can you give me an example of parameters to this function that would cause an infinite loop?
I tossed it in visual studio and passed it the parameters you specified in your post and it goes through the loop several times and the result ends up being 66418.75
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine(EC(5, 1, 2, -7, 1, 2, 1, 5, 0.01))
  2.  
Oct 26 '14 #2
Rabbit
12,516 Expert Mod 8TB
You should be explicit in your variable declarations. You have some in your function that aren't defined anywhere. And if they're not defined, then they might be pulling their value from a wider scope when you don't intend them to. And if they're pulling their value from outside the scope of the function, then you have values persisting that can mess with the function call.
Oct 26 '14 #3
iam_clint
1,208 Expert 1GB
I forgot to mention that, I casted all the variables to singles since you didn't define them in your post.
Oct 26 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rémi | last post by:
Hello all! I've been using Stephen Lebans' MouseWheel stuff for a number of years now, without problems. I've used on different combinations of Access and Windows, and have had great results with...
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
1
by: Apirsun | last post by:
I've been trying to use loops to add labels to a frame. I was able to get the program to compile but the labels aren't showing up. import javax.swing.*; import java.awt.*; import...
4
by: janice3 | last post by:
hi.I am a new member in this site. today my question is how to make a diamond shape using loops? the shape is like: * * * * * * * * * * * * * *...
3
by: aparna kulkarni | last post by:
i want to find out prime nos from 1 to 25 using do while loop.
1
by: =?ISO-8859-1?Q?Christian_R=FChl?= | last post by:
hi @all! i have a little problem with xsd. is it possible to define a loop in a schema? the xml files i want to validate look like this: - product -- component --- sub-component
4
by: isaacsanya01 | last post by:
Hello there, I need some help working with arrays and loops. i have a set of records that contains different actions. i need to iterate over the records, check the actions per record, and do some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.