The problem with an error when using "fzero" when solving a nonlinear task

Good afternoon! I can't use the code I have. At the beginning I set the parameters I need, an error occurs during the calculation. What could this be related to? In addition, I have a comment near the function f0 (variable appears to change size on every loop iteration).
clc;
clear all;
% Resonsnt frequency of the Gunn diode
warning off MATLAB:fzero:UndeterminedSyntax
disp('Calculation of DG resonance frequency')
global Rdl
VarName = input('Name of a variable parameter, [R0, Rc, Lc, Cc, C0, Cd]: ', 's' );
if(VarName == 'R0')
Xaxis=[VarName,', Ohm'];
elseif (VarName=='Rc')
Xaxis=[VarName,', Ohm'];
elseif (VarName =='Lc')
Xaxis=[VarName,', nH'];
elseif (VarName=='Cc')
Xaxis=[VarName,', PF'];
elseif (VarName=='C0')
Xaxis=[VarName,', PF'];
elseif (VarName=='Cd')
Xaxis=[VarName,', PF'];
else
disp('Error in VarName')
end
ParName=input('Name of parametric variable, [R0, Rc, Lc, Cc, C0, Cd]: ','s');
%St = stremp(Varname, Parname)
while (VarName==ParName)
ParName=input('Input another name of PV','s');
end
LimVar=input('Limits of variable and step [MinVal, [Step], MaxVal] = ');
ParVal=input('Input values of parametric variable [, , ..] = ');
N=101;
Nv=length(LimVar);
Vmin=LimVar(1)
if Nv == 3
N=LimVar(2); г
Vmax=LimVar(3)
else
Vmax=LimVar(2)
end
dVar=(Vmax-Vmin)/(N-1)
VarV=Vmin:dVar:Vmax;
Npar=length(ParVal);
for n=1:Npar
for m=1:N
options=optimset('Display','off','TolX',1e-5);
[x,fval, exitflag, output]=fzero(@ResFrqNew,35,options,ParVal(n),VarV(m),VarName,ParName);
f0(m,n)=x;
Rdom(m,n)=Rdl;
%Nit(m) = output.iterations
end
subplot(1,2,1)
plot (VarV, f0(:, n), '-k', 'LineWidth', 2)
text(VarV(5), f0(5, n), int2str(n), 'FontSize', 12)
xlabel(Xaxis)
ylabel('Fres, GHz')
hold on
subplot(1,2,2)
plot(VarV, Rdom(:, n) , '-k', 'LineWidth', 2)
text(VarV(5), Rdom(5, n) , int2str(n), 'FontSize', 12)
xlabel(Xaxis)
ylabel('Rd, Ohm')
hold on
end
function GdNum = DomRes(Rd, f, PV, W, VarName, ParName)
Lcl = 0.1e-9;
R0 = 8;
C0 = 0.6e-12;
Cd = 0.27e-12;
Rc = 0.5;
if(ParName=='R0')
R0 = PV;
elseif (ParName == 'Rc')
Rc = PV;
elseif (ParName == 'Lc')
Lcl = PV*1e-9;
elseif (ParName == 'Cc')
Cc = PV * 1e-12;
elseif (ParName == 'C0')
C0 = W * 1e-12;
elseif (ParName == 'Cd')
Cd = W*1e-12;
else
disp('Error in defining Parameter name')
end
if(VarName=='R0')
R0 = W;
elseif (VarName=='Rc')
Rc = W;
elseif (VarName=='Lc')
Lcl = W * 1e-9;
elseif (VarName=='Cc')
Cc = W * 1e-12;
elseif (VarName == 'C0')
C0 = W * 1e-12;
elseif (VarName == 'Cd')
Cd = W * 1e-12;
end
Om = 2e9*pi*f;
tau0 = C0*R0;
tauD = Rd*Cd;
F1 = R0/(1 + (Om * tau0)^2);
F2 = Rd/(1 + (Om * tauD)^2);
GdNum = Rc + F1 + F2;
end
function Bd = ResFrq(f, PV, W , VarName, ParName)
% Function calculates Im(Yd) for a given frequency f and parameters Lcl,R0
global Rdl
Rc = 0.5;
Cc = 0.5e-12;
Lc = 0.1e-9;
R0 = 8;s
C0 = 0.6e-12;
Cd = 0.27e-12; %емкость домена
if(ParName == 'R0')
R0=PV;
elseif(ParName=='Rc');
Rc=W;
elseif(ParName=='Lc')
Lcl=PV*1e-9;
elseif (ParName == 'Cc')
Cc = PV * le-12;
elseif (ParName == 'C0')
C0 = W * le-12;
elseif (ParName == 'Cd')
Cd = W * le-12;
else
disp('Error in ParName')
end
if(VarName == 'RO')
R0 = W;
elseif (VarName == 'Rc')
Rc = W ;
elseif (VarName == 'Lc')
Lcl = W * le-9;
elseif (VarName == 'Cc')
Cc = W * le-12;
elseif (VarName == 'CO')
C0 = W * le-12;
elseif (VarName == 'Cd')
Cd = W * le-12;
else
disp('Error in VarName')
end
Om = 2e9*pi*f;
tau0 = C0 * R0;
optionsR = optimset ('Display', 'off', 'TolX', le-5) ;
[x, fval, exitflag, outp] = fzero(@DomResNew, -10, optionsR, f, PV, W ,VarName, ParName);
Rd = x;
Rdl = Rd;
% Niter = outp.iterations
tauD = Cd * Rd;
F1 = tau0 * R0 /(1 + (Om*tau0)^2);
F2 = tau0 * Rd /(1 + (Om * tauD)^2);
Bd = Cc - (Lcl - F1 - F2) / ((RC + F1 + F1)^2 + Om^2 * (Lcl - F1 - F2)^2);
end

5 Comments

Do not use == to compare character vectors -- use strcmp() or strcmpi()
Or use == to compare against a string such as VarName == "Cc" instead of VarName == 'Cc'
Please include executable code in which the error is reproducable.
(I attach a file to the message)
Calculation of DG resonance frequency
Name of a variable parameter, [R0, Rc, Lc, Cc, C0, Cd]: R0
Name of parametric variable, [R0, Rc, Lc, Cc, C0, Cd]: Rc
Limits of variable and step [MinVal, [Step], MaxVal] = [1:1:102]
Input values of parametric variable [, , ..] = [1,2]
Error using fzero>localFirstFcnEval
FZERO cannot continue because user-supplied function_handle ==> ResFrqNew failed with the error below.
Undefined function 'ResFrqNew' for input arguments of type 'double'.
Error in fzero (line 305)
fx = localFirstFcnEval(FunFcn,FunFcnIn,x,varargin{:});
Error in hop (line 45)
[x,fval, exitflag, output]=fzero(@ResFrqNew,35,options,ParVal(n),VarV(m),VarName,ParName);
The attached code cannot be simply executed. We have to reproduce the error.
And I don't see a function with name "ResFrqNew" that "fzero" could call.

Sign in to comment.

 Accepted Answer

[x,fval, exitflag, output]=fzero(@ResFrqNew,35,options,ParVal(n),VarV(m),VarName,ParName);
You ask for ResFrqNew to be invoked
[x, fval, exitflag, outp] = fzero(@DomResNew, -10, optionsR, f, PV, W ,VarName, ParName);
You ask for DomResNew to be invoked
function GdNum = DomRes(Rd, f, PV, W, VarName, ParName)
You define function DomRes not DomResNew
function Bd = ResFrq(f, PV, W , VarName, ParName)
You define function ResFrq, not ResFrqNew
So you are missing definitions of two functions, and you define two functions that you are not using.

11 Comments

but it still didn't solve the problem:(
Walter tried to help you. BUT, you did not actually supply sufficient information to allow a solution. Should we be able to see inside your computer, to know what those functions are?
I think that the code as a whole needs to be improved, I am grateful to everyone who responded to my request for help!
I suspect that you need
[x,fval, exitflag, output]=fzero(@ResFrq,35,options,ParVal(n),VarV(m),VarName,ParName);
and
[x, fval, exitflag, outp] = fzero(@DomRes, -10, optionsR, f, PV, W ,VarName, ParName);
Yes, I renamed these functions, but for some reason the error still remained
It still sends the error
Error in VarName
Error using fzero>localFirstFcnEval
FZERO cannot continue because user-supplied function_handle ==> ResFrq failed with the error below.
Not enough input arguments.
Error in fzero (line 305)
fx = localFirstFcnEval(FunFcn,FunFcnIn,x,varargin{:});
Error in main (line 52)
[x,fval, exitflag, output]=fzero(@ResFrq,35,options,ParVal(n),VarV(m),VarName,ParName);
Okay, I think I've solved this problem, but I have a new one:
Unrecognized function or variable 'f0'.
Error in main (line 64)
plot (VarV, f0(:, n), '-k', 'LineWidth', 2)
You appear to be assigning to f0 inside of
for n=1:Npar
for m=1:N
If Npar is less than 1, or N is less than 1, then the assignment to f0 would not be executed and f0 would be undefined after the loop.
You should consider pre-allocating f0 .
I tried using syms(), but it didn't help
syms f0(m,n);
syms Rdom(m, n);
f0(m,n)=x; %элемент [m,n] равен x
Rdom(m, n)=Rdl;
%Nit(m) = output.iterations
Unrecognized function or variable 'f0'.
Error in main (line 65)
plot (VarV, f0(:, n), '-k', 'LineWidth', 2)

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Asked:

on 21 Aug 2023

Commented:

on 22 Aug 2023

Community Treasure Hunt

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

Start Hunting!