Return Vector of length zero ??

I am building a robot simulation by solving the differential equations at different input speed values, the function uses [0 0 0]=[x y theta] as initial conditions then updates the initial conditions every iteration. when i use a for loop to input the number of iterations i want the equation to be solved it works fine!, however when i use a while loop to keep the function solving as i input the speed (through a edit text in gui) i get an error message "KPATH returns a vector of length 0, but the length of initial conditions vector is 3." . How can function return a zero length vector ? or the while loop cannot be used in this case ?
%
**code
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global sld1
global sld2
check = get(handles.checkbox1,'Value');
initials = zeros(1,3); %start at origin
p = par();
tspan = [0 1];
while check == true
p.WL = sld1 ;p.WR = sld2;
sol= ode23(@Kpath, tspan, initials,[],p);
[t,s] = ode23(@Kpath, tspan, initials,[],p);
initials = deval(sol,2);
xlabel('x-position [m]');ylabel('y-position [m]');
title('robot path');
grid on
plot(s(:,1),s(:,2),'b','linewidth',1.5);
hold on
for j = 1:length(s(:,1))
q = plot(s(j,1),s(j,2),'ro','MarkerSize',5,'linewidth',1.5);
axis([-2.5 2.5 -2.5 2.5]);grid on;
pause(0.01)
delete(q)
end
end
function p = par()
p.L = 0.12; %length [m]
p.r = 0.1; %radius of wheel [m]
function dt = Kpath(t,c,p)
x = c(1);y = c(2);th = c(3);
dx = (((p.r*p.WL)+(p.r*p.WR))/2) * cos(th);
dy = (((p.r*p.WL)+(p.r*p.WR))/2) * sin(th);
dth= ((p.r*p.WL)-(p.r*p.WR))/p.L;
dt = [dx;dy;dth]

 采纳的回答

I am guessing here because I cannot run your code.
Defining ‘initials’ as the last row of ‘s’ may be what you want:
initials = s(end,:);
The new initial conditions will be the last row of the previous solution vector.

6 个评论

but i already did that with (initials = deval(sol,2);) ? but thanks
I should have looked closer.
You probably need to use an anonymous function construction in your ‘Kpath’ call:
@(t,c)Kpath(t,c,p)
See if that works.
is there a mistake when using the while loop with the ode function ? because same code with for loop works perfectly but i need it to be continuous ?
The only problem I see is that the value of ‘check’ never changes in the while loop.
The global variables could be a problem. I have no idea what the global variables are doing, or what effect they may have on your code. It is generally best to avoid using them them. Pass the values as arguments to your function instead.
Otherwise, it is not obvious to me that there is a reason your code would work in a for loop and not a while loop.
Thanks alot i removed the global variables and it worked fine :) i have no idea why but it works :) thanks again
As always, my pleasure!

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2017-4-18

1 个投票

Kpath is called with the parameters defined by p.WL = sld1 ;p.WR = sld2. If sld1 and sld2 are empty matrices, KPath replies an empty array also.
Unfortunately these parameters are defined as global variables. Then all other functions can set the values to empty matrices and debugging is tedious. This is the well known disadvantage of globals. Try to avoid it, because there is always a better way.
Use anonymous functions instead of parmeters in the call of ODE23, see http://www.mathworks.com/matlabcentral/answers/1971.

1 个评论

Thanks alot i removed the global variables and it worked fine :) i have no idea why but it works :) thanks again

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by