The problem with an error when using "fzero" when solving a nonlinear task
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 21 Ago 2023
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'
Dyuman Joshi
il 21 Ago 2023
You could also use switch - switch, case, otherwise
Torsten
il 21 Ago 2023
Please include executable code in which the error is reproducable.
Albert
il 21 Ago 2023
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Solver Outputs and Iterative Display in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!