Problem during the use of 'fzero'

14 views (last 30 days)
During the exxecution of my codes, the compiler returns this error which I can't understand completely ... Some help ?
Error:
Error using fzero>localFirstFcnEval
FZERO cannot continue because user-supplied
function_handle ==> f failed with the error below.
Not enough input arguments.
Error in fzero (line 305)
fx = localFirstFcnEval(FunFcn,FunFcnIn,x,varargin{:});
Error in AnalisiVibrazioniAuto (line 19)
point = fzero(fun,x0);
Here below the code:
clc
clear
close all
%% Global variables declaration
global c
c = 1.4*10e4;
global m
m = 1.2*10e3;
global k
k = 1.25*10e6;
%% Initial condition
x0 = 0.3;
%% Physical parameters
n = c/(2*m);
p = sqrt(k/m-(c^2)/(4*m^2));
%% Function to examinate
t = 0:0.1:1;
fun = @f;
point = fzero(fun,x0);
%figure
%plot(t,fun,'r');
%plot(t,point,'g');
%hold on
-------------------------------------
function fun = f(x,n,p,x0)
fun = (exp(-n.*x)).*((x0.*cos(p.*x))+(x0.*n/p.*sin(p.*x)));
end
Thanks :)

Accepted Answer

Star Strider
Star Strider on 27 Mar 2023
I am not certain what you want to do.
I suspect that you want to loop over varying values of ‘t’ however ‘t’ never appears as an argument to ‘f’ or as far as I can determine, an other calculation.
The ‘f’ funciton has to have all its arguments supplied, even if you are only solving for one of them.
This illustrates that reference —
%% Global variables declaration
% global c
c = 1.4*10e4;
% global m
m = 1.2*10e3;
% global k
k = 1.25*10e6;
%% Initial condition
x0 = 0.3;
%% Physical parameters
n = c/(2*m);
p = sqrt(k/m-(c^2)/(4*m^2));
%% Function to examinate
t = 0:0.1:1;
fun = @(x)f(x,n,p,x0);
point = fzero(fun,x0)
point = 0.2531
%figure
%plot(t,fun,'r');
%plot(t,point,'g');
%hold on
% -------------------------------------
function fun = f(x,n,p,x0)
fun = (exp(-n.*x)).*((x0.*cos(p.*x))+(x0.*n/p.*sin(p.*x)));
end
Also, using global variables is considered to be bad programmiung practice. They are not used anywhere here, anyway. Instead, pass them as arguments as appropriate.
.

More Answers (1)

Torsten
Torsten on 27 Mar 2023
Edited: Torsten on 27 Mar 2023
I'm surprised the initial value x0 is part of your function definition. Are you sure this is correct ?
%% Global variables declaration
global c
c = 1.4*10e4;
global m
m = 1.2*10e3;
global k
k = 1.25*10e6;
%% Initial condition
x0 = 0.3;
%% Physical parameters
n = c/(2*m);
p = sqrt(k/m-(c^2)/(4*m^2));
%% Function to examinate
t = 0:0.01:1;
fun = @(x)f(x,n,p,x0);
point = fzero(fun,x0);
hold on
plot(t,fun(t),'r');
plot(point,fun(point),'o');
hold off
grid on
%-------------------------------------
function fun = f(x,n,p,x0)
fun = (exp(-n.*x)).*((x0.*cos(p.*x))+(x0.*n/p.*sin(p.*x)));
end
  1 Comment
Andrea Quintavalle
Andrea Quintavalle on 27 Mar 2023
I'm a bit surprised too ... Tomorrow I'll check the answer at lesson

Sign in to comment.

Categories

Find more on Optimization in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!