using System;
namespace Science.Physics.GeneralPhysics
{
///
/// NewtonEquation is written with Force, Mass, and Acceleration.
///
public class NewtonEquation
{
public NewtonEquation()
{
}
private TotalForce[] f;
private Mass[] m;
private Acceleration[] a;
private ConstraintFunctionToBeZero[] cf;
private int numcons = 0;
public NewtonEquation(TotalForce[] ff, Mass[] mm,
Acceleration[] aa)
{
f = ff;
m = mm;
a = aa;
}
public delegate double ConstraintFunctionToBeZero(TotalForce[] f, Mass[] m, Acceleration[] a);
public void Constraint(ConstraintFunctionToBeZero[] func)
{
cf = func;
numcons = func.Length;
}
public void Solve()
{
int ndim = 0;
for(int k = 0; k < m.Length; k++) if(m[k].VariableQ) ndim += 1;
for(int k = 0; k < a.Length; k++)
{
if(a[k].XVariableQ) ndim += 1;
if(a[k].YVariableQ) ndim += 1;
if(a[k].ZVariableQ) ndim += 1;
}
for(int k = 0; k < f.Length; k++)
{
for(int kk = 0; kk < f[k].NumberOfDecomposedForces; kk++)
{
if(f[k].DecomposedForce[kk].XVariableQ) ndim += 1;
if(f[k].DecomposedForce[kk].YVariableQ) ndim += 1;
if(f[k].DecomposedForce[kk].ZVariableQ) ndim += 1;
}
}
Calculus.Function fun
= new Calculus.Function(Func);
double[] c = new double[ndim];
double[] ans = Calculus.MinimumOfFunction(fun, c, 100.0);
}
private double Func(double[] x)
{
int i = 0;
for(int k = 0; k < m.Length; k++) if(m[k].VariableQ) this.m[k].kg = x[i++];
for(int k = 0; k < a.Length; k++)
{
if(a[k].XVariableQ) this.a[k].X = x[i++];
if(a[k].YVariableQ) this.a[k].Y = x[i++];
if(a[k].ZVariableQ) this.a[k].Z = x[i++];
}
for(int k = 0; k < f.Length; k++)
{
for(int kk = 0; kk < f[k].NumberOfDecomposedForces; kk++)
{
if(f[k].DecomposedForce[kk].XVariableQ)
this.f[k].DecomposedForce[kk].X = x[i++];
if(f[k].DecomposedForce[kk].YVariableQ)
this.f[k].DecomposedForce[kk].Y = x[i++];
if(f[k].DecomposedForce[kk].ZVariableQ)
this.f[k].DecomposedForce[kk].Z = x[i++];
}
}
double sum = 0.0;
for(int k = 0; k < numcons; k++)
{
sum += cf[k](this.f,this.m,this.a)*cf[k](this.f,this.m,this.a);
}
return BasicConstraint()+sum;
}
private double BasicConstraint()
{
double zero = 0.0;
for(int k = 0; k < this.f.Length; k++)
zero += (this.f[k].X - this.m[k].kg*this.a[k].X)*
(this.f[k].X - this.m[k].kg*this.a[k].X)
+(this.f[k].Y - this.m[k].kg*this.a[k].Y)*
(this.f[k].Y - this.m[k].kg*this.a[k].Y)
+(this.f[k].Z - this.m[k].kg*this.a[k].Z)*
(this.f[k].Z - this.m[k].kg*this.a[k].Z);
return zero;
}
}
}