Solve nonlinear function with Origin C

this is the code of Origin C solving nonlinear function using Newton-method.
also it shows how to use worksheet, datarange, vector…

#include
void SolveV0()
{
Worksheet wks=Project.ActiveLayer();
double V, H, h0, V0, f;
double beta1=0.562, beta2=0.99;
double beta, dfdV0;

beta = (beta1+beta2)^(beta1+beta2)/beta1^beta1/beta2^beta2;
if(!wks)
{
out_str(“error”);
return;
}

DataRange drV, drH, drh0, drV0;
Column col(wks,0);
int nrows=col.GetNumRows();

drV.Add(“”,wks,0,4,nrows,4);
drH.Add(“”,wks,0,5,nrows,5);
drh0.Add(“”,wks,0,10,nrows,10);
drV0.Add(“”,wks,0,11,nrows,11);

vector vV, vH, vh0, vV0;
drV.GetData(&vV,0);
drH.GetData(&vH,0);
drh0.GetData(&vh0,0);
drV.GetData(&vV0,0); //dummy

for(int i=0;i<nrows;i++)
{
V=vV[i];
H=vH[i];
H=abs(H);
// V=abs(V);
h0=vh0[i];
V0=sqrt(H^2+V^2);
if (H==0.0)
vV0[i]=V0;
else
{
f=H/(V0*h0) – beta*(V/V0)^beta1*(1-V/V0)^beta2;
while(abs(f)>=0.0000001)
{
dfdV0 = -H/h0/V0^2 + beta*beta1*V^beta1/V0^(beta1+1)*(1-V/V0)^beta2 – beta*beta2*(V/V0)^beta1*(1-V/V0)^(beta2-1)*V/V0^2;
V0 = V0 – f/dfdV0;
f=H/(V0*h0) – beta*(V/V0)^beta1*(1-V/V0)^beta2;
}
}
}
drV0.SetColumnData(vV0,FALSE,0);
}

Advertisements

About yhtian

I am an academic researcher working on Offshore Geotechnical Engineering. My blog aims to write down some work related trivial things and tricks about software, programming. It is basically a memo for me to back up some thoughts and small details. But I am more than happy if someone would visit and discuss.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s