How to fix the error?

2 ビュー (過去 30 日間)
Gazza Dazzle
Gazza Dazzle 2014 年 9 月 2 日
コメント済み: Gazza Dazzle 2014 年 9 月 2 日
Hi I am not sure how to fix the error in this program, thanks in advance.
clc, clear all, close all
l=0.3; sampling=0.01; runningstep=200; timespan=sampling*runningstep;
x0=[pi/6 0];
%main program for numberical solution k=l; while(k<1000) [t,y]=ode45(@pendulum,[0 sampling], x0, [], l); if k==1 yp(1,:)=y(1,1:2); tp(1,:)=t(1,1); else yp(k,:)=y(size(y,1),1:2); tp(k,1)=sampling*k; end %update date x0=y(size(y,1),1:2); %change initial value k=k+1; end
%animation xa=l*sin(yp(:,1)); ya=-l*cos(yp(:,1)); hp=plot([0,0],[xa(1),ya(1)],'linewidth',2,'Marker','o','Erasemode','xor'); axis([-l -0.1 l+0.1 -l-0.1 l+0.1]); for i=1: size(tp) set(hp,'xdata',[0,xa(i)],'ydata',[0,ya(i)]); drawnow pause(0.001); grid on mov(i)=getframe; end movie2avi(mov,'pendulum.avi')
2nd Function file ------------------------------------------- function dy=pendulum(t,y,l) dy(1,1)=y(2,1); dy(2,1)=-9.81/l*sin(y(1,1));
  2 件のコメント
Gazza Dazzle
Gazza Dazzle 2014 年 9 月 2 日
Subscript indices must either be real positive integers or logicals.
Error in pendulum01 (line 18) yp(k,:)=y(size(y,1),1:2);
Pierre Benoit
Pierre Benoit 2014 年 9 月 2 日

サインインしてコメントする。

採用された回答

Yona
Yona 2014 年 9 月 2 日
you start by
l=0.3;
and you put this value in k
k=l;
and because k>1 you do
yp(k,:)=y(size(y,1),1:2);
but you don't have yp(0.3,:).
k need to be positive integer.
  1 件のコメント
Gazza Dazzle
Gazza Dazzle 2014 年 9 月 2 日
OH great thanks for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeClassical Mechanics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by