Hello,
I just started programming in C++ and i have a problem with a task.I need to write a program which illustrates the Bisection method in C++. I have some codes, which use the bisection code, but they are written in C not C++. I know that i am asking too much, but i really need to write the program and send it tomorrow(22.01) . I will be very gratefull to those who help me. Here are the codes that I mentioned above: - /*
-
This is a demonstration program for the
-
rootfinding subroutine 'bisect', given
-
in Section 4.1.
-
*/
-
-
#include <stdio.h>
-
#include <math.h>
-
-
-
float fcn(float);
-
float sign(float, float);
-
void bisect(float(*f)(float), float, float, float, float *, int *);
-
-
-
main()
-
{
-
-
float a, b, epsilon, root;
-
int ier;
-
-
-
while (1) {
-
-
/* Input problem parameters */
-
-
printf("\n\n What are a,b,epsilon");
-
printf("\n to stop, let epsilon=0 : \n");
-
scanf("%f %f %f", &a, &b, &epsilon);
-
if (epsilon == 0.0)
-
return 0;
-
-
/* Calculate root */
-
-
bisect(fcn, a ,b ,epsilon, &root, &ier);
-
-
/* Print answers */
-
-
printf("\n\n a = %11.4e b = %11.4e epsilon = %9.3e",
-
a, b, epsilon);
-
printf("\n root = %14.7e ier = %1d", root, ier);
-
}
-
system ("pause");
-
return 0;
-
}
-
-
-
-
float fcn(float x)
-
{
-
float result;
-
-
result = x - exp(-x);
-
return(result);
-
}
-
-
-
float sign(float a, float b)
-
{
-
if (b < 0.0)
-
return(-fabs(a));
-
else
-
return(fabs(a));
-
}
-
-
-
void bisect(float(*f)(float), float a, float b, float eps,
-
float *root, int *ier)
-
{
-
/*
-
The program uses the bisection method to solve
-
the equation
-
f(x) = 0.
-
The solution is to be in [a,b] and it is assumed
-
that
-
f(a)*f(b) <= 0.
-
The solution is returned in root, and it is to
-
be in error by at most eps.
-
-
ier is an error indicator.
-
If ier=0 on completion of the routine, then the
-
solution has been computed satisfactorily.
-
If ier=1, then f(a)*f(b) was greater than 0,
-
contrary to assumption.
-
*/
-
-
const float zero = 0.0, one = 1.0, two = 2.0;
-
float c, fa, fb, fc, sfa, sfb, sfc;
-
-
-
/* Initialize */
-
-
fa = (*f)(a);
-
fb = (*f)(b);
-
sfa = sign(one, fa);
-
sfb = sign(one, fb);
-
if (sfa*sfb > 0.0)
-
{
-
-
/* The choice of a and b is in error */
-
-
*ier = 1;
-
return;
-
}
-
-
/* Create a new value of c, the midpoint of [a,b] */
-
-
while (1) {
-
c = (a + b)/two;
-
if (fabs(b-c) <= eps)
-
{
-
-
/* c is an acceptable solution of f(x)=0 */
-
-
*root = c;
-
*ier = 0;
-
return;
-
}
-
-
/* The value of c was not sufficiently accurate.
-
Begin a new iteration */
-
-
fc = (*f)(c);
-
if (fc == zero)
-
{
-
-
/* c is an acceptable solution of f(x)=0 */
-
-
*root = c;
-
*ier = 0;
-
return;
-
}
-
-
sfc = sign(one, fc);
-
if (sfb*sfc > zero)
-
{
-
-
/* The solution is in [a,c] */
-
-
b = c;
-
sfb = sfc;
-
}
-
else
-
{
-
-
/* The solution is in [c,b] */
-
-
a = c;
-
sfa = sfc;
-
}
-
}
-
}
Here is a pseudocode vor Visual Basic: - 'Bisection Method
-
-
'Start loop
-
Do While (xR - xL) > epsilon
-
-
'Calculate midpoint of domain
-
xM = (xR + xL) / 2
-
-
'Find f(xM)
-
If ((f(xL) * f(xM)) > 0) Then
-
'Throw away left half
-
xL = xM
-
Else
-
'Throw away right half
-
xR = xM
-
End If
-
Loop
Unfortunately this is everything i can give you for the moment. As i mentioned i am new in programming and i can't give any more help.
6 11879
In most cases, a C program can be compiled as a C++ program, and it will work with only minor fixups. What is it you need, really? Have you tried your C code?
The only real difference here would be the use of cin and cout for your interface rather than scanf and printf. The rest stays the same
horace1 1,510
Recognized Expert Top Contributor
rather than using pointers when passing parameters to bisect -
void bisect(float(*f)(float), float a, float b, float eps,
-
float *root, int *ier)
-
you could use reference parameters (which don't exist in C)
also you could make it a template function so it could take parameters of different types (float, double, complex, etc)
for example, this is a copy of the Newton-Raphson method using a template function with reference parameters -
// Newton-Raphson method of finding roots //
-
// Passing references to functions f(x) and f'(x) as function parameters //
-
// also demonstrates use of a function template //
-
-
#include <iostream>
-
#include <iomanip>
-
#include <math.h>
-
#include <complex>
-
-
using namespace std;
-
-
//----------------------------------------------------------------------------//
-
// Function template: Newton-Raphson method find a root of the equation f(x) //
-
// see http://en.wikipedia.org/wiki/Newton's_method //
-
// Parameters in: &x reference to first approximation of root //
-
// (&f)(x) reference to function f(x) //
-
// (fdiv)(x) reference to function f'(x) //
-
// max_loop maxiumn number of itterations //
-
// accuracy required accuracy //
-
// out: &x return root found //
-
// function result: > 0 (true) if root found, 0 (false) if max_loop exceeded //
-
template <class T1>
-
int newton(T1 &x, T1 (&f)(T1), T1 (&fdiv)(T1),
-
int max_loop, const double accuracy)
-
{
-
T1 term;
-
do
-
{
-
// calculate next term f(x) / f'(x) then subtract from current root
-
term = (f)(x) / (fdiv)(x);
-
x = x - term; // new root
-
}
-
// check if term is within required accuracy or loop limit is exceeded
-
while ((abs(term / x) > accuracy) && (--max_loop));
-
return max_loop;
-
}
-
-
//----------------------------------------------------------------------------//
-
// test functions
-
-
double func_1(double x) // root is 1.85792
-
{ return (cosh(x) + cos(x) - 3.0); } // f(x) = cosh(x) + cos(x) - 3 = 0
-
-
double fdiv_1(double x)
-
{ return (sinh(x) - sin(x)); } // f'(x) = sinh(x) - sin(x)
-
-
double func_2(double x) // root is 5.0
-
{ return (x*x - 25.0); } // f(x) = x * x - 25 = 0
-
-
double fdiv_2(double x)
-
{ return (2.0 * x); } // f'(x) = 2x
-
-
complex<double> func_3(complex<double> x) // roots 5 + or - 3
-
{ return x*x - 10.0*x + 34.0; } // f(x) x^2 - 10x + 34
-
-
complex<double> fdiv_3(complex<double> x)
-
{ return 2.0*x -10.0; } // f'(x) 2x - 10
-
-
double func_4(double x) // three real roots 4, -3, 1
-
{ return 2*x*x*x - 4*x*x - 22*x + 24 ; } // f(x) = 2x^3 - 4x^2 - 22x + 24
-
-
double fdiv_4(double x)
-
{ return 6*x*x - 8*x - 22; } // f'(x) = 6x^2 - 8x - 22
-
-
//----------------------------------------------------------------------------//
-
// Main program to test above function
-
int main()
-
{
-
cout << "\nFind root of f(x) = cosh(x) + cos(x) - 3 = 0";
-
double x = 1.0; // initial 'guess' at root
-
if (newton(x, func_1, fdiv_1, 100, 1.0e-8))
-
cout << "\n root x = " << x << ", test of f(x) = " << func_1(x);
-
else cout << "\n failed to find root ";
-
-
cout << "\n\nFind root of f(x) = x * x - 25 = 0";
-
x = 1.0; // initial 'guess' at root
-
if ( newton(x, func_2, fdiv_2, 100, 1.0e-8))
-
cout << "\n root x = " << x << ", test of f(x) = " << func_2(x);
-
else cout << "\n failed to find root ";
-
-
cout << "\n\nFind root of f(x) = x^2 - 10x + 34 = 0";
-
complex<double> xc = complex<double>(1.0, 1.0); // initial 'guess' at root
-
if ( newton(xc, func_3, fdiv_3, 100, 1.0e-8))
-
cout << "\n root x = " << xc << ", test of f(x) = " << func_3(xc);
-
else cout << "\n failed to find root ";
-
-
cout << "\n\nFind root of f(x) = x^2 - 10x + 34 = 0";
-
xc = complex<double>(1.0, -1.0); // initial 'guess' at root
-
if ( newton(xc, func_3, fdiv_3, 100, 1.0e-8))
-
cout << "\n root x = " << xc << ", test of f(x) = " << func_3(xc);
-
else cout << "\n failed to find root ";
-
-
cout << "\n\nFind root of f(x) = 2x^3 - 4x^2 - 22x + 24 = 0";
-
x = 5.0; // initial 'guess' at root
-
if ( newton(x, func_4, fdiv_4, 100, 1.0e-8))
-
cout << "\n root x = " << x << ", test of f(x) = " << func_4(x);
-
else cout << "\n failed to find root ";
-
-
cin.get();
-
return 0;
-
}
-
-
The only real difference here would be the use of cin and cout for your interface rather than scanf and printf. The rest stays the same
I changed the printf and scanf to cout and cin and it here's what i got: - #include <iostream>
-
using namespace std;
-
#include <stdio.h>
-
#include <math.h>
-
-
-
float fcn(float);
-
float sign(float, float);
-
void bisect(float(float), float, float, float, float *, int *);
-
-
-
int main()
-
{
-
-
float a, b, epsilon, root;
-
int ier;
-
-
-
while (1) {
-
-
/* Input problem parameters */
-
-
cout <<("\n\n What are a,b,epsilon");
-
cout <<("\n to stop, let epsilon=0 : \n");
-
cin >>a, b, ε
-
if (epsilon == 0.0)
-
return 0;
-
-
/* Calculate root */
-
-
bisect(fcn, a ,b ,epsilon, &root, &ier);
-
-
/* Print answers */
-
-
cout <<("\n\n a = %11.4e b = %11.4e epsilon = %9.3e",
-
a, b, epsilon);
-
cout <<("\n root = %14.7e ier = %1d", root, ier);
-
}
-
-
return 0;
-
}
-
-
-
-
float fcn(float x)
-
{
-
float result;
-
-
result = x - exp(-x);
-
return(result);
-
}
-
-
-
float sign(float a, float b)
-
{
-
if (b < 0.0)
-
return(-fabs(a));
-
else
-
return(fabs(a));
-
}
-
-
-
void bisect(float(*f)(float), float a, float b, float eps,
-
float *root, int *ier)
-
{
-
/*
-
The program uses the bisection method to solve
-
the equation
-
f(x) = 0.
-
The solution is to be in [a,b] and it is assumed
-
that
-
f(a)*f(b) <= 0.
-
The solution is returned in root, and it is to
-
be in error by at most eps.
-
-
ier is an error indicator.
-
If ier=0 on completion of the routine, then the
-
solution has been computed satisfactorily.
-
If ier=1, then f(a)*f(b) was greater than 0,
-
contrary to assumption.
-
*/
-
-
const float zero = 0.0, one = 1.0, two = 2.0;
-
float c, fa, fb, fc, sfa, sfb, sfc;
-
-
-
/* Initialize */
-
-
fa = (*f)(a);
-
fb = (*f)(b);
-
sfa = sign(one, fa);
-
sfb = sign(one, fb);
-
if (sfa*sfb > 0.0)
-
{
-
-
/* The choice of a and b is in error */
-
-
*ier = 1;
-
return;
-
}
-
-
/* Create a new value of c, the midpoint of [a,b] */
-
-
while (1) {
-
c = (a + b)/two;
-
if (fabs(b-c) <= eps)
-
{
-
-
/* c is an acceptable solution of f(x)=0 */
-
-
*root = c;
-
*ier = 0;
-
return;
-
}
-
-
/* The value of c was not sufficiently accurate.
-
Begin a new iteration */
-
-
fc = (*f)(c);
-
if (fc == zero)
-
{
-
-
/* c is an acceptable solution of f(x)=0 */
-
-
*root = c;
-
*ier = 0;
-
return;
-
}
-
-
sfc = sign(one, fc);
-
if (sfb*sfc > zero)
-
{
-
-
/* The solution is in [a,c] */
-
-
b = c;
-
sfb = sfc;
-
}
-
else
-
{
-
-
/* The solution is in [c,b] */
-
-
a = c;
-
sfa = sfc;
-
}
-
}
-
}
Now it compilates and runs properly, but it doesn'n calculate anything. Maybe because I changed this line: - scanf("%f %f %f ",&a, &b, ε
to:
I dont know if it's ok but it's the only way in which the program compilates.
Should I do something else to make it better.
About the : - float fcn(float);
-
float sign(float, float);
-
void bisect(float(float), float, float, float, float *, int *);
I don't understand what should i do with this floats could you give me more tips?
I managed to make some source which compilates with no errors, but when i start it here's what comes out: - Error!
-
-100
-
Press any key to continue...
I managed to make some source which compilates with no errors, but when i start it here's what comes out: - Error!
-
-100
-
Press any key to continue...
I forgot the code here it is: - #include <iostream>
-
#include <stdlib.h>
-
#include <math.h>
-
-
using namespace std;
-
-
double f1( double x) {
-
return x*x-pow(1.034554,2);
-
}
-
double f2( double x) {
-
return 2*x;
-
}
-
-
double bisection(double (*f)(double),
-
-
double xL=-100, double xR=100) {
-
-
double xN;
-
-
if( (*f)(xL)*(*f)(xR) >0) {
-
cout << "Error!" << endl;
-
return xL;
-
}
-
-
while (fabs(xL-xR)>0.0001) {
-
xN = (xL+xR)/2;
-
if( (*f)(xL)*(*f)(xN) <0)
-
xR = xN; // i.e. xL and xN have different signs
-
// so xRand xN have the same signs
-
else
-
xL = xN;
-
}
-
return (xL+xR)/2;
-
}
-
-
int main() {
-
double tmp;
-
cout << bisection(f1,-100,100) << endl;
-
system("PAUSE");
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: aruna |
last post by:
to write a c program to find the roots of the equation using bisection method
that too using array or pointers
|
by: t4zone58 |
last post by:
Hey guys!
I am beginner at VB studying. I got confused with solve the functions by applying bisection method in VB. Basically I can understand the bisection method mathmatically. However i got stucked when i trying to write a code for it.
My code writing is
Dim xr, xl, xm as double
do
xl=0 (coz the domain is greater than 0)
|
by: Cappo |
last post by:
Hi,
I wrote befor about the bisection method in C++. This time I managed to make two source's and compile them. But both give me some error that i don't understand. Here are the codes:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
double f1( double x) {
return x*x-pow(1.034554,2);
|
by: beneyam |
last post by:
i want to develop a program on bisection method using c++.
the program should ask user to enter the function and the no of iterations at the run time.
any one do this i really appreciate.
10qs 4 ur help.
|
by: Baba sina |
last post by:
How can I solve f(x) with bisection method with the interval ?
| |
by: zxipo |
last post by:
the numerical prolem is is x*x*x*x-4*x-10=0 this to be solve by bisection method
|
by: jhen61315 |
last post by:
Hello there,
First time here in forum, I hope im welcome here.... And I hope you'll help me in my assignment in bisection method because i dont know how to start to make in visual basic.. If you dont mind, Im just asking and help me? or if its okay for you please e-mail me in my email addres so that I ask formally to help, some open question for visual basic and C++.
This is my email: jhen61315@yahoo.com
Thanks and GOD BLESS!!!!
|
by: lhbrown |
last post by:
Hi. I'm looking for some help with the Bisection method in Visual Basic, which I'm using for the first time as part of a university project. I am using the bisection method to work out the water level of a river for a given flow.
My variables are Wla, Wlb, Wlc, I have a sub routine called Qcalculate which calculates the discharge using the bisection method.
My problem is my original Wlb, I did have it set at a ridiculously high level that...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |