How to define function to work on any double array

2 views (last 30 days)
Dear all,
I'm writing a code to solve a system of PDEs with finite differencing. I'm using a two dimensional grid, with one spatial and one time dimension and I'm using arrays with two indices for my physical variables (e.g. R(i,n), where i is the spatial and n is the time index). Some of the arrays have to be averaged to give a value at a half grid point (e.g. (R(i+1,n)+R(i,n))/2 ). Since I have to do this for many functions, I would like to define the averaging function such that it accepts any array and returns me the averaged value. I tried using a placeholder variable "a" for the arrays and tested it for a one dimensional array like this:
% test function
function testreturn = test_average(n,a)
t_n = (a(n+2)+a(n+1))/2.;
testreturn = t_n;
end
But when I replace "a" by an array, I'm getting the error: "Undefined function 'test_average' for input arguments of type 'double'." I would appreciate any tips :) I guess the solution is quite easy, but it is my first time programming with Matlab and I'm still getting used to it.
Thank you all
  1 Comment
Zeljko Grljusic
Zeljko Grljusic on 13 Nov 2015
I meant replacing "a" in the function call like this:
test_average(1,timesteps)
And you were right: it was becaused the function wasn't called test_average.m. Thanks for your help.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 13 Nov 2015
The error message you are getting is typical when MATLAB can't find the function. Check your path to ensure that the file test_average.m is where you can see it from the location you are running your program.
  1 Comment
Zeljko Grljusic
Zeljko Grljusic on 13 Nov 2015
I didn't call my file test_average.m but simply test.m. I changed it and now it works.
Thanks a lot :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!