Integration problem

John W. Eaton jwe at octave.org
Thu Jan 8 16:34:19 CST 2009


On  8-Jan-2009, James Sherman Jr. wrote:

| While I don't have octave available currently, I believe your problem is
| that the quad function requires the first input to be a function of one
| input.  So, for a single u value it would look like:
| 
| >function y=int(x)
| >y=sin(10.*x);
| >endfunction;
| 
| Then the quad line should work.  Now if you want to dynamically change the u
| value, then you might do something like:
| 
| index = 1;
| results = zeros(size([-10:0.1:10]));
| 
| for u = [-10:0.1:10]
| 
|    % creates the function as a string where the first input to int is the
| current value of u
|    function_string = sprintf('int(%d, x)', u);
| 
|    % creates an inline function that only has one input 'x', because we want
| u to be constant.
|    int_inline = inline(function_string, 'x');
| 
|    results(index) = quad('int_inline',0,2.*pi);
| 
|    index = index + 1;
| end
| 
| There may be a better way to do this, but I think this should work.

Using an anonymous function is a bit simpler:

  rng = -10:0.1:10;
  results = zeros (size (rng));
  idx = 1;
  for u = rng
    results(idx++) = quad (@(x) sin(u*x), 0, 2*pi);
  endfor

jwe


More information about the Help-octave mailing list