Getting rid of fzero errors

3 次查看(过去 30 天)
Heitor Silva
Heitor Silva 2021-4-22
Hey all,
I am a beginner user of MATLAB.
I have a code for solving an ODE whose solution gives the Townes soliton profile.
The code is the following:
function[]=shooting()
% Calculate R(r) in d dimensions using shooting
% Output is r_sol=[r0:dr:rf], R_sol=R(r_sol) and dR=R'(r_sol).
global R0 Dim r_inf dr_Taylor tol sigma options
Dim =2; sigma =1; r_int =15; dr=0.001; tol=1e-10;
dr_Taylor=1e-6; %when to switch from Taylor to direct formula
R0_guess = 2.2; %initial guess for R(0)
options = odeset('RelTol',tol,'AbsTol',tol);
R0 = fzero(@shoot_for_R0,R0_guess); %Find R(0) using shooting
% X is [R,R',I_R2 I_dR2 I_2sigma_2], where I_R2=int R^2 r^(d-1),
% I_dR2=int R'^2 r^(d-1), I_2sigma_2=int R^(2 sigma+2) r^(d-1)
X0 = [R0 0 0 0 0]; %X0=X(0)
[r_sol,X]=ode45(@R_profile,[0:dr:r_inf], X0, options);
R_sol = X(:,1); dR_sol=X(:,2);
plot(r_sol,R_sol,r_sol,dR_sol);legend('R','dR');xlabel('r'); shg
% Verifications - check Pohozaev identities
I_R2 = X(end, 3); I_dR2 = X(end,4); I_2sigFZERO cannot continue because user-supplied function_handle ==> shoot_for_R0 failed with the error belowma_2 = X(end,5);
Pohozaev1 = IR2-(2-sigma*(Dim-2))/2/(sigma+1)*I_2sigma_2
Pohozaev2 = I_dR2-sigma*Dim/2/(sigma+1)*I_2sigma_2
end
%
function R_inf = shoot_for_R0(R0) %R(r_inf) as function of R0
global r_inf options
X0 = [R0 0 0 0 0];
[r_sol,X]=ode45(@R_profile, [0 r_inf], X0, options);
R_inf = X(end,1);
end
%
function dX = R_profile(r,X) % Evaluate R and associated integrals
global dr_Taylor R0 Dim sigma
R = X(1); dR = X(2); dx = zeros(5,1); dX(1) = dR;
R_2sigma = abs(R)^(2*sigma);
if (r >= dr_Taylor)
dX(2) = R-R_2sigma*R-(Dim-1)*dR/r; %R''(r)
else %Near the origin, approximate R'/r with R''(0)
if isempty(R0)==1
R0=0; %set arbitrary value to R0 before it is being used
end
ddR0 = R0*(1-abs(R0)^(2*sigma))/Dim; %R''(0)
dX(2) = R-R_2sigma*R-(Dim-1)*ddR0; %R''(r)
end
dX(3) = r^(Dim-1)*R^2; %for I_R2
dX(4) = r^(Dim-1)*dR^2; %for I_dR2
dX(5) = r^(Dim-1)*R_2sigma*R^2; %for I_2sigma_2
end
I have to confess that I still do not understand yet much of the code but I have tried to run it and the following errors are being reported:
Error using fzero>localFirstFcnEval (line 729)
FZERO cannot continue because user-supplied function_handle ==> shoot_for_R0 failed with the error below.
When the first argument to ode45 is a function handle, the tspan argument must have at least two elements.
Error in fzero (line 286)
fx = localFirstFcnEval(FunFcn,FunFcnIn,x,varargin{:});
Error in townes (line 9)
R0 = fzero(@shoot_for_R0,R0_guess); %Find R(0) using shooting
Can someone please lend me a hand and tell me how to get rid of these errors?

回答(1 个)

Walter Roberson
Walter Roberson 2021-4-22
You never assign to r_inf and it is a global variable so it is initialized to empty. You use [0 r_inf] but r_inf is empty so that is the same as just 0
The moral of the story is "Do not use global variables if you can avoid it!"
https://www.mathworks.com/help/matlab/math/parameterizing-functions.html

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by