问题
I wrote this code in octave:
syms z;
f=z-2;
fsolve("f",0.)
Then this gives the error
@f: no function and no method found.
Also using fsolve(@f,0) gives the same error
When I write code as:
syms z;
f=z-2;
fsolve(f,0.)
Then this gives the error
ind2sub: subscript indices must be either positive integers less than 2^31 or logicals.
Please explain to me how to actually use fsolve.
回答1:
% syms z; % Not needed, actually slows down the code
f=@(z)(z-2);
fsolve(f,0.)
You're missing the @ symbol, which is a function handle. This tells Octave that f is not a variable, but actually is a(n anonymous) function, in this case of z, which is the first argument.
You'll probably want to have z to be a regular variable, because making it symbolic turns MATLAB from a speeding race car to a drudging farm vehicle. Unless there's a specific reason to have z symbolic (I cant think of any in case of usage with fsolve)' it's best to avoid symbolic maths.
来源:https://stackoverflow.com/questions/37890794/fsolve-error-no-function-or-method-found