The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Simulation issue
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

ase724

USA
9 Posts

Posted - 10/27/2013 :  01:46:38 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro9.0.0, SR2.
Operating System: Windows 7

Hi,
I wrote a simulation code for a function n1 as a function of time. I found that Origin doesn't compute all the outputs (n1). The Origin C code below only computes n1(0)~n1(10) (t = 0~10 minutes), and gives 0 for the remaining results (n1(11)~n1(99)). I use Mathematica to compute the same function with the same input parameters. Mathematica gives the same value of n1(0)~n1(10) as Origin C, and Mathematica also computes other values (n1(11)~n1(99)) which are not zero. What is the reason causing Origin C to return 0 for some results?

Origin C code:

void NofT()
{
double a = 2.18;
double b = 0.656;
double airr = 9.982E-7;
double Ip = 30;
int time = 100; // min., total time.
int tcount = 100;
double dt = time/tcount;

double ma = b+(a+airr)*Ip;
double mc = b+(a-airr)*Ip;
double mb = sqrt((b+(a+airr)*Ip)^2-4*b*airr*Ip);

vector vt;
vt = TimeAxis(tcount, dt);
vector n1;
n1.SetSize(tcount);

for(int tt = 0; tt < tcount; tt++)
{
n1[tt] = ((mb-mc)*(mb+mc)*(exp(mb*vt[tt])-1)*exp(-0.5*(ma+mb)*vt[tt]))/(2*mb*(ma-mc));
printf("n1[%d]=%f\n",tt ,n1[tt]);
}
}

vector TimeAxis(int tcount, double dt)
{
vector vt;
vt.SetSize(tcount);
for (int tt = 0; tt < tcount; tt++)
{
vt[tt] = tt*dt; // t axis, min.
}
return (vt);
}

rlewis

Canada
253 Posts

Posted - 10/27/2013 :  03:32:47 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I think one of the exponential terms in your function overflows ...
Try recoding you function ....
Go to Top of Page

ase724

USA
9 Posts

Posted - 10/27/2013 :  1:51:26 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you, rlewis! The exponential terms are indeed overflowing which causes the zero values.
Go to Top of Page

rlewis

Canada
253 Posts

Posted - 10/27/2013 :  6:21:37 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The following is a recoded version of your function to avoid the exponential oveflow problem ...

void NofT()
{
	double a = 2.18;
	double b = 0.656;
	double airr = 9.982E-7;
	double Ip = 30;
	int time = 100; // min., total time.
	int tcount = 100;
	double dt = time/tcount;

	double ma = b+(a+airr)*Ip;
	double mc = b+(a-airr)*Ip;
	double mb = sqrt((b+(a+airr)*Ip)^2.0 - (4.0*b*airr*Ip));
	
	double Phi=((mb-mc)*(mb+mc))/(2.0*mb*(ma-mc));
	double lnPhi=ln(Phi);
	
	vector<double> vt;
	vt.Data(0,((tcount-1)*dt),dt);

	vector<double> n1;
	n1.SetSize(vt.GetSize());

	double 	Lna, Lnb;
	for(int tt = 0; tt < tcount; tt++)
	{
		if((mb*vt[tt] < 665.0) || ((ma+mb)*vt[tt] < 665.0))
		{
			n1[tt] = Phi*((exp(mb*vt[tt])-1.0)*exp(-0.5*(ma+mb)*vt[tt]));
			printf("n1[%d]=%f\n",tt ,n1[tt]);
		}
		else
		{
			Lna=mb*vt[tt];
			Lnb=-0.5*(ma+mb)*vt[tt];
			n1[tt]=exp(lnPhi+Lna+Lnb);
			printf("n1[%d]=%f\n",tt ,n1[tt]);
		}
	}
}
Go to Top of Page

ase724

USA
9 Posts

Posted - 11/06/2013 :  5:55:37 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks again, rlewis! This is very helpful for me!
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000