Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
I've written this program to try and solve y(0.5) where dy/dx=y^3(1-x^2). Code: /* Eulers method for dy/dx = y^3(1-x^2), approximating y(0.5)*/ #include<iostream> #include<cmath> #include<cstdlib> using namespace std; int main() { ...
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    33

    C++ Euler's Method.

    I've written this program to try and solve y(0.5) where dy/dx=y^3(1-x^2).
    Code:
    /* Eulers method for dy/dx = y^3(1-x^2), approximating y(0.5)*/
    
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    using namespace std;
    
    int main() {
    
    	double x = 0 , y = 0 , h , k;
    	int i;
    	
    	cout << "Enter a step size:";
    	cin >> h;
    
    	k = 0.5 / h; //The number of steps (preferably, h divides 0.5)
    
    	cout << "x_0 = " << x << '\n';
    	cout << "y_0 = " << y << '\n' << '\n';
    
    	for( i=1 ; i <= k ; i++) {
    
    		y = y + h*((y^3)*(1-x^2));
    		x = x + h;
    		
    		cout << "x_" << i << " = " << x << '\n';
    		cout << "y_" << i << " = " << y << '\n' << '\n';
    
    		}
    
    return 0;
    
    }
    Unfortunately, when I run this program I get:


    name@computer ~ $ g++ eulerass.cpp -o eulerass -Wall
    eulerass.cpp: In function ‘int main()’:
    eulerass.cpp:23: error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator^’
    eulerass.cpp:23: error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator^’
    eulerass.cpp:23: warning: suggest parentheses around arithmetic in operand of ‘^’

    so it has something to do with the "y = y + h*((y^3)*(1-x^2));" part of my program.

    I don't understand what's wrong with this. I have a double on the left hand side, and only double values on the right hand side.

    Can someone show me how to do this part correctly and also explain why the compiler doesn't like it?

    Thanks
    Last edited by elija; 02-12-2012 at 07:35 PM. Reason: Added code tags

  2. #2
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,301
    I added [ code ] tags to make your code readable. I suspect your problem is that y is a double and 3 (or 2) are integers. Try passing them as doubles.
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    Hi Catch22,
    I see your problem, you can do this in C or C++:
    Code:
    const int x = 2;
    const int y = x^2;
    but conversely, you cannot do this in C or C++:
    Code:
    const double x = 2.0;
    const double y = x^2.0;
    but the good news is the math library will do it for you. The above fragment should be:
    Code:
    const double x = 2.0;
    const double y = pow( x, 2.0 );

  4. #4
    Just Joined!
    Join Date
    Feb 2007
    Posts
    4
    I believe Brian is correct: you have to use the pow() function. Note that the ^ operator is not exponentiation, so even if you could pass it doubles, you wouldn't get the answer you need from it.

  5. #5
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,301
    DOH! Brain fart moment there ^ is XOR not power
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    33
    Thanks! I managed to get it to work (and also modified it slightly to accommodate later parts of the question). Here is the final code:

    Code:
    /* Eulers method for dy/dx = y^3(1-x^2), approximating y(x)*/
    
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    using namespace std;
    
    int main() {
    
    	double x = 0 , y = 1 , h , k, l;
    	int i;
    
    	cout << "Enter x value for y(x):";
    	cin >> l;
    	
    	cout << "Enter a step size:";
    	cin >> h;
    
    	k = l / h; //The number of steps (preferably, h divides l)
    
    	cout << "x_0 = " << x << '\n';
    	cout << "y_0 = " << y << '\n' << '\n';
    
    	for( i=1 ; i <= k ; i++) {
    
    		y = y + h*((pow(y,3.0))*(1-pow(x,2.0)));
    		x = x + h;
    		
    		cout << "x_" << i << " = " << x << '\n';
    		cout << "y_" << i << " = " << y << '\n' << '\n';
    
    		}
    
    return 0;
    
    }
    I have a final problem that I would like to address. I want to write something like:

    if( k != integer) { cout << "h must divide x"; }
    else { Rest of the code}

    but how do I write the k != integer part? I think I would have to use booleans, but I can't see how.

  7. #7
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    You should be able to do this with the typeid operator. I'm not sure what the support is like for this on Linux (I've never used it on there). I also think you might need RTTI (runtime type information) turned on - and that the output might be compiler-specific.
    Linux user #126863 - see http://linuxcounter.net/

  8. #8
    Just Joined!
    Join Date
    Dec 2009
    Posts
    6
    If you just want to check whether the double k is an integer (say, 5.0), I guess you can just check wheter it's equal to it's own floor value:

    Code:
    if (k == floor(k)) 
            ...

  9. #9
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    Quote Originally Posted by Leibnix View Post
    If you just want to check whether the double k is an integer (say, 5.0), I guess you can just check wheter it's equal to it's own floor value:

    Code:
    if (k == floor(k)) 
            ...
    The problem with == on floats are well documented. Because of the way the number is stored (i.e. mantissa and exponent) rounding errors can be introduced. You should only use the comparative '>' and '<' with floats for this reason.

    If you ever have to do fractional computations where equality is important, then using fixed-point numbers is usually better than using floating point ones.
    Linux user #126863 - see http://linuxcounter.net/

  10. #10
    Just Joined!
    Join Date
    May 2008
    Posts
    3
    Too many languages. Try the equivalent of abs(k - int(k+epsilon1)) < epsilon2 where in takes the integer portion of the number. Select your choice of epsilon. 0.00...01 or even 0.5 if you were rounding (which you are not).
    The floor produces the hazard of something that you wanted to be an integer of 4.9999999998 but floor produces something that is not close enough to 1 to be considered an integer.
    The two epsilon may be the same, or epsilon 1 can be a rounding factor, and epsilon2 is your "close enough to an integer" value. A proper choice of epsilon1 would permit using floor for int.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...