Error in ODE arguments in line 87

조회 수: 2 (최근 30일)
Mariana
Mariana 2017년 7월 22일
댓글: Mariana 2019년 11월 1일
Hi everyone!
I'm trying to reed a .m funtion (with global variables) with the following script:
function [dY]=fun2(t,yfo)
for i=1:ng
for j=1:ng
if i~=j
aux2_pe=C_fal(i,j)*sin(yfo(i)-yfo(j))+D_fal(i,j)*cos(yfo(i)-yfo(j));
end
end
Pe_fal(i,1)=vg(i)^2*real(y_fal(i,i))+aux2_pe;
dy(i+ng)=(Pm(i)-Pe_fal(i,1))/M(i);
dy(i)=yfo(i+ng);
end
dY=[dy(1); dy(2); dy(3); dy(4); dy(5); dy(6)]
end
using the following codes for ODE23s:
yfo=[0.4677 0.4625 0.1171 0 0 0];
tspan=[0:0.002:0.198];
[tf,yf]=ode45('fun2',tspan,yfo);
and obtaining the following answer:
Undefined function or variable 'ng'.
Error in fun2 (line 5)for i=1:ng
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Untitled2 (line 246) [tf,yf]=ode45('fun2',tspan,yfo);

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 22일
Global variables are only accessible in the workspaces where they are specifically declared with global.
Using global is not a good idea for this purpose. See http://www.mathworks.com/help/matlab/math/parameterizing-functions.html

추가 답변 (0개)

카테고리

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