help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Numerical Differentiation and Integration of Array Data


From: c.
Subject: Re: Numerical Differentiation and Integration of Array Data
Date: Tue, 6 Dec 2011 08:02:47 +0100

On 6 Dec 2011, at 00:11, syberraith wrote:

> Hey C. 
> 
> I got this mostly working, the anonymous function.
> 
> The only problem fully implementing you scheme is getting the anonymous
> function to pass the four parameter values to the raw_vel and del_the
> functions.
> 
> I had to resort to using some global variables.  Here's what I got working.


avoiding global variables was the reason why I used function handles in the 
first place.
using function handles you can construct a function whith just one input 
parameter 
from one with multiple parameters like this:

## fun is a function that takes two inputs
function f = fun (x, p)
 f = x + p;
endfunction

## fix the value of p
p = 3;

## specialized_fun is a function of x only
specialized_fun = @(x) fun (x, p)  

## fix a different value of p
p = 2;

## specialized_fun_2 is also a function of x 
## but is different from specialized_fun
specialized_fun_2 = @(x) fun (x, p)  


The rough C++ equivalent of this is something like

#include <iostream>
class fun
{
private:
 const double p;
public:
 fun (const double pin) : p (pin) {};
 double operator() (const double x) {return (x+p);};
};

int main (void)
{
 fun specialized_fun (3.0);
 std::cout << specialized_fun (1.0) << std::endl;

 fun specialized_fun_2 (2.0);
 std::cout << specialized_fun_2 (1.0) << std::endl;
}

c.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]