Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on writing a recurrence relation in C

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
First of all, I'm an absolute beginner, so don't assume something is too obvious to say :p

Basically, I'm trying to write a program to evaluate, up to a given value of n, the recurrence relation:

y_n+1 = y_n-1 + 2*h*function(x_n,y_n)

Where _ denotes a subscript.

Ie. The current term is related to the previous term, and also the one before that.
(Here h is just a small constant, function() is a known function, and x_n+1 = x_n + h).

All the examples I have seen along these lines have been relating the current term with just the previous one (i=i+1 etc). How do I tell the program to use the one before that as well?

I hope this is just somthing simple that I don't know about, but I've been trying to work it out for a long time now and I'm completely stumped. I have to get past this in order to progress with my project, so any help at all would be very much appreciated.

Many thanks.
 
Hi Dave,

This may get you started.
Code:
#define MAX 10
double function(double x, double y)
{
/* put your function calculation here */
  return ( ...);
}
main()
{
  double y[MAX],x,h;
  int i;
/* Initialize your variables here */
  h    = hval;
  x    = x-initial-value-1;
  y[0] = y-initial-value-0;
  y[1] = y-initial-value-1;

  for (i=1;i<MAX-1;y++)
  {
    x += h;
    y[i+1] = y[i-1] + 2*h*function(x,y[i]);
  }
}
CaKiwi
 
Thanks very much!

That's a great help :)

Didn't know you could use arrays quite like that. I'll go try it.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top