![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
Hi everyone,
I have written a MC numerical integration program for solving indefinite integrals. The problem I am facing is trying to dump the output of the numbers generated to a file so I can plot it using Excel or other spreadsheet. I have a function for the output but it doesn't seem to work properly. It only gives me one set of number. I need the entire set of numbers generated (>10000) by the program to analyze and plot in a spreadsheet. Also is there a particular format I should output the file so that it can be easily read by any spreadsheet program such as (.csv) format?
#include <iostream>
#include <fstream>
#include <cstdlib> // random function rand() and srand()
#include <cmath> // pow() and sqrt() functions
using namespace std;
float fileout(double resultF4, double resultErr4);
main() {
double x, func = 0.0, func2 = 0.0, resultF, errorF, resultErr;
int itry, ntry;
cout << "Input the number of MC trials" << "\n";
cin >> ntry;
// seed rand() function with void srand( (unsigned) time((int *)0) ); dependant on time program run
srand((unsigned)time((long *)0));
for (itry=0; itry<ntry; itry++) {
// random generate to largest number (2^31 - 1) RAND_MAX
x = rand()/(float)RAND_MAX;
func = func + pow(x,2); // function to be evaluated is parabola of x^2
func2 = func2 + pow(x,4);
errorF = sqrt(func - func2);
resultF = func/ntry;
resultErr = errorF/ntry;
fileout(resultF, resultErr);
}
}
float fileout(double resultF2, double resultErr2)
{
ofstream out("output.dat");
if (!out) {
cout << "Cannot open file." << "\n";
return 1;
}
out << "Function = " << resultF2 << " " << " Error = " << resultErr2 << "\n";
out.close();
}I am using a Debian Linux with GNU C/C++. Anyone can help I really appreciate it. Thank you. |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|