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
2018년 1월 30일
@Campbell Gray: please show us the complete error message. This means all of the red text.
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
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
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!