Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Back Room
  4. my math problem

my math problem

Scheduled Pinned Locked Moved The Back Room
questionalgorithmshelp
24 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kannan Kalyanaraman

    Nish played a trick on you, but its true that Colin is a Math genius here, its just that he hasnt went thru ur post. Anyway this place is for all your rants :-) probably try posting in the vc++ forum cheers Kannan I am not here to accommodate the requirements of mass hysteria.

    L Offline
    L Offline
    lucy 0
    wrote on last edited by
    #8

    okay, I'll try that. Thank you!

    1 Reply Last reply
    0
    • L lucy 0

      Rickard Andersson wrote: YOU are in the wrong forum, dude! what's the correct forum? I posted it in lounge, and I was told I was doing it wrongly. Nish suggested I could try here, and I got the same response. :(( also, why Rickard Andersson wrote: lucy wrote: Dr = FRACTION * ( pow(VectLength, EXPONENT) ); Thats wrong! this is wrong? I am using a math function from C++, and I think the syntax is correct? :confused: :confused:

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #9

      Hey man! I'm just joking with ya! I don't know what your doing at all! So I just tried to make you confused... and it worked! :laugh: :-D ;P ------------------------------------ ****Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

      L 1 Reply Last reply
      0
      • R Rickard Andersson20

        Hey man! I'm just joking with ya! I don't know what your doing at all! So I just tried to make you confused... and it worked! :laugh: :-D ;P ------------------------------------ ****Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

        L Offline
        L Offline
        lucy 0
        wrote on last edited by
        #10

        Rickard Andersson wrote: So I just tried to make you confused... yep, you did, especially I have only a small bunch of brain cells left from my math equations. X|

        1 Reply Last reply
        0
        • L lucy 0

          thanks a lot!

          C Offline
          C Offline
          ColinDavies
          wrote on last edited by
          #11

          Hi Lucy, I'm humbled that you selected me. :-) The best answer I have seen for your problem was by Chris M in the lounge where he made an assumption about Y. However I couldn't understand how he arrived at it. :-( But the code you have posted looks better than your Math, so I'll have a try soon and see if I can sove it. I'm unsure how the condition

          if ( VectLength != (double)0.0 )

          will relate to the solution Regardz Colin J Davies

          Sonork ID 100.9197:Colin

          More about me :-)

          N L 2 Replies Last reply
          0
          • C ColinDavies

            Hi Lucy, I'm humbled that you selected me. :-) The best answer I have seen for your problem was by Chris M in the lounge where he made an assumption about Y. However I couldn't understand how he arrived at it. :-( But the code you have posted looks better than your Math, so I'll have a try soon and see if I can sove it. I'm unsure how the condition

            if ( VectLength != (double)0.0 )

            will relate to the solution Regardz Colin J Davies

            Sonork ID 100.9197:Colin

            More about me :-)

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #12

            ****Colin Davies wrote: Hi Lucy, I'm humbled that you selected me. Blast! She posted that in the Lounge and I re-directed her to the sopabox and told her that Colin davies can help her :-) She first mis-posted it in the C++ forum. Then she came to the right forum. You can thank me too :-) Nish


            Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

            S 1 Reply Last reply
            0
            • L lucy 0

              Rickard Andersson wrote: YOU are in the wrong forum, dude! what's the correct forum? I posted it in lounge, and I was told I was doing it wrongly. Nish suggested I could try here, and I got the same response. :(( also, why Rickard Andersson wrote: lucy wrote: Dr = FRACTION * ( pow(VectLength, EXPONENT) ); Thats wrong! this is wrong? I am using a math function from C++, and I think the syntax is correct? :confused: :confused:

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #13

              lucy wrote: what's the correct forum? Colin has been posting a lot of math related stuff in this forum ever since it got renamed to soapbox. That's why I asked you to post it here. By the way I saw that you also posted it in the VC++ forum. Was that before or after the sopabbox post? And did anuyone answer youi there? Nish


              Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

              L 1 Reply Last reply
              0
              • N Nish Nishant

                ****Colin Davies wrote: Hi Lucy, I'm humbled that you selected me. Blast! She posted that in the Lounge and I re-directed her to the sopabox and told her that Colin davies can help her :-) She first mis-posted it in the C++ forum. Then she came to the right forum. You can thank me too :-) Nish


                Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

                S Offline
                S Offline
                Shog9 0
                wrote on last edited by
                #14

                Nish [BusterBoy] wrote: She first mis-posted it in the C++ forum. Then she came to the right forum. Read down a bit; she was re-directed to the C++ forum after posting here. Poor post, looking for a home... :) --------_**

                When will I realize that this skin I'm in Hey, it isn't mine And when will the kill be too much meat for me to hide on...

                **_

                -- Blind Melon, Skinned

                N 1 Reply Last reply
                0
                • S Shog9 0

                  Nish [BusterBoy] wrote: She first mis-posted it in the C++ forum. Then she came to the right forum. Read down a bit; she was re-directed to the C++ forum after posting here. Poor post, looking for a home... :) --------_**

                  When will I realize that this skin I'm in Hey, it isn't mine And when will the kill be too much meat for me to hide on...

                  **_

                  -- Blind Melon, Skinned

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #15

                  Shog9 wrote: Poor post, looking for a home... This is indeed a sad state of affairs if a post cannot find a forum of its own :( Nish


                  Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

                  M 1 Reply Last reply
                  0
                  • N Nish Nishant

                    Shog9 wrote: Poor post, looking for a home... This is indeed a sad state of affairs if a post cannot find a forum of its own :( Nish


                    Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

                    M Offline
                    M Offline
                    Maxwell Chen
                    wrote on last edited by
                    #16

                    Then, where can I post my turbo C++ / ASM / BIOS question?? Here's that question: I am trying to write a DOS program to wake up the BIOS program on AGP card...... :confused: Maxwell Chen People say "No news is good news". Then, no code is good code!?

                    1 Reply Last reply
                    0
                    • L lucy 0

                      here is the original code

                      void Compensate(float XCentroid, float ZCentroid,
                      float *CompensatedX, float *CompensatedZ)
                      {
                      double Dr = 0.0;
                      double LCentroidX = 0.0;
                      double LCentroidZ = 0.0;
                      double VectLength = 0.0;
                      double FRACTION = 0.009835;
                      double EXPONENT = 2.400;

                      LCentroidX = (double)XCentroid;
                      LCentroidZ = (double)ZCentroid;
                      
                      VectLength = sqrt( (pow(LCentroidX, 2.0)) + (pow(LCentroidZ, 2.0)) );
                      Dr = FRACTION \* ( pow(VectLength, EXPONENT) );
                      if ( VectLength != (double)0.0 )
                        {
                          \*CompensatedX = (float)LCentroidX + ( Dr \* ( LCentroidX / VectLength ) );
                          \*CompensatedZ = (float)LCentroidZ + ( Dr \* ( LCentroidZ / VectLength ) );
                        }
                      else
                        {	/\* return original co-ordinates \*/
                      \*CompensatedX = XCentroid;
                      \*CompensatedZ = ZCentroid;
                        }
                      return;
                      

                      } /* end Compensate */

                      so how can I reverse this algorithm? That is, based on CompensatedX and CompensatedZ, how can I get the original XCentroid and ZCentroid? I tried the following, but it doesn't work.

                      CompensatedVect = sqrt(pow(CompensatedX, 2.0), pow(CompensatedZ, 2.0));
                      CompensatedDr = pow((CompensatedVect/FRACTION), 1/EXPONENT);
                      XCentroid = CompensatedX / (1 + ompensatedDr/CompensatedVect);
                      ZCentroid = CompensatedZ / (1 + CompensatedDr/CompensatedVect);

                      M Offline
                      M Offline
                      Maxwell Chen
                      wrote on last edited by
                      #17

                      I tried to solve it using a pen and piece of paper, I got the reverse formula. But when I translated the formula to C++ code, I got a wrong answer.... :(( :(( :(( Maxwell Chen People say "No news is good news". Then, no code is good code!?

                      1 Reply Last reply
                      0
                      • N Nish Nishant

                        lucy wrote: what's the correct forum? Colin has been posting a lot of math related stuff in this forum ever since it got renamed to soapbox. That's why I asked you to post it here. By the way I saw that you also posted it in the VC++ forum. Was that before or after the sopabbox post? And did anuyone answer youi there? Nish


                        Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

                        L Offline
                        L Offline
                        lucy 0
                        wrote on last edited by
                        #18

                        Nish [BusterBoy] wrote: And did anuyone answer youi there? Yeah, Felix Gartsman answered me in lounge. But I had no luck in C++ forum. Felix said I should use Newton-Rapson algorithm. I am trying to find out what the algorithm is. I haven't touched such kind of stuff for how many years? 8 or ten?:((

                        1 Reply Last reply
                        0
                        • C ColinDavies

                          Hi Lucy, I'm humbled that you selected me. :-) The best answer I have seen for your problem was by Chris M in the lounge where he made an assumption about Y. However I couldn't understand how he arrived at it. :-( But the code you have posted looks better than your Math, so I'll have a try soon and see if I can sove it. I'm unsure how the condition

                          if ( VectLength != (double)0.0 )

                          will relate to the solution Regardz Colin J Davies

                          Sonork ID 100.9197:Colin

                          More about me :-)

                          L Offline
                          L Offline
                          lucy 0
                          wrote on last edited by
                          #19

                          ****Colin Davies wrote: if ( VectLength != (double)0.0 ) I think this is checked to make sure that we are not divided by a 0, in the equation: X = x + FRACTION * Dr * x / vect. In fact, I think we can test for a x!=0.0 && y!=0.0. That's enough.

                          1 Reply Last reply
                          0
                          • L lucy 0

                            here is the original code

                            void Compensate(float XCentroid, float ZCentroid,
                            float *CompensatedX, float *CompensatedZ)
                            {
                            double Dr = 0.0;
                            double LCentroidX = 0.0;
                            double LCentroidZ = 0.0;
                            double VectLength = 0.0;
                            double FRACTION = 0.009835;
                            double EXPONENT = 2.400;

                            LCentroidX = (double)XCentroid;
                            LCentroidZ = (double)ZCentroid;
                            
                            VectLength = sqrt( (pow(LCentroidX, 2.0)) + (pow(LCentroidZ, 2.0)) );
                            Dr = FRACTION \* ( pow(VectLength, EXPONENT) );
                            if ( VectLength != (double)0.0 )
                              {
                                \*CompensatedX = (float)LCentroidX + ( Dr \* ( LCentroidX / VectLength ) );
                                \*CompensatedZ = (float)LCentroidZ + ( Dr \* ( LCentroidZ / VectLength ) );
                              }
                            else
                              {	/\* return original co-ordinates \*/
                            \*CompensatedX = XCentroid;
                            \*CompensatedZ = ZCentroid;
                              }
                            return;
                            

                            } /* end Compensate */

                            so how can I reverse this algorithm? That is, based on CompensatedX and CompensatedZ, how can I get the original XCentroid and ZCentroid? I tried the following, but it doesn't work.

                            CompensatedVect = sqrt(pow(CompensatedX, 2.0), pow(CompensatedZ, 2.0));
                            CompensatedDr = pow((CompensatedVect/FRACTION), 1/EXPONENT);
                            XCentroid = CompensatedX / (1 + ompensatedDr/CompensatedVect);
                            ZCentroid = CompensatedZ / (1 + CompensatedDr/CompensatedVect);

                            M Offline
                            M Offline
                            Marshall
                            wrote on last edited by
                            #20

                            lucy wrote: so how can I reverse this algorithm? That is, based on CompensatedX and CompensatedZ, how can I get the original XCentroid and ZCentroid? I just started looking this over and I'm affraid I have no answers yet, but do have a few questions. It looks like you are saying all that is known when you "reverse" this, is the values of CompensatedX and CompensatedZ. Is that right? I'd have a much better chance of helping you if I knew what you are trying to accomplish with this algorithm. In the context of your application, what does "compensate" mean? How did you arrive at the algorithm in it's current form? :)

                            L 1 Reply Last reply
                            0
                            • M Marshall

                              lucy wrote: so how can I reverse this algorithm? That is, based on CompensatedX and CompensatedZ, how can I get the original XCentroid and ZCentroid? I just started looking this over and I'm affraid I have no answers yet, but do have a few questions. It looks like you are saying all that is known when you "reverse" this, is the values of CompensatedX and CompensatedZ. Is that right? I'd have a much better chance of helping you if I knew what you are trying to accomplish with this algorithm. In the context of your application, what does "compensate" mean? How did you arrive at the algorithm in it's current form? :)

                              L Offline
                              L Offline
                              lucy 0
                              wrote on last edited by
                              #21

                              Hi Marshall, As you can see from the first post, I get the CompensatedX and CompensatedZ from XCentroid and ZCentroid following the equations in the yellow box in my first post. In a math equation, it is: CompensatedX = XCentroid + FRACTION * Dr * XCentroid / VectLength; ---- Compensatedz = zCentroid + FRACTION * Dr * ZCentroid / VectLength; ---- where FRACTION is a constant of 0.009835, VectLength = sqrt(pow(XCentroid, 2), pow(ZCentroid, 2)); Dr = pow(Vect, 2.4); All the above is done in program 1. In my program 2, all I get is CompensatedX and CompensatedZ, and I need to reverse the above formula to get XCentroid and ZCentroid. But I couldn't get it. :-( I was told that I can use Newton-Rapson to approach it, but I don't know how. :-( Any more info do you need? I'd be glad to provide.

                              M 1 Reply Last reply
                              0
                              • L lucy 0

                                Hi Marshall, As you can see from the first post, I get the CompensatedX and CompensatedZ from XCentroid and ZCentroid following the equations in the yellow box in my first post. In a math equation, it is: CompensatedX = XCentroid + FRACTION * Dr * XCentroid / VectLength; ---- Compensatedz = zCentroid + FRACTION * Dr * ZCentroid / VectLength; ---- where FRACTION is a constant of 0.009835, VectLength = sqrt(pow(XCentroid, 2), pow(ZCentroid, 2)); Dr = pow(Vect, 2.4); All the above is done in program 1. In my program 2, all I get is CompensatedX and CompensatedZ, and I need to reverse the above formula to get XCentroid and ZCentroid. But I couldn't get it. :-( I was told that I can use Newton-Rapson to approach it, but I don't know how. :-( Any more info do you need? I'd be glad to provide.

                                M Offline
                                M Offline
                                Marshall
                                wrote on last edited by
                                #22

                                lucy wrote: I was told that I can use Newton-Rapson to approach it, but I don't know how. Newton's method? Yes, I suppose you could do that. You could certainly use it to find the roots - but since CompensatedZ is not a derivative of CompensatedX I don't see a quick application of Newton's method. I guess you could then solve as a system of equations, but it's too late for me to think straight on that. X| By the way, Newton's method (AKA Newton-Raphson) is an iterative method for estimating the roots (intercepts) of a function. You use it to find a value for x such that f(x) = 0. The trick is to make a good guess of x for the first iteration, else you'll end up going through a LOT more iterations than you want. So, if guess #1 is x1, then here's how it would work: x2 = x1 - [f(x1)/f '(x1)] x3 = x2 - [f(x2)/f '(x2)] . . . until xn is sufficiently close for your needs. I know that's probably not much help, :( but at least maybe you'll remember encountering Newton's method in a calculus course somewhere. I won't have time to play with this until lunch tomorrow so if you solve this before then, please let me know via email or posting on CP. Then I'll know if I can eat or do more math!:-D

                                L 2 Replies Last reply
                                0
                                • M Marshall

                                  lucy wrote: I was told that I can use Newton-Rapson to approach it, but I don't know how. Newton's method? Yes, I suppose you could do that. You could certainly use it to find the roots - but since CompensatedZ is not a derivative of CompensatedX I don't see a quick application of Newton's method. I guess you could then solve as a system of equations, but it's too late for me to think straight on that. X| By the way, Newton's method (AKA Newton-Raphson) is an iterative method for estimating the roots (intercepts) of a function. You use it to find a value for x such that f(x) = 0. The trick is to make a good guess of x for the first iteration, else you'll end up going through a LOT more iterations than you want. So, if guess #1 is x1, then here's how it would work: x2 = x1 - [f(x1)/f '(x1)] x3 = x2 - [f(x2)/f '(x2)] . . . until xn is sufficiently close for your needs. I know that's probably not much help, :( but at least maybe you'll remember encountering Newton's method in a calculus course somewhere. I won't have time to play with this until lunch tomorrow so if you solve this before then, please let me know via email or posting on CP. Then I'll know if I can eat or do more math!:-D

                                  L Offline
                                  L Offline
                                  lucy 0
                                  wrote on last edited by
                                  #23

                                  Hi Marshall, Thank you for the reminder of Newton's method. I am approaching it. Will tell you once I got it proved. Have a nice lunch!

                                  1 Reply Last reply
                                  0
                                  • M Marshall

                                    lucy wrote: I was told that I can use Newton-Rapson to approach it, but I don't know how. Newton's method? Yes, I suppose you could do that. You could certainly use it to find the roots - but since CompensatedZ is not a derivative of CompensatedX I don't see a quick application of Newton's method. I guess you could then solve as a system of equations, but it's too late for me to think straight on that. X| By the way, Newton's method (AKA Newton-Raphson) is an iterative method for estimating the roots (intercepts) of a function. You use it to find a value for x such that f(x) = 0. The trick is to make a good guess of x for the first iteration, else you'll end up going through a LOT more iterations than you want. So, if guess #1 is x1, then here's how it would work: x2 = x1 - [f(x1)/f '(x1)] x3 = x2 - [f(x2)/f '(x2)] . . . until xn is sufficiently close for your needs. I know that's probably not much help, :( but at least maybe you'll remember encountering Newton's method in a calculus course somewhere. I won't have time to play with this until lunch tomorrow so if you solve this before then, please let me know via email or posting on CP. Then I'll know if I can eat or do more math!:-D

                                    L Offline
                                    L Offline
                                    lucy 0
                                    wrote on last edited by
                                    #24

                                    Hi Marshall, Here's the final resolution::laugh:

                                    void DeCompensate_For_Lens_Aberations(double *XCentroid, double *ZCentroid,
                                    double CompensatedX, double CompensatedZ)
                                    {
                                    double CompensatedVect = 0.0;
                                    double FRACTION = 0.009835;
                                    double EXPONENT = 2.4;

                                    double delta = 0.000000001;
                                    
                                    CompensatedVect = pow((sqrt((pow(CompensatedX, 2.0) + pow(CompensatedZ, 2.0))
                                                                 / pow(CompensatedX, 2.0))), (EXPONENT - 1.0));
                                    double inc = CompensatedX / 100;
                                    double rem = CompensatedX;
                                    if ( rem < 0 ) rem = -rem;
                                    int count = 0;
                                    double try\_value = CompensatedX - inc;
                                    while ( rem > delta )
                                    {
                                        // x2 = x1 - f(x)/f'(x);
                                        try\_value = try\_value -
                                            (FRACTION \* pow(try\_value, EXPONENT) \* CompensatedVect + try\_value - CompensatedX)/       // f(x)
                                            (FRACTION \* EXPONENT \* pow(try\_value, (EXPONENT - 1)) \* CompensatedVect + 1);            // f'(x)
                                    
                                        rem = FRACTION \* pow(try\_value, EXPONENT) \* CompensatedVect + try\_value - CompensatedX;
                                    
                                        if ( rem < 0 )
                                            rem = -rem;
                                    
                                        count ++;
                                        if ( count > 30 )
                                            break;
                                    } // end of Newton formula for X
                                    
                                    if ( count <= 30 )
                                    {
                                        \*XCentroid = try\_value;
                                        \*ZCentroid = \*XCentroid \* CompensatedZ / CompensatedX;
                                    }
                                    else
                                        \*ZCentroid = OUT\_OF\_RANGE;
                                    

                                    } /* end of DeCompensate_For_Lens_Aberations */

                                    The heart of this solution is the Newton algorithm. Thank you so much! The limitation is: the input CompensatedX and CompensatedZ cannot be negative since we need a pow(x, 2.4), which doesn't work for negative value. To overcome this, just put fabs(CompensatedX) and fabs(CompensatedZ) to the function, and after return from this function, do this:

                                    if ( CompensatedX < 0.0 )
                                    XCentroid = -XCentroid;
                                    if ( CompensatedZ < 0.0 )
                                    ZCentroid = -ZCentroid;

                                    Last but not least, thank all you guys who helped me in this problem. I appreciated it a lot.:rose: :rose: :rose: :rose: :rose:

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups