How to define function to work on any double array
Show older comments
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
on 13 Nov 2015
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!