#include <iostream>
#include <cmath>
#include <stdlib.h>
usingnamespace std;
double f(double x);
double biseccion(double a, double b, double tolerancia, int maxiter);
int main()
{
double a, b, raiz, h;
double tolerancia=0.00000;
int maxiter=25;
cout << "Input interval start: ";
cin >> a;
cout << "Input interval end: ";
cin >> b;
cout << "\n";
h=f(a)*f(b);
if (h==0)
{
cout << "La raiz es cero.";
return 0;
}
if (h>0)
{
do
{
cout << "There is no root in that interval.";
cout << "\n";
cout << "\n";
cout << "Input interval start: ";
cin >> a;
cout << "Input interval end: ";
cin >> b;
cout << "\n";
h=f(a)*f(b);
}
while (h>0);
if (h==0)
{
cout << "Root is zero.";
return 0;
}
}
if (h<0)
{
cout << " # "<<"\n"<<"Iteration"<<"\t"<<" A"<<"\t"<<" B"<<"\t"<<" C"<<"\t"<<" f(c)"<<endl;
raiz=biseccion(a,b,tolerancia,maxiter);
cout << "\n";
cout << "The root is: "<< raiz <<endl;
}
return 0;
}
double f(double x)
{
return x*x*x-x-2;
}
double biseccion(double a, double b, double tolerancia, int maxiter)
{
double c;
int numiter=1;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
/*
if(f(b)*f(c)<0)
{
a=c;
}
else
{
b=c;
}
*/
cout<<" "<<numiter<<"\t"<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<f(c)<<endl;
numiter++;
}
while((abs(f(c))>tolerancia)&&(numiter<maxiter));
return c;
}
I want to focus on this for a moment.
1 2 3 4
double f(double x)
{
return x*x*x-x-2;
}
That function has x*x*x-x-2 before return, thats the mathematical function I need to calculate. Now, what I want is that instead I code this function user could input it by console. Something like "Input function: ", so user input something like "x*x-x+5", and THAT is placed after the return.
First I thought about using a string to store the user input, but of course, I can't use a string in a function that uses a double variable.
Is there a way to achieve what I need?, convert the content of a string variable (x*x-x+5) to used in a double variable?, replace the x*x*x-x-2 after the return with whatever user input when asked?
You need to manually parse the expression yourself, basically doing what your compiler would do. If you limit it to simple expressions, your task is a bit easier, but if you allow more complex syntax your task becomes harder.
Can you give an example of the syntactically most complex expression you will allow in your program? Like, pulling out all the stops?
Well, I was adviced about using a parser library, like muparser (http://muparser.beltoforion.de/index.html), but I never could make it work properly. Besides, I'm not sure if a parser is what I really need?. You think that's what I need?
Like I said, it depends on the complexity of expressions you want to deal with. Can you show me the most complex expression you want to allow as input?
Well, there is a small chance that I will use one or two functions like that, mostly because when you write something like x^3-3x^2+2x-5 c++ need to see it like (x*x*x)-(3(x*x))+(2*x)-5...
OK then, I'd recommend a recursive parser. Basically, you pretend the entire expression is wrapped in parens (), and whenever you encounter an open paren, recurively call the parse function on the rest of the expression, and go back up the recursion stack when you encounter a close paren ).
I would recommend a library if you can get one set up and if you are allowed to use one, bur if not, you will have to write the parser yourself.
I try that code and I can't resolve the error "Unexpected token "x" found at position 0". And when I replace "x*x*x-x-2" for "funcion" then the compiler nag me about conversion between string and double. It's like muparser it's not parsing... I'm so frustrated...
And if I replace "x*x*x-x-2" for "funcion", then I got this error: "cannot convert ‘std::string’ to ‘double’ in return".
If I read the documentation of muparser correctly, I need to define a variable that will contain what I need to parse (in this case the variable "funcion"), so I declare it with:
parser.DefineVar("funcion", &fVal);
Where &fVal is tha name for c++ and "funcion" is the name for muparser.
So, I
1 2
cout << "Input function: ";
cin >> funcion;
In theory, muparser will take the content from "funcion", parse it, and store result of parsing in fVal. Before muparser could parse, it need to know where is the expression to be parsed, so I
parser.SetExpr(funcion);
And then
fVal = parser.Eval();
But if I use fVal instead of "x*x*x-x-2" then I got "error: ‘fVal’ was not declared in this scope"
Lines 54-57 can be removed, they're not needed anymore - your expressions are strings now, not C++ code.
Also, please click in your output log, Ctrl+A to select all, Ctrl+C to copy it, and then Ctrl+V to paste here. You're not giving me the full error messages.