I have written a simple code to calculate an integral numerically with both the midpoint and trapezoidal rule. It works fine, but now I would like to modify it so that it can calculate the same integral but with an unspecified parameter. So instead of the integral of e^(-x^2), I would like to calculate the integral of e^(-\alpha*x^2). However, I am stuck here. I tried to just declare a new double alpha without initializing it, but that clearly takes me no where. Is there a way to do such a thing in C++? Thank you in advance.
//=============================================================
// A simple program to numerically integrate a Gaussian over
// the interval a to b
//=============================================================
#include <iostream>
#include <cmath>
using namespace std;
// function implementing the midpoint rule
double midpoint(double a, double b, int n){
double sum = 0.0;
double width = (b-a)/n;
for (int i = 1; i < n; i++){
sum += exp((-1)*(a+(i+0.5)*width)*(a+(i+0.5)*width))*width;
}
return sum;
}
//function implementing the trapezoidal rule
double trapezoidal(double a, double b, int n){
double width = (b-a)/n;
double sum = (width/2)*(exp(-a*a)+exp(-b*b));
for (int i = 1; i < n; i++){
sum += 2*exp((-1)*(a+i*width)*(a+i*width))*width/2;
}
return sum;
}
int main(){
cout << "THIS IS A SIMPLE PROGRAM TO INTEGRATE A GAUSSIAN OVER A CERTAIN INTERVAL." << endl;
int n;
double a, b;
cout << "Enter the lower and upper limit of integration as doubles" << endl;
cin >> a >> b;
cout << "Enter the number of partitions" << endl;
cin >> n;
double actual = 0.886207;
double midRule = midpoint(a,b,n);
double trapRule = trapezoidal(a,b,n);
cout << "For the function e^(-x^2) integrated from "<< a << " to " << b << endl;
cout << "The analytic value of the integral is: " << actual << endl;
cout << "The midpoint value of the integral for " << n << " partitions is: " << midRule << endl;
cout << "The trapezoidal value of the integral for " << n << " partitions is: " << trapRule << endl;
cout << "The percent error for the midpoint is: " << (abs(actual-midRule)/actual)*100 << "%" << endl;
cout << "The percent error for the trapezoidal is: " << (abs(actual-trapRule)/actual)*100 << "%" << endl;
return 0;
}
Related
I appreciate you taking the time to read my problem sorry I'm just a beginner in programming. Apparently I am not that good in loops and if else statement and I don't know whats wrong in my code. when I run it and input "up" its just go to (8,10). it should have been (0,1) then the console will ask which direction they want to go again. Thanks in advance!!! :D
#include<iostream>;
#include<string>;
using namespace std;
int main()
{
int x = 0;
int y = 0;
string input;
cout << "objective: go to (8,9)" << endl;
cout << "current location (" << x << "," << y << ")" << endl;
while ( (x = 8) && (y = 9) )
{
cout << "which way do you want to go?: ";
cin >> input;
if (input == "up")
{
++y;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "down")
{
--y;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "right")
{
++x;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "left")
{
--x;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
}
cout << "current location (" << x << "," << y << ")" << endl;
cout << "Congratulations! You have reach your destination! :D" << endl;
system("pause");
return(0);
}
"
I tried removing all the break but it went straight down to "congratulations! you have reach your destination" when I tried to run it and I change the condition to (x ==8) && (y ==9) still no improvement.
"
Your loop condition is incorrect. The while loop will only run if x is 8 and y is 9. They are 0 and 0 at the beginning of the program, which explains why we skip the loop. The real condition is: "run the loop as long as x and y are not these values." while (x != 8 || y != 9) and alternately alternately: while (!(x == 8 && y == 9))
[Updated Code Pictutre][1]Can someone help me understand what I am doing wrong with my array in this code? It is giving me the same error for all my variables of the array
I dont know how to show you the whole code without adding like 5 pictures, i tried to capture all the important information
Ok I have posted the code here below
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
// output for a single input driving frequency
double m, c1, c2, k1, k2, r, Y;
m = c1 = c2 = k1 = k2 = r = Y = 0;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
double ceq, keq, wn, dampratio, staticamp;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
double cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double Fo, alpha, w, t;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
double Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
double phi = atan((ceq*w) / (keq - m*w*w));
double xp = Xp*sin(w*t - phi);
double H, Td, Ftwall, Ftbase;
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
//*****************************Section 1.2 **********************************************
// 1.2 output for a range of driving frequencies
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = keq = 0;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m); //natural frequency
dampratio = ceq / (2 * sqrt(m*keq)); // damping ratio
//Determine the Case
cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double staticamp[200], Xp[200], xp[200], Fo[200], phi[200], w[200];
double alpha[200], Td[200], H[200], Ftbase[200], Ftwall[200], r[200];
r[1] = .1;
for (int i = 0; i<200; i++) {
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r[i] * wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha[i] = atan(-c2*w / k2);
}
//Static amplitude
staticamp[i] = 0;
staticamp[i] = Fo / keq;
Xp[i] = (Fo / keq) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
phi[i] = atan((ceq*w) / (keq - m*w*w));
xp[i] = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td[i] = 0;
Td[i] = (Fo / (Y*keq)) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Frequanecy Response
H[i] = 0;
H[i] = 1 / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Force Transmissibilty to the bang
Ftbase[i] = 0;
Ftbase[i] = (Xp*sqrt(pow((k1 - keq*r[i] * r[i]), 2) + pow((c1*wn*r[i]), r[i]))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall[i] = 0;
Ftwall[i] = (Xp*sqrt((k1*k1) + pow((c1*r[i] * wn), 2))) / (Y*k2);
//increment r
r[i + 1] = r[i] + .1;
}
ofstream Data(" 1.2 Case 1 .txt"); // File Creation to import in text file to graph
for (int i = 0; i > 200; i++) { //NEED TO CHANGE FILE NAME FOR NEW DATA SET
Data << r[i] << "," << H[i] << "," << phi[i] << "," << Td[i] << "," << Ftbase[i] << "," << Ftwall[i] << endl;
}
//************************* Section 2 ****************
cout << "\t\tSection 2" << endl << endl;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
cout << "Which Case (1-5): ";
cin >> cas;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
phi = atan((ceq*w) / (keq - m*w*w));
xp = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
return 0;
}
I have a problem running an MPI program (written in C or C++) over a cluster comprising of two nodes.
Details:
OS: Ubuntu 16.04
No. of nodes: 2 (master and slave)
Everything works well. When I run a simple mpi_hello program on the cluster with 12 as an argument (no. of processes) I see 4 mpi-hello instances running on the slave node (checked using top).
Output on master node + mpi_hello instances running on the second node (slave node)
When I try to run another program (for instance a simple program calculating and printing prime numbers in a range) it is running on the master node but i don't see any instances of it on the slave node.
#include <stdio.h>
#include<time.h>
//#include</usr/include/c++/5/iostream>
#include<mpi.h>
int main(int argc, char **argv)
{
int N, i, j, isPrime;
clock_t begin = clock();
int myrank, nprocs;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
printf("Hello from the processor %d of %d \n" , myrank, nprocs);
printf("To print all prime numbers between 1 to N\n");
printf("Enter the value of N\n");
scanf("%d",&N);
/* For every number between 2 to N, check
whether it is prime number or not */
printf("Prime numbers between %d to %d\n", 1, N);
for(i = 2; i <= N; i++){
isPrime = 0;
/* Check whether i is prime or not */
for(j = 2; j <= i/2; j++){
/* Check If any number between 2 to i/2 divides I
completely If yes the i cannot be prime number */
if(i % j == 0){
isPrime = 1;
break;
}
}
if(isPrime==0 && N!= 1)
printf("%d ",i);
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\nThe time spent by the program is %f\n" , time_spent);
while(1)
{}
MPI_Finalize();
return 0;
}
What could be the possible reasons behind it ?
Are there any other ways to check if it is running on the slave node as well ?
Thanks
Okay so here is a code I worked with. A vector containing first 500 integers. Now I want to divide them into 4 processes equally (i.e. each process gets 125 integers -- the first process gets 1-125, the second 126-250 and so on). I tried to use MPI_Scatter(). but I don't see the data equally divided or even divided. Do I have to use MPI_Recv() (I have another piece of code which is functional and uses only scatter to divide data equally).
Could you pint out any problems in the code. Thanks
int main(int argc, char* argv[])
{
int root = 0;
MPI_Init(&argc, &argv);
int myrank, nprocs;
MPI_Status status;
//variables for prime number calculation
int num1, num2, count, n;
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
char name[MPI_MAX_PROCESSOR_NAME + 1];
int namelen;
MPI_Get_processor_name(name, &namelen);
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
int size = 500;
int size1 = num2 / nprocs;
cout << "The size of each small vector is " << size1 << endl;
auto start = get_time::now(); //start measuring the time
vector<int> sendbuffer(size), recbuffer(size1); //vectors/buffers involved in the processing
cout << "The prime numbers between " << num1 << " and " << num2 << " are: " << endl;
if (myrank == root)
{
for (unsigned int i = 1; i <= num2; ++i) //array containing all the numbers from which you want to find prime numbers
{
sendbuffer[i] = i;
}
cout << "Processor " << myrank << " initial data";
for (int i = 1; i <= size; ++i)
{
cout << " " << sendbuffer[i];
}
cout << endl;
MPI_Scatter(&sendbuffer.front(), 125, MPI_INT, &recbuffer.front(), 125, MPI_INT, root, MPI_COMM_WORLD);
}
cout << "Process " << myrank << " now has data ";
for (int j = 1; j <= size1; ++j)
{
cout << " " << recbuffer[j];
}
cout << endl;
auto end = get_time::now();
auto diff = end - start;
cout << "Elapsed time is : " << chrono::duration_cast<ms>(diff).count() << " microseconds " << endl;
MPI_Finalize();
return 0;
}`
Okay The program compiles, but i am getting wrong output. Any help would be awesome. i do understand there are many other examples on this topic and i have looked at them all. None seemed to have help me clearly understand.
#include <iostream>
#include <cmath>
using namespace std;
double quadplus(float a, float b, float c);
double quadminus(float a, float b, float c);
int main()
{
double a, b, c;
double discriminant;
cout << "Please Enter variables for a, b, and c." << endl << endl;
cout << "Enter number for variable a." << endl << endl;
cin >> a;
cout << "Enter number for variable b." << endl << endl;
cin >> b;
cout << "Enter number for variable c." << endl << endl;
cin >> c;
discriminant = (b * b) - (4 * a * c);
if(discriminant == 0)
cout << quadplus(a,b,c) << endl;
else if(discriminant > 0)
cout << quadplus(a,b,c) << endl;
else if(discriminant < 0)
cout << quadminus(a,b,c) << quadplus(a,b,c) << endl;
return 0;
}
double quadplus(float a, float b, float c)
{
return ((-1 * b) + (sqrt(( b * b) - (4 * a * c))) / (2 * a));
}
double quadminus(float a, float b, float c)
{
return ((-1 * b) - (sqrt(( b * b) - (4 * a * c))) / ( 2 * a));
}
cout << quadplus << endl;
should probably be
cout << quadplus(a, b, c) << endl;
Also, if the discriminant is negative, your solutions will be
complex numbers of the form x + yi and your code doesn't properly account
for that. If discriminant != 0, you should probably print both roots.
Below is the code which provides width,height and BGR values of 2 images.But the problem is until i close the first image i cant see the second image.What modifications to be made such that i can see both images at a time and get the all the pixel values.
1 . #include <cv.h>
#include<iostream>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv[])
{
int width,height;
int i=0,j=0,k=3,l=3;
IplImage *img1 = cvLoadImage("E:/images.jpg");
cvNamedWindow("Image1:",1);
cvShowImage("Image1:",img1);
cout << "Width:" << img1->width << endl;
cout << "Height:" << img1->height << endl;
CvScalar s;
s=cvGet2D(img1,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image1:");
IplImage *img2 = cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
cvNamedWindow("Image2:",2);
cvShowImage("Image2:",img2);
cout << "Width:" << img2->width << endl;
cout << "Height:" << img2->height << endl;
s=cvGet2D(img2,k,l); // get the (k,l) pixel value
printf("B1=%f, G1=%f, R1=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image2:");
cvReleaseImage(&img1);
cvReleaseImage(&img2);
return 0;
}
problem is with cvWaitKey()
try:
IplImage *img1 = cvLoadImage("E:/images.jpg");
cvNamedWindow("Image1:",1);
cvShowImage("Image1:",img1);
cout << "Width:" << img1->width << endl;
cout << "Height:" << img1->height << endl;
CvScalar s;
s=cvGet2D(img1,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
IplImage *img2 = cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
cvNamedWindow("Image2:",2);
cvShowImage("Image2:",img2);
cout << "Width:" << img2->width << endl;
cout << "Height:" << img2->height << endl;
s=cvGet2D(img2,k,l); // get the (k,l) pixel value
printf("B1=%f, G1=%f, R1=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image1:");
cvDestroyWindow("Image2:");
cvReleaseImage(&img1);
cvReleaseImage(&img2);