Operator '-' is not supported for operands of type 'function_handle'.

조회 수: 56(최근 30일)
Augustine Nashwan Aziz Al-Zebary
댓글: Steven Lord 2021년 5월 18일
Can someone please help me understand this error? Why do i get it? I dont get what im doing wrong here. Here's my code
global R C u
R = 10e3;
C = 3.3e-6;
tau = R*C;
u = @(t) 10*(t>tau);
ode45(@odeFun, [0, 10], 0);
function A3 = odeFun(t, u)
global R C u
A3 = R * C * + u(t) - u;
Here are all the errors that i get
Warning: The value of local variables may have
been changed to match the globals. Future
versions of MATLAB will require that you declare
a variable to be global before you use that
> In A3>odeFun (line 15)
In odearguments (line 90)
In ode45 (line 115)
In A3 (line 8)
Operator '-' is not supported for operands of
type 'function_handle'.
Error in A3>odeFun (line 17)
A3 = R * C * + u(t) - u;
Error in odearguments (line 90)
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 A3 (line 8)
ode45(@odeFun, [0, 10], 0);


James Tursa
James Tursa 2021년 5월 18일
편집: James Tursa 2021년 5월 18일
You are using u as both an input argument and a global function handle
function A3 = odeFun(t, u) <-- input argument
global R C u <-- function handle
Change your notation to be consistent. E.g., make the input argument y.
That being said, this function handle has a discontinuity that ode45( ) isn't going to like. You will probably want to split this up into two separate integrations, one up to tau and one after tau.
  댓글 수: 2
Steven Lord
Steven Lord 2021년 5월 18일
Generally you should avoid using global variables unless they're absolutely necessary. In this case they're not: see the "Parameterizing Functions" link in the description of the odefun input argument on the documentation page for the ode45 function for alternate approaches to get those additional parameters into your ODE function.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by