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

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 Commenti

‘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.
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...

Accedi per commentare.

 Risposta accettata

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 Commento

I see the error now. Yes, I understand where I have made the error.
I just have a few more bugs to correct now.

Accedi per commentare.

Più risposte (2)

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 Commenti

Unfortunately my function isn't symbolic, it's a numerically defined one.
@Mat Hunt: that is good, because Steven Lord's answer does not mention symbolic functions anywhere. The examples are all numeric functions.
Unfortunately that doesn't answer my question. I may have to avoid using function handles if I can't define functions in the manner I have.
Steven's answer does answer your question very well actually. Your code is attempting to do something that makes no sense mathematically, probably because of a bug in your code, and matlab rightly rejects it.
You can compose functions and functions handles with no issues (if that's what you're attempting to do), you just have to use the proper syntax.
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.
Unfortunately that doesn't work.
Can I ignore the function handle completely and just use it as a function?

Accedi per commentare.

Guillaume
Guillaume il 26 Lug 2016
Modificato: Guillaume il 26 Lug 2016
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.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by