How to define function to work on any double array

2 次查看(过去 30 天)
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 个评论
Zeljko Grljusic
Zeljko Grljusic 2015-11-13
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.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2015-11-13
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 个评论
Zeljko Grljusic
Zeljko Grljusic 2015-11-13
I didn't call my file test_average.m but simply test.m. I changed it and now it works.
Thanks a lot :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by