Hello everyone, I am a Mathematics major and my mentor asked me to put together a program that evaluates explicit stencil methods along the difference mesh. Basically I need to use a recursive method to get my value, as I know base values and boundaries, but the C++ experience I have is minimal, and I need to use iterated loops. I am having trouble, and if anyone can help I would appreciate it.

This is the wiki page for Explicit stencil: (Note I am not using standard central difference)


Here is my code thus far:

This program will take in a lower level node on the finite-difference mesh and
output a node that is on the level above it. This program also takes in a value
for lambda for future use. This program takes in a value for the stepsize along
the x axis. 


#include <iostream>
#include <cmath>

using namespace std; 
//long double forwardAdjustedCentral(long double, long double);

int main()
	long double uN, n, x, t, lambda, tau, h, H, Hminus, uNminus, uNplus, tMplus; 

	cout << "Enter a negative value for lambda: ";
	cin >> lambda;
	cout << "Enter a value for a stepsize tau on the t axis: ";
	cin >> tau;
	cout << "Enter a value for a stepsize h on the x axis: ";
	cin >> H; 
	cout << "Enter a node on the finite difference mesh (x,t): ";
	cin >> x;
	cin >> t; 

	Hminus = H;

	//long double array oldu [3];

	for (n = 0; n <= t; ++n)
		if (x == 1 || x == 0)
			uN = 0;
			uNplus = 0;
			uNminus = 0;

		if (t == 0)
			uN = sin(x);
			uNplus = sin(x);
			uNminus = sin(x);

		tMplus = (((2*lambda*tau)/((H*Hminus*(H+Hminus))))*(H*uNminus-H*uN+Hminus*uNplus))+uN;


	cout << uN << endl << uNplus << endl << uNminus << endl;
	cout << tMplus << endl;

	return 0;

/*long double forwareAdjustedCentral(long double x, long double t);
	Mplus = (((2*lambda*tau)/((H*Hminus*(H+Hminus))))*(H*Nminus-H*N+Hminus*Nplus))+N

	return Mplus;

Thanks so much everyone!

8 Years
Discussion Span
Last Post by MylesDBaker

I think I understand what you are trying to do (though I could be wrong). What problems are you having exactly? Where do you need help?

I see you haven't called your function forwareAdjustedCentral() yet in the main and that you have a for that isn't finished, so are you working on that function or the for in the main?


Well I figured the function wouldn't be necessary. I can just make one statement. Ignore that.

Sorry for late response.


Tis fine, it's your question after all. I'm just trying to help.

Alright, I see where you put the statement. That still leaves that open for loop you need to either fill or eliminate. As far as you cout statements:

cout << uN << endl << uNplus << endl << uNminus 
       << endl << tMplus << endl;

You have a value for those elements and the line above tMplus = ... is fine. I'm just lost as what part of the program you're having trouble with.


Basically what happens is I need the value to be updated. Essentially I have a mesh grid that fills in values from the bottom up (recursion). So in order to know the target value I must know the original values and then build it up.

So, I know my target value, it is given by the user, and I know the parameters of the equation. However, I don't know how to make a computer program that will make all values in between so the target value can be evaluated.

I need to make a loop that updates the loops by one step and uses the values generated and stored in tMplus and then checks that value to see if it the same grade as the t that I am asking for.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.