How to solve error in odearguments?Full code below is not runnnig.

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

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".
Error using dlmwrite
append is not a valid attribute or delimiter. Delimiter must be a single character.
Error in testing>queen (line 20)
dlmwrite('ev.txt',0,'append')
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in testing (line 3)
[t,x]=ode45(@queen,[0 5],3);
The option ls '-append' not 'append'
Thanks at least its giving some result with a few errors
Error using odearguments
QUEEN returns a vector of length 0, but the length of initial conditions vector is 1. The vector returned by QUEEN and
the initial conditions vector must have the same number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in testing (line 3)
[t,x]=ode45(@queen,[0 5],3);

Sign in to comment.

 Accepted Answer

Every time
if 32*(e^2)>abs(x)^1.5 && t > 0
is satisfied, you set a global variable x. That global x is then used in the next and following iterations
xt = getGlobalx;
and you do calculations based on xt.
You are assuming here that your calculations are always progressing in time, and that if the condition succeeded then the (t, x) combination will be accepted as the new base point. Neither of those assumptions are true. Please read https://www.mathworks.com/matlabcentral/answers/1818985-how-to-work-variables-outside-function#answer_1068360
You will need to change your calculations.

3 Comments

well, there was a lot explantion to the link you reffered me to. But I am to at understand where my error is. Still not getting desirable result, good news they are partly correct.
In my days growing up, children who want to cross the street are trained to look ahead to see if the path is clear, then to look to the left to see if a car is approaching from the nearby lane, and then to look to the right to see if a car is approaching from the opposite lane.
Now each time you do a "look" operation in your ode function, if the condition is met, you update global x. After 6 different "look" are done, the result in x is the result of the most recent update. But which direction were you looking at the time? Did you look to the left and see that a car was about to run you over and set x to the distance to that car, and then look to the right and see that the car opposite is in the process of passing by on the other side and will be gone when you get there, and record the distance to it... and then go ahead and walk across the street because it was safe in the most recent direction that you looked, since the x that was recorded earlier was overwritten by the information in the more recent look??
You need to change your approach. If you need the change relative to the previous x then you are working with Δx so make that an explicit derivative in your system.
The mathematics behind ode45 requires that the functions have continuous derivatives. sign(x)*sqrt(abs(x)) does not have a continuous derivative
It looks to me as if you are implementing a friction term of some kind, wanting to have the friction oppose the motion. ode45 cannot handle switching direction in such situations, not in a single call to ode45. You need to set an event function to detect the zero crossing and terminate the ode45 and then make another ode45 call to continue from there. See the "ballode" example.

Sign in to comment.

More Answers (1)

Was missing this condition.and no initialisation of variable as mentioned earlier by another expert. overall thank to you all who to the liberty to attend to my problem.
setGlobalx(3)
[t,x]=ode45(@queen,[0 5],3);
figure(1)
plot(t,x)
u = t*0;
for i = 1:length(t)
if 3*x(i);
else
u(i)=0;
end

1 Comment

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.

Sign in to comment.

Products

Release

R2022b

Tags

Community Treasure Hunt

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

Start Hunting!