Error of binary operator '/' not implemented for 'scalar' by 'function handle' operations

조회 수: 9 (최근 30일)
So I have been experimenting with function handles and generally trying to vectorise my code to make it run nice and fast. I have been trying to solve the forced KdV equation using a Newton-Raphson scheme and I have the basic code working with a simple example but I am having trouble with the odd error message in the title of this cry for help.
Anyone know what I should do?
I use matlab at work and Octave at home.
  댓글 수: 2
Star Strider
Star Strider 2016년 7월 25일
‘Anyone know what I should do?’
Providing a bit more detail would do for a start. Please copy and paste the entire error message (all the red text) from the Command Window to a Comment here.
Mat Hunt
Mat Hunt 2016년 7월 26일
error: binary operator './' not implemented for 'scalar' by 'function handle' operations error: called from: error: /home/mat/Matlab programs/eta.m at line 7, column 4 error: /home/mat/Matlab programs/weakly_nonlinear.m at line 19, column 2
You also have the whole code as well...

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2016년 7월 26일
The weakly_nonlinear function includes these lines:
h=@eta;
while (sol_error>converge_limit)
F=-feval(h,x,sol,p,F,h,B,choice)';
This calls the eta function with @eta as the fifth input argument. Let's look at a chunk of eta:
function y=eta(x,f,p,F,h,B,choice)
N=length(x);
y=zeros(1,N+1);
rho=1;
g=9.81;
a_1=1-f(N+1);
a_2=0.75./h;
The last line is the one throwing the error. When computing a_2 you're dividing by @eta. That doesn't look right. Usually in this type of problem a variable named h represents the spacing between elements in x or something similar. If that's the case, you probably want to use diff(x) or something along those lines.
  댓글 수: 1
Mat Hunt
Mat Hunt 2016년 7월 26일
I see the error now. Yes, I understand where I have made the error.
I just have a few more bugs to correct now.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2016년 7월 25일
You can't add, subtract, multiply, divide, etc. function handles.
f1 = @sin;
g1 = @(x) x.^2;
thisWillNOTWork1 = f1./2;
thisWillNOTWork2 = @(x) f1./2;
You can add, subtract, multiply, divide, etc. the values returned by evaluating function handles.
f1 = @sin;
g1 = @(x) x.^2;
thisWillWork1 = @(x) f1(x)./2;
thisWillWork2 = @(x, y) f1(x)./g1(y); % Assuming the sizes match
thisWillWork3 = @(x) f1(g1(x));
thisWillWork4 = @(x) cos(f1(x)) + exp(g1(x));
  댓글 수: 7
Guillaume
Guillaume 2016년 7월 26일
편집: Guillaume 2016년 7월 26일
The one in Steven's answer:
thisWillWork3 = @(x) f1(g1(x));
However, as per Steven's 2nd answer and mine, the problem is nothing to do with function handles per say, but your overloading of the variable h to represent two different things when it can of course only represent one.
Mat Hunt
Mat Hunt 2016년 7월 26일
Unfortunately that doesn't work.
Can I ignore the function handle completely and just use it as a function?

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2016년 7월 26일
편집: Guillaume 2016년 7월 26일
It's unfortunately difficult to give you a way forward other than: find the bugs in your code.
Probably, the first bug is your usage of global variables, which is a discouraged programming practice as that makes it very difficult to follow the flow of the program.
Briefly looking at your code, we see that in weakly_nonlinear.m you define a global h which you set to 1. Later on in the same code, you then change that value to a function handle. Because it's a global, it's unknown if that 1 value has ever been used. In any case, that h value is passed twice as an argument to feval: as the function to execute and as an argument to the function to execute.
If you're trying to do some sort of recursion that's not the way to go about it, but I suspect that you simply forgot that you've used the variable h for something else and the second h was meant to be that 1 value that's been replaced by @eta
Morale of the story:
  • don't use globals
  • learn to debug and step through your code
  • use descriptive variable names (words are good) so that you don't end up reusing the same name for two different things.

카테고리

Help CenterFile Exchange에서 Function Handles에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by