Help with pow/ e (Eulers number)

Hey guys hows it going. Trying to figure out if im using the pow function correctly for this problem. Im trying to say e^x/2 in c++ language which im not very good at. I missed the problem for last weeks homework because I didn't really understand how to use sqrt. I think im messing up in the pow. I know the answers should be close to 0.90364 and 1.32552 because I used Excel. Any input would be greatly 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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double i, x, y, y0;
    double dx = 0.01;
	double e = 2.71828;

    x =dx;
    y= sqrt(pow(e,x/2))-2*sin(x);
    y0=y;

    for (i=0;i<=200;i++)
    {
        y0=y;
        x += dx;
        y = sqrt(pow(e,x/2))-2*sin(x);
    }
	if (y*y0<=0)
		cout << "x = " << x << "\n";


    cout << "x =" << x << "\n";


    system("pause");
    return 0;
}
 


I keep getting just one root and it comes out to 2.02 :(
Notice that you are never actually using x effectively. (That is to say, you are not using it the way you probably intend to)

You initialize 'x = dx;'

Line 11:
x = dx;

In your for loop later on, all you are doing is adding 'dx' to 'x' 201 times. This is why you are getting 2.02 as your answer.

Line 15:
1
2
3
4
5
6
for (i=0;i<=200;i++)
    {
        y0=y;
        x += dx;
        y = sqrt(pow(e,x/2))-2*sin(x);
    }


You are using the pow() function correctly, where your first parameter is your base, and your second parameter is your exponent.

Are you trying to print out 'y' instead of x?
Ah my error my professor told us we had to use

1
2
3
4
{
if (y*y0<=0)
		cout << "x = " << x << "\n";
}


in order to get both roots.

I changed the second part to y now and im getting both roots but they are both incorrect. Im looking like this now

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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double i, x, y, y0;
    double dx = 0.01;
	double e = 2.71828;

    x = dx;
    y= sqrt(pow(e,x/2))-2*sin(x);
    y0=y;

    for (i=0;i<=200;i++)
    {
        y0=y;
        x += dx;
        y = sqrt(pow(e,x/2))-2*sin(x);
    
	if (y*y0<=0)
		cout << "x = " << x << "\n";
	}

    cout << "y =" << y << "\n";


    system("pause");
    return 0;
}


But im getting the incorrect answers of
x=0.63 and y = -0.144601
Topic archived. No new replies allowed.