Problem during the use of 'fzero'
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrea Quintavalle
am 27 Mär. 2023
Kommentiert: Star Strider
am 27 Mär. 2023
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 :)
0 Kommentare
Akzeptierte Antwort
Star Strider
am 27 Mär. 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)
%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.
.
2 Kommentare
Weitere Antworten (1)
Torsten
am 27 Mär. 2023
Bearbeitet: Torsten
am 27 Mär. 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
Siehe auch
Kategorien
Mehr zu Optimization 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!