Trouble with global vars

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble with global vars

Tony Mullins

I am getting the following errors when attempting to use the builtin
quad function to integrate a scalar integrand.  If I set the values of
the global variables position, time and sum_limit at the octave
command line and call the function integrand directly I get no such
error.  The .m files follow the error message.

Tony Mullins |internet: [hidden email]
Dept. of Chem. Eng'rg., CPE 5.438 |ma bell : 512-471-1024
University of Texas - Austin |ma FAX  : 512-471-7060
Austin, TX  78712-1062 |Home sweet home: 512-467-0126

> octave integral_soln.m
Octave, version 1.1.1.
Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

error: `tau' undefined near line 14 column 48
error: evaluating expression near line 14, column 48
error: evaluating binary operator `*' near line 14, column 46
error: evaluating argument list element number 0
error: evaluating index expression near line 14, column 31
error: evaluating binary operator `+' near line 14, column 29
error: evaluating assignment expression near line 14, column 16
error: evaluating for command near line 11, column 3
error: called from `integrand' in file `/home/jamull/octave/integrand.m'
error: evaluating argument list element number 0

******************** integral_soln.m ********************
# This file computes the solution to the 1D diffusion equation
# on 0<x<1 using the result from the boundary integral technique.
# It is used in conjunction with the function integrand(tau) and
# calls the Octave function quad("f", a, b, tol, sing) to perform
# the integration.
 
global position time sum_limit;
x = linspace(0,1,100);
time = .025;
lower_limit = 0;
upper_limit = time;
singularity = time;
sum_limit = 100;

for i = 1:length(x)
  position = x(i);
  y(i) = quad(integrand,lower_limit,upper_limit,singularity);
endfor

******************** integrand.m ********************
function int_grand = integrand(tau)
  # This function computes the integrand in the time-convolution
  # integral for the boundary integral solution to the 1-D diffusion
  # equation on 0<x<1.
  # These global variables are necessary because the quadrature
  # routines do not provide for parameter communication.
 
  global position time sum_limit;
 
  exp_series = 1;
  for i = 1:sum_limit
    lambda = (2*i+1) * pi;
    lambda_sq = lambda*lambda;
    exp_series = exp_series + exp(-lambda_sq * tau);
  endfor
 
  den_1 = 4*(time-tau);
  rt_den_1 = sqrt(den_1*pi);
  den_2 = den_1 * den_1 * den_1;
  rt_den_2 = sqrt(den_2*pi);
  x_sq = position*position;
  x_minus_1 = position-1;
  x_minus_1_sq = x_minus_1 * x_minus_1;
  int_grand = position*exp(-x_sq/den_1);
  int_grand = (int_grand - x_minus_1*exp(-x_minus_1_sq/den_1))/rt_den_2;
  int_grand = 2*int_grand + 4*exp_series* (exp(-x_sq/den_1) ...
      + exp(-x_minus_1_sq/den_1))/rt_den_1;
endfunction

Reply | Threaded
Open this post in threaded view
|

Re: Trouble with global vars

John Eaton-4
Tony Mullins <[hidden email]> wrote:

: I am getting the following errors when attempting to use the builtin
: quad function to integrate a scalar integrand.

:   y(i) = quad(integrand,lower_limit,upper_limit,singularity);

Change that to

    y(i) = quad("integrand",lower_limit,upper_limit,singularity);

and I think it should work as you expect.

The reason you get the undefined variable message is that Octave wants
to evaluate the expressions in the argument list to turn them into
values, sees the identifier `integrand', and calls it without any
arguments.

jwe