Skip to content

Commit 5c99743

Browse files
committed
added code
1 parent 8484f74 commit 5c99743

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/derivative.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ std::ofstream ofile;
1212
int main(int argc, char* argv[])
1313
{
1414
char *outfilename;
15-
double derivativex[1000];
15+
double derivative;
1616
// Read in output file, abort if there are too few command-line arguments
1717
if( argc <= 3 ){
1818
std::cout << "Bad Usage: " << argv[0] <<
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <fstream>
4+
#include <iomanip>
5+
// Note: not using namespace for std
6+
7+
// Begin of main program
8+
9+
int main(int argc, char* argv[])
10+
{
11+
double derivative;
12+
// Read in output file, abort if there are too few command-line arguments
13+
if( argc <= 2 ){
14+
std::cout << "Bad Usage: " << argv[0] <<
15+
" read number of integration points" << std::endl;
16+
exit(1);
17+
}
18+
// extracting number of mesh points
19+
int i = atoi(argv[1]);
20+
double x = atof(argv[2]); // reading x-value
21+
double h = 1.0/((double) i); // setting up step size
22+
double Derivative = (exp(x+h)-2.*exp(x)+exp(x-h))/(h*h);
23+
double RelativeError = log10(fabs(Derivative-exp(x))/exp(x));
24+
std::cout << std::setw(15) << std::setprecision(8) << "relative error=" << RelativeError << std::endl;
25+
return 0;
26+
}
27+

0 commit comments

Comments
 (0)