Error of binary operator '/' not implemented for 'scalar' by 'function handle' operations
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mat Hunt
am 25 Jul. 2016
Kommentiert: Mat Hunt
am 26 Jul. 2016
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 Kommentare
Star Strider
am 25 Jul. 2016
‘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.
Akzeptierte Antwort
Steven Lord
am 26 Jul. 2016
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.
Weitere Antworten (2)
Steven Lord
am 25 Jul. 2016
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 Kommentare
Guillaume
am 26 Jul. 2016
Bearbeitet: Guillaume
am 26 Jul. 2016
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.
Guillaume
am 26 Jul. 2016
Bearbeitet: Guillaume
am 26 Jul. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Function Handles finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!