Important alert: (current site time 10/31/2014 3:13:16 AM EDT)
 

VB icon

Bisection Method(Interval Halving)

Email
Submitted on: 8/24/2011 11:35:13 AM
By: Akash Khaitan 
Level: Advanced
User Rating: Unrated
Compatibility: C
Views: 4244
author picture
(About the author)
 
     Takes a function, range and tolerance as input and outputs the bisection table. To compile use gcc -o filename filename.c -lmuparser

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Bisection Method(Interval Halving)
// Description:Takes a function, range and tolerance as input and outputs the bisection table. To compile use gcc -o filename filename.c -lmuparser
// By: Akash Khaitan
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13635&lngWId=3//for details.//**************************************

//Bisection Method
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <muParserDLL.h>
#define PARSER_CONST_PI 3.141592653589793238462643
#define PARSER_CONST_E2.718281828459045235360287
#define PARSER_MAXVARS		10
int main()
{
	muFloat_t calc(muChar_t [],float);
 	muChar_t szLine[100];
	int tol=0,i=0;
 	float val3=0,val1=0,val2=0;
	muFloat_t fVal=1;
	printf("Enter the function : ");
 	gets(szLine);
	printf("Enter range of x : ");
	scanf("%f %f",&val1,&val2);
	printf("Enter the value of tolerane : ");
	scanf("%d",&tol);
	printf("Iteration	X1		X2		X3		f(X3)\n");
	while(fVal>pow(10,tol))
	{
		val3=(val1+val2)/2;
		fVal = calc(szLine,val3);
		if(fVal<0)					 
 			fVal=fVal*-1;
 		printf("%d		%f	%f	%f	%f\n", i+1,val1,val2,val3,calc(szLine,val3));
		if(calc(szLine,val3)*calc(szLine,val1)<0)
			val2=val3;
		else
			val1=val3;
		i=i+1;
	}
	return 0;
}
muFloat_t calc(muChar_t szLine[],float val3)
{
	muFloat_t fVal=0,val=val3;
	muParserHandle_t hParser;
	hParser = mupCreate();
	mupDefineConst(hParser, "pi", PARSER_CONST_PI);
	mupDefineConst(hParser, "e", PARSER_CONST_E);
	mupDefineVar(hParser, "x", &val);
	mupSetExpr(hParser, szLine);
	fVal = mupEval(hParser);
	mupRelease(hParser);
	return fVal;
}


Other 19 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.