Error of binary operator '/' not implemented for 'scalar' by 'function handle' operations
Mostra commenti meno recenti
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
Star Strider
il 25 Lug 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.
Mat Hunt
il 26 Lug 2016
Risposta accettata
Più risposte (2)
Steven Lord
il 25 Lug 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 Commenti
Mat Hunt
il 26 Lug 2016
Stephen23
il 26 Lug 2016
@Mat Hunt: that is good, because Steven Lord's answer does not mention symbolic functions anywhere. The examples are all numeric functions.
Mat Hunt
il 26 Lug 2016
Guillaume
il 26 Lug 2016
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.
Mat Hunt
il 26 Lug 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.
Mat Hunt
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!