Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Can somebody help me write a c++ code for an iterative method.
For example:

val= x+ sin(val);...................equation 1
where the initial calculation is different for val, lets say val=PI-tanz.

I need to substitute this val intially in equation 1. And then subsitute that answer in the subsequent iteration until There is not much difference!

I wonder how to code that! Any suggestions?
Posted

Wouldn't it just be something like:
double prev, val=PI-tan(z);
do {
   prev = val;
   val=x+sin(val);
} while(fabs(prev-val)<epsilon);>

EPSILON is something you need to define as your "not much difference", for example 0.05

Good luck!
 
Share this answer
 
Comments
Sumal.V 22-Feb-12 8:32am    
Hello! Thanks a ton! yup i'm looking for something like that!!! I really hope this works! cheers
Haven't bothered to test this, but it should work with little (or no) fixes:
C++
typedef double valuetype; // you didn't tell us the type for val ;-)

class sequencer {
public:
   virtual valuetype getNext(valuetype v) = 0; //prototype for the iteration function
   valuetype getAt(std::size_t iterations, valuetype initial_value) {
      v = initial_value;
      for (std::size_t i = 0; i < iterations; ++i) {
          v = getNext(v);
      }
      return v;
   }
};

class AddSinusSequence : public sequencer {
public:
   virtual valuetype getNext(valuetype v) {
      return v + sin(v);
   }
};

int main() {
   AddSinusSequence ass;
   std::cout << "first value: " << 1.5 << std::endl;
   std::cout << "fifth value: " << ass.getAt(5, 1.5) << std::endl;
   return 0;
}

Note that there is in fact no need for the two classes if there is really only one function you're interested in: I only used the classes as a means to separate the function you want to iterate from the actual calculation based on that.

[edit] added missing : public sequencer [/edit]

[edit2] the simple version:
C++
double AddSinus(double value) {
   return value + sin(value);
}
double getAt(unsigned i, double initial_value) {
   double v = initial_value;
   for (unsigned i=0; i < iterations; ++i)
      v = AddSinus(v);
   return v;
}
int main() {
   double initial = 1.5;
   double v_at_5 = getAt(5, initial);
   // output as needed ...
   return 0;
}

[/edit2]
 
Share this answer
 
v2
Comments
Sumal.V 22-Feb-12 8:17am    
Well thanks very much. Need some time to understand though!

I did not want to add complicated stuff as I'm dealing with calculating longitudes and the unit is degrees, minutes and sec!! And that is the datatype of val
Stefan_Lang 22-Feb-12 8:28am    
I fixed the initial version and added a simpler one: AddSinus is your iteration function, getAt calculates the value at a given iteration (in the example call, it's the 5th). Instead of the typedef I just used double.

The first version does exactly the same, only that it uses a virtual function overload to enable you to reuse the code for different iteration functions with minimal overhead.
Sumal.V 22-Feb-12 8:34am    
Oh! okay I got that! Thanks
Maximilien 22-Feb-12 8:39am    
Nice, but it's missing a lambda or two in there! :-)
Stefan_Lang 22-Feb-12 10:17am    
Ah, good idea, but I'm not that much into functional programming ;)
I did consider using a template instead of the typedef, but I wanted to keep it at housewife level. :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900