How to define function to work on any double array

조회 수: 2 (최근 30일)
Zeljko Grljusic
Zeljko Grljusic 2015년 11월 13일
댓글: Zeljko Grljusic 2015년 11월 13일
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