How to solve error in odearguments?Full code below is not runnnig.
Show older comments
clc,clear all;
[t,x]=ode45(@queen,[0 5],3);
function dy = queen(t,x)
xt = getGlobalx;
e = xt-x;
u=2*(x+e);
dy =-sign(x)*sqrt(abs(x))+ x + u;
if 32*(e^2)>abs(x)^1.5 && t > 0
setGlobalx(x);
event = 1 ;
t;
dimwrite('ev.txt',1,'append')
else
dimwrite('ev.txt',0,'append')
end
plot(t,x)
end
function r = getGlobalx
global x
r = x;
end
function setGlobalx(val)
global x
x = val;
end
4 Comments
Torsten
on 8 Oct 2022
When "getGlobalx" is first called in "queens", the global variable x is not yet initialized. This will throw an error.
Further, "dimwrite" must be "dlmwrite".
KUDAKWASHE PHIRI
on 9 Oct 2022
Walter Roberson
on 9 Oct 2022
The option ls '-append' not 'append'
KUDAKWASHE PHIRI
on 9 Oct 2022
Accepted Answer
More Answers (1)
KUDAKWASHE PHIRI
on 9 Oct 2022
0 votes
1 Comment
Walter Roberson
on 9 Oct 2022
u = t*0;
That is an array of 0 the same size as t
if 3*x(i);
That tests whether 3*x(i) is non-zero, which would be true if x(i) is non-zero, no point in the 3* part.
else
u(i)=0;
else would be true in the case that x(i) is exactly 0. In that case you set an element of u to be 0. But you created u to be all zero so this is a waste of time.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!