Why can I not pass a maths function into another function

I have a function 'heun' which I am trying to pass a mathematical function into, I have managed to do this before but can not do it consistently.
fb = @(t,y)(5*y(t));
y = heun( fb, 0.1/10, 10, 0, 1 );
this is my error message:
Function 'subsindex' is not defined for values of class 'function_handle'
Please help

5 件のコメント

Stephen23
Stephen23 2018 年 1 月 30 日
@Campbell Gray: please show us the complete error message. This means all of the red text.
Adam
Adam 2018 年 1 月 30 日
編集済み: Adam 2018 年 1 月 30 日
Which line is actually causing the problem? You should be able to use the debugger to isolate it better.
The only function handle I see in your code is 'fb' and judging from your reply to Walter (which should be included in the original question, by the way, since it is fundamental to the problem), this becomes f in your heun function, which is then given what appear to always be two scalars as arguments. This in itself seems odd given your definition of fb:
fb = @(t,y)(5*y(t));
y(t) is rarely going to make sense when y and t are scalars (only when t is 1 when it is a degenerate statement anyway).
I wouldn't necessarily have expected all this to yield the error message you show, but I may be losing some of the subtleties in exactly what point will throw the first error.
Campbell Gray
Campbell Gray 2018 年 1 月 30 日
This is the code:
fa = @(t,y)(sin(t));
heun(1) = heun( fa, pi/10, 10, 0, 0 );
The error message I am getting:
Function 'subsindex' is not defined for values of class 'function_handle'.
Error in Tables (line 37)
heun(1) = heun( fa, pi/10, 10, 0, 0 );
Adam
Adam 2018 年 1 月 30 日
編集済み: Adam 2018 年 1 月 30 日
This is fundamentally different code to what you showed in the question. You are trying to use heun as both the function name and the name of the variable to which you are trying to assign the result. This clearly won't work. Use different names for variables and for functions!!
Walter Roberson
Walter Roberson 2018 年 1 月 30 日
The above would work the first time, because the first time through the variable heun has not been assigned to so heun refers to the function. The second time through, though, heun is now a scalar value rather than a reference to the function.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 30 日

1 投票

At that point in your code, heun is a variable (other than a function handle) rather than a function.

2 件のコメント

Campbell Gray
Campbell Gray 2018 年 1 月 30 日
Sorry I don't quite understand.
I have defined the function in another script as such:
function y = heun( f, h, n, t0, y0 )
t(1) = t0;
z(1) = y0;
for i = 1:1:n
k1 = h*f(t(i),z(i));
k2 = h*f(t(i)+(h/3),z(i)+(k1/3));
k3 = h*f(t(i)+((2*h)/3),z(i)+((2*k2)/3));
z(i+1) = z(i) + ((k1/4) + ((3*k3)/4));
t(i+1) = t(i) + h;
i = i+1;
end
y = z(n+1);
end
Walter Roberson
Walter Roberson 2018 年 1 月 30 日
The error says that you tried to use a function handle as a subscript. That would happen if you tried to do something like this:
heun = 17;
fb = @(t,y)(5*y(t));
y = heun( fb, 0.1/10, 10, 0, 1 );
In this code, when the third line is executed, heun refers to the array named heun, rather than to the available function named heun, so this would be a request to subscript the array named heun with first index fb (a function handle), second index 0.01, third index 10, fourth index 0, fifth index 1.
The priority for resolving names always goes to local variables first. Otherwise code such as
sum = 0;
for K = 1 : 5
sum(K+1) = sum(K) + K;
end
would not be able to work, if MATLAB gave priority to function calls over local variables.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by