28.10.08

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 {
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);
}

没有评论: