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

8 vues (au cours des 30 derniers jours)
Augustine Nashwan Aziz Al-Zebary
Commenté : Steven Lord le 18 Mai 2021
Hello!
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;
end
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
variable.
> 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);

Réponses (1)

James Tursa
James Tursa le 18 Mai 2021
Modifié(e) : James Tursa le 18 Mai 2021
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 commentaires
Augustine Nashwan Aziz Al-Zebary
Any suggestions on how i can do that? I'm pretty new to matlab :)
Steven Lord
Steven Lord le 18 Mai 2021
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by