abs() in anonymous function

Hi there
I solved the previous problem by introducing an anonymous function
f2 = @(t,J) lambda * tau_0 * (0.5*(tanh(J-theta_i)-tanh(-theta_i))) * (0.25*(1-(tanh(-theta_0))^2)) * [ ((1-J)^mu)*(3/2-(3/2-t/tau_0)*(exp(-t/tau_0)))/(tau_p) - 3*alpha*(J^mu)/(tau_m) ] ;
The function "J" appearing in the derivative (f2) is supposed to be "abs(J)" .
All attempts I did ended up with syntax errors..
Thanks in advance :)
Uri

4 commentaires

Geoff Hayes
Geoff Hayes le 27 Fév 2020
Ori - what attempts did you make? What syntax errors did you observe? Why can't you just replace J with abs(J) in your function "body" (leave the input parameter as is)?
Ori Iger
Ori Iger le 27 Fév 2020
I have no idea, but this is the code.
I've tried some solutions of similar problems I found online, with no luck.
Array indices must be positive integers or logical values.
Error in
Model_S1_Weights_Lims>@(t,J)lambda*tau_0*(0.5*(tanh(abs(J(t))-theta_i)-tanh(-theta_i)))*(0.25*(1-(tanh(-theta_0))^2))*[(1-(abs(J(t)))^mu)*(3/2-(3/2-t/tau_0)*(exp(-t/tau_0)))/(tau_p)-3*alpha*((abs(J(t)))^mu)/(tau_m)]
(line 40)
f = @(t,J) lambda * tau_0 * (0.5*(tanh(abs(J(t)) -theta_i)-tanh(-theta_i)))*(0.25*(1-(tanh(-theta_0))^2)) * [ (1
- (abs(J(t)))^mu) * (3/2-(3/2-t/tau_0) * (exp(-t/tau_0))) /(tau_p) - 3*alpha*((abs(J(t)))^mu) /(tau_m) ];
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 Model_S1_Weights_Lims (line 54)
[t,J]=ode45(f,[0 5000],0.9);
Thanks
Uri
Ori Iger
Ori Iger le 27 Fév 2020
also got this in one attempt
Conversion to logical from symfun is not possible.
Geoff Hayes
Geoff Hayes le 27 Fév 2020
Ori - you may need to show more of your code so that we can see how the variables to your anonymous function are defined. Are all of the variables scalars? Are any arrays? The error message seems to be telling you that you are trying to use a negative or non-integer index into an array. I do see that you are using square brackets in your code...what is the intention here? Is this valid syntax?

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 27 Fév 2020

0 votes

If this error started occurring when you added an abs call inside the anonymous function, you most likely have defined a variable named abs, and your attempts to call the function abs are being interpreted as an attempt to index into the variable abs. Rename or remove the variable, recreate the anonymous function, and call ode45 with the recreated function handle.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by