Hey:

I am kinda new to C++. I created this code to display the edge of chaos. Now my professor wants me to graph it in a coordinate system. I know I could use excel and plot it there but I want to learn how to do it using C++. Does anyone knows how ?

Thanks,

G

Here is the code

#include <fstream>
#include <cmath>  //for fabs, log
using namespace std;

int main(void)
{
ofstream data("lambda.doc");
int T = 10000;	//numbver of iterations
double x = 0.618;	//initial value
double x1;
double eps = 0.0005;
double xeps = x-eps;
double xeps1;
double r = 3.0;
double sum = 0.0;
while(r <= 4.0)
	{
	for(int t=0;t<T;t++)
		{
		x1 = x;
		x = r*x1*(1.0-x1);
		double distance = fabs(x-xeps);
		sum += log(distance/eps);
		xeps = x-eps;
		}
	double lambda = sum/((double)T);
	data << r << "  " << lambda << "\n";
	sum = 0.0;
	r += 0.001;
	} //end while
data.close();
return 0;
}

You won't be able to draw something in a coordinate system using only c++ (console programs).
The only way I know of drawing things is using WinAPI, but it's not as easy to learn in an hour or two.

I've made a Class in wxWidgets tp display values against time. But you could modify it for your needs.

Or you could take the easy way and just use the wxPlotCtrl from wxWidgets :)

You won't be able to draw something in a coordinate system using only c++ (console programs).
The only way I know of drawing things is using WinAPI, but it's not as easy to learn in an hour or two.

My professor recommended learning GNU. Is there any connection between GNU and C++ or am I just jumping to the wrong conclussion here ?

Hey:

I am kinda new to C++. I created this code to display the edge of chaos. Now my professor wants me to graph it in a coordinate system. I know I could use excel and plot it there but I want to learn how to do it using C++. Does anyone knows how ?

Thanks,

G

Here is the code

#include <fstream>
#include <cmath>  //for fabs, log
using namespace std;

int main(void)
{
ofstream data("lambda.doc");
int T = 10000;	//numbver of iterations
double x = 0.618;	//initial value
double x1;
double eps = 0.0005;
double xeps = x-eps;
double xeps1;
double r = 3.0;
double sum = 0.0;
while(r <= 4.0)
	{
	for(int t=0;t<T;t++)
		{
		x1 = x;
		x = r*x1*(1.0-x1);
		double distance = fabs(x-xeps);
		sum += log(distance/eps);
		xeps = x-eps;
		}
	double lambda = sum/((double)T);
	data << r << "  " << lambda << "\n";
	sum = 0.0;
	r += 0.001;
	} //end while
data.close();
return 0;
}

Don't assume people here know what you mean when you say you want to display the edge of chaos. Is this Chaos Theory?

Regarding graphing, what kind of graph are you talking about? Bar graph? Scatter plot with a variety of (x,y) points? Looking at the code, I imagine it's a scatter plot.

[EDIT]
Wow! Some fast posting here. Three posts while I'm editing! Also, is this to be a console program graph or is there a GUI involved? A console graph is somewhat limited and won't look nearly as nice.
[/EDIT]

Don't assume people here know what you mean when you say you want to display the edge of chaos. Is this Chaos Theory?

Regarding graphing, what kind of graph are you talking about? Bar graph? Scatter plot with a variety of (x,y) points? Looking at the code, I imagine it's a scatter plot.

Sorry about that, but yes it is the chaos theory. And yes what I am looking to plot is a scatter plot.

I've made a Class in wxWidgets tp display values against time. But you could modify it for your needs.

Or you could take the easy way and just use the wxPlotCtrl from wxWidgets :)

I guess now I will need to learn wxWidgets. Thanks for the reply I'll go through your code and see what I can do.

I guess now I will need to learn wxWidgets. Thanks for the reply I'll go through your code and see what I can do.

I was joking when I said you needed my code. The only thing you need is a wxPlotCtrl which comes standard with wxWidgets :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.