Hello everyone,
I'm trying to calculate numeric integrals with Monte Carlo Method. Briefly, it's a method to calculate integrals with random numbers. My assignment is to calculate multiple integrals (maximum 3) with the help of these method.
Algorithm:
1)Input number of multiple integrals (It can be 1,2 or 3)
2)Input a function (It can be f(x),f(x,y),f(x,y,z))
3)Input upper and lower limits (Depending on number of integrals. If user inputs
3 in 1st step, he/she must input 6 limits for three integrals)
4)Input number of experiments (This is related with Monte Carlo Method, a high number decreases the error between analytic solution and numerical solution)
5)Output
So the thing is I've coded all steps except the 2. Because I don't know how to input an equation from keyboard and evaluate it with limits of integral. I've tried to make it string but then it doesn't do the calculations. When I tried to double or int, then compiler doesn't know what is * or - means. If you give me an idea, I'd be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
// Monte Carlo Method for evaluating integrals
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
double Random(){
return rand()/(RAND_MAX+1.0);
}
int main(){
int degree;
double upperx,lowerx,uppery,lowery,upperz,lowerz,n,s=0;
string function;
cout << "Input your degree(1,2,3): " << endl;
cin >> degree;
if(degree==1){
cout << "Input your function: " << endl;
cin >> function; // How to input an equation?
cout << "Input upper limit of x: " << endl;
cin >> upperx;
cout << "Input lower limit of x: " << endl;
cin >> lowerx;
cout << "Input n: " << endl;
cin >> n;
for(int i=1; i<=n; i++){
double x = lowerx + (upperx-lowerx)*Random();
s+=function; // How to calculate x values in an equation and sum the results?
}
double mean = s/n;
double integ = (upperx-lowerx)*mean;
cout << integ;
}
else if (degree==2){
cout << "Input your function: " << endl;
cin >> function; // How to input an equation?
cout << "Input upper limit of x: " << endl;
cin >> upperx;
cout << "Input lower limit of x: " << endl;
cin >> lowerx;
cout << "Input upper limit of y: " << endl;
cin >> uppery;
cout << "Input lower limit of y: " << endl;
cin >> lowery;
cout << "Input n: " << endl;
cin >> n;
for(int i=1; i<=n; i++){
double x = lowerx + (upperx-lowerx)*Random();
double y = lowery + (uppery-lowery)*Random();
s+=function; // How to calculate x and y values in an equation and sum the results?
}
double mean = s/n;
double integ = (upperx-lowerx)*(uppery-lowery)*mean;
cout << integ;
}
else if (degree==3){
cout << "Input your function: " << endl;
cin >> function; // How to input an equation?
cout << "Input upper limit of x: " << endl;
cin >> upperx;
cout << "Input lower limit of x: " << endl;
cin >> lowerx;
cout << "Input upper limit of y: " << endl;
cin >> uppery;
cout << "Input lower limit of y: " << endl;
cin >> lowery;
cout << "Input upper limit of z: " << endl;
cin >> upperz;
cout << "Input lower limit of z: " << endl;
cin >> lowerz;
cout << "Input n: " << endl;
cin >> n;
for(int i=1; i<=n; i++){
double x = lowerx + (upperx-lowerx)*Random();
double y = lowery + (uppery-lowery)*Random();
double z = lowerz + (upperz-lowerz)*Random();
s+=function; // How to calculate x, y and z values in an equation and sum the results?
}
double mean = s/n;
double integ = (upperx-lowerx)*(uppery-lowery)*(upperz-lowerz)*mean;
cout << integ;
}
else cout << "Please put a number between 1 and 3!" << endl;
}
|