# C# One Variable Function Example

← All NMath Code Examples

```using System;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Examples.CSharp
{
/// <summary>
/// A .NET example in C# showing how to create and manipulate functions of one variable.
/// </summary>
class OneVariableFunctionExample
{

static double MyFunction( double x )
{
return Math.Sin( x ) + Math.Pow( x, 3 ) / Math.PI;
}

static void Main( string[] args )
{
Console.WriteLine();

// Class OneVariableFunction encapsulates an arbitrary function, and
// works with other numerical classes to approximate integrals and
// derivatives.

// A OneVariableFunction is constructed from an NMathFunctions.DoubleUnaryFunction,
// a function delegate that takes a single double parameter and returns a double.
// For example, to encapsulate MyFunction (see above):
var d = new Func<double, double>( MyFunction );
var f = new OneVariableFunction( d );

// The Evaluate() method evaluates a function at a given x-value.
Console.WriteLine( "f(pi) = {0}\n", f.Evaluate( Math.PI ) );

// Evaluate() also accepts a vector of x-values, and returns a vector of
// y-values, such that y[i] = f( x[i] ). This code evaluates f at 10 points
// between 0 and 1:
var x = new DoubleVector( 10, 0, 1.0 / 10 );

Console.WriteLine( "x... " );
Console.WriteLine( x );
Console.WriteLine();

DoubleVector y = f.Evaluate( x );
Console.WriteLine( "y..." );
Console.WriteLine( y.ToString( "F3" ) );
Console.WriteLine();

// Class OneVariableFunction provides overloads of the arithmetic operators
// (and equivalent named methods) that work with either with two function
// objects, or with a function and a scalar. For example:
OneVariableFunction g = ( f + 1 ) / 2;
g = f * g;
Console.WriteLine( "g(pi) = {0}", g.Evaluate( Math.PI ) );

// Evaluate() accepts another OneVariableFunction, and returns a new
// function encapsulating the composite, g( f(x) ).
OneVariableFunction composite = g.Evaluate( f );
Console.WriteLine( "g( f(pi) ) = {0}\n", composite.Evaluate( Math.PI ) );

// The Integrate() method approximates the integral of a function over a
// given interval.
double integral = f.Integrate( 0, Math.PI / 2 );
Console.WriteLine( "Integral of f from 0 to pi/2 = {0}", integral );

// To perform integration, every OneVariableFunction has an IIntegrator
// object associated with it. NMath Core integration classes such as
// RombergIntegrator and GaussKronrodIntegrator implement the IIntegrator
// interface. The default integrator for a OneVariableFunction is an instance
// of RombergIntegrator, which may be changed using the Integrator property.
f.Integrator = new GaussKronrodIntegrator();
integral = f.Integrate( 0, Math.PI / 2 );
Console.WriteLine( "Integral of f from 0 to pi/2 using Gauss-Kronrod = {0}\n",
integral );

// The Differentiate() method computes the derivative of a function at
// a given x-value.
double derivative = f.Differentiate( Math.PI / 2 );
Console.WriteLine( "Derivative of f at pi/2 = {0}\n", derivative );

// Derivative() returns a new function object encapsulating the first
// derivative of a function.
OneVariableFunction f1stDeriv = f.Derivative();
OneVariableFunction f2ndDeriv = f1stDeriv.Derivative();
Console.WriteLine( "1st derivative of f at pi/2 = {0}",
f1stDeriv.Evaluate( Math.PI / 2 ) );
Console.WriteLine( "2nd derivative of f at pi/2 = {0}",
f2ndDeriv.Evaluate( Math.PI / 2 ) );

Console.WriteLine();
Console.WriteLine( "Press Enter Key" );