473,396 Members | 1,864 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,396 software developers and data experts.

Problem with Infinite Loop in Half-Interval/Bisection Method

Can anyone assist me with my half-interval/bisection program. It does not terminate the while loop. Here is the code I've done so far.


import java.util.*;

public class HIM
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double upper, lower, ans, err, err2=1, temp1=0, temp2=0, temp3=0, temp4=0, temp5=0, x;
System.out.print("Enter Power of Polynomial: ");
int pow = sc.nextInt();
System.out.print("Enter Termination Criterion: ");
err = sc.nextDouble();
int arr[] = new int[(pow+1)*2];
int iter = 1;

for(int j=1; j<arr.length;j++)
{
System.out.print("\nEnter Coefficient: ");
arr[j-1] = sc.nextInt();
System.out.print("Enter Exponent: ");
arr[j] = sc.nextInt();
j++;
}

System.out.print("\nInput Formula: " + arr[0] + "x^" + arr[1] + " + " + arr[2] + "x^" + arr[3] + " + " + arr[4] + "x^" + arr[5] + " + " + arr[6] + "x^" + arr[7] + " + " + arr[8] + "x^" + arr[9] + " + " + arr[10] + "x^" + arr[11]);

System.out.print("\nEnter Upper Estimate of x: ");
upper = sc.nextDouble();
System.out.print("\nEnter Lower Estimate of x: ");
lower = sc.nextDouble();



while(err2>err)
{
System.out.print("\nIteration Number: " + iter);
x = (upper+lower)/2;
System.out.print("\nImproved Estimate: " + x);
for(int i=1;i<arr.length;i++)
{
temp1 += arr[i-1]*Math.pow(upper,arr[i]);
temp2 += arr[i-1]*Math.pow(lower,arr[i]);
temp3 += arr[i-1]*Math.pow(x,arr[i]);
i++;
}

System.out.print("\nValue of the function at the upper estimate: " + temp1);
System.out.print("\nValue of the function at the lower estimate: " + temp2);
System.out.print("\nValue of the function at the improved estimate: " + temp3);

temp4 = temp1*temp3;
temp5 = temp2*temp3;

System.out.print("\nProduct of the value function at the upper estimate & improved estimate : " + temp4);
System.out.print("\nProduct of the value function at the lower estimate & improved estimate : " + temp5);

if(temp4<0)
{
err2 = (upper-x)/upper;
if(err2<0)
{
err2 = -1*err2;
}
System.out.print("\nRelative Error: " + err2);
if(err2>err)
{
upper = x;
}
}
if(temp5<0)
{
err2 = (lower-x)/lower;
if(err2<0)
{
err2 = -1*err2;
}
System.out.print("\nRelative Error: " + err2);
if(err2>err)
{
lower = x;
}
}
iter++;
}
//System.out.print("Relative Error: " + err2);
}
}
Oct 26 '11 #1
0 1220

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

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
5
by: Allerdyce.John | last post by:
Hi, I have this piece of code which loops thru a STL list, but that causs an infinite loop. bool Executer::group(MyList& bl, ResultList & grl) { for (ExecuterList::iterator i =...
11
by: jojobar | last post by:
I have a aspx file (snippet shown below): ======= <td class="light-m1" id="rwCompleteButton" runat="server"><br/> <asp:ImageButton CssClass="clear-m1" runat="server" CommandName="Complete"...
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
11
by: kalar | last post by:
Hello, i have a problem with an infinite loop. I have got 4 arrays , each of them, have 5 integers the elements are from random funtcion, I don't want to have same elements in the arrays so i make...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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.