Problem during the use of 'fzero'

16 ビュー (過去 30 日間)
Andrea Quintavalle
Andrea Quintavalle 2023 年 3 月 27 日
コメント済み: Star Strider 2023 年 3 月 27 日
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 :)

採用された回答

Star Strider
Star Strider 2023 年 3 月 27 日
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.
.
  2 件のコメント
Andrea Quintavalle
Andrea Quintavalle 2023 年 3 月 27 日
Thanks. Now is all clear
Star Strider
Star Strider 2023 年 3 月 27 日
My pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2023 年 3 月 27 日
編集済み: Torsten 2023 年 3 月 27 日
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 件のコメント
Andrea Quintavalle
Andrea Quintavalle 2023 年 3 月 27 日
I'm a bit surprised too ... Tomorrow I'll check the answer at lesson

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOptimization についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by