I've been struggling with this program for a long time.
Is this ode45 being used incorrectly?
global d;
d=1;
tspan=[0;0.05];
xi=[1;2;pi];
vk=[3;5];
func=@(xi,vk) TwoWheel2(xi,vk);
[t,y]=ode45(func,tspan,0);
function dxi = TwoWheel2(xi,vk)
v1=speed(vk);
wk=toRadian(vk);
dxi = zeros(3,1);
dxi(1) = cos(xi(3)) * v1;
dxi(2) = sin(xi(3)) * v1;
dxi(3) = wk;
end
function u0=toRadian(vk)
global d;
vr=vk(1);
vl=vk(2);
u0=(vr-vl)/(2*d);
end
function v1=speed(vk)
vr=vk(1);
vl=vk(2);
v1=(vr+vl)/2;
end
>> testode
The index exceeds the number of array elements (1).
error: testode>speed (line 22)
vl=vk(2);
error: testode>TwoWheel2 (line 7)
v1=speed(vk);
error: testode>@(xi,vk)TwoWheel2(xi,vk) (line 4)
func=@(xi,vk) TwoWheel2(xi,vk);
error: odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
error: ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
error: testode (line 5)
[t,y]=ode45(func,tspan,0);

 채택된 답변

Alan Stevens
Alan Stevens 2020년 9월 19일

0 개 추천

See the following:
global d;
d=1;
tspan=[0;0.05];
xi=[1;2;pi];
vk=[3;5];
[t,y]=ode45(@TwoWheel2,tspan,xi,[],vk);
function dxi = TwoWheel2(~,xi,vk)
v1=speed(vk);
wk=toRadian(vk);
dxi = zeros(3,1);
dxi(1) = cos(xi(3)) * v1;
dxi(2) = sin(xi(3)) * v1;
dxi(3) = wk;
end
function u0=toRadian(vk)
global d;
vr=vk(1);
vl=vk(2);
u0=(vr-vl)/(2*d);
end
function v1=speed(vk)
vr=vk(1);
vl=vk(2);
v1=(vr+vl)/2;
end

댓글 수: 5

Tsubasa Mano
Tsubasa Mano 2020년 9월 19일
Thank you very very much!
If you don't mind,May I ask you to tell me why doing so would solve the problem?
Alan Stevens
Alan Stevens 2020년 9월 19일
You were calling the function with a single initial value of zero. Ode45 needed to call it with the same size vector as is returned (one with three elements here).
Tsubasa Mano
Tsubasa Mano 2020년 9월 19일
I understand, thank you.Let me ask you one more thing.
[t,y]=ode45(@TwoWheel2,tspan,xi,[],vk);
→[t,y]=ode45(@TwoWheel2,tspan,xi,vk,[]);Is this okay?I think [] is maybe option
dxi = TwoWheel2(~,xi,vk)
what does mean '~'? Sorry for all the questions.
Alan Stevens
Alan Stevens 2020년 9월 19일
편집: Alan Stevens 2020년 9월 19일
  1. Yes, [] is a placeholder for options.
  2. You would often have t, the time value there (more generally, the name of the independent variable); but, in your case, t is not used explicitly within TwoWheel2, so MATLAB suggests replacing it by ~. If it did make explicit use of the value of t, then ~ would need to be replaced by t. The ~ isn't essential; the routine would still work if you did replace it with t.
Tsubasa Mano
Tsubasa Mano 2020년 9월 19일
Thank you for everything.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2020b

태그

질문:

2020년 9월 19일

댓글:

2020년 9월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by