473,513 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using loops to find a convergence point in calculating an integral

1 New Member
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 1213
iam_clint
1,208 Recognized Expert Top Contributor
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 Recognized Expert Moderator MVP
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 Recognized Expert Top Contributor
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
5805
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 it. (Even better, considering what I paid for it.) Just hit a situation where it didn't work, though, and I was wondering if anyone else has hit...
0
2786
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 Replace" fuction, I scan the merchandise which then searches the spreadsheet for the matching inventory number. When it is found, I highlight that...
1
3103
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 javax.swing.border.*; public class Assignment3 extends JFrame { private ImageIcon cross = new ImageIcon("Assignment3/cross.gif"); private ImageIcon not = new...
4
4796
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
3990
by: aparna kulkarni | last post by:
i want to find out prime nos from 1 to 25 using do while loop.
1
4785
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
2184
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 calculations based on the actions. so for example if 1 record have one action, the calculation is performed once, if another record have 2 actions,...
0
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.