# Index exceeds matrix dimensions

3 ビュー (過去 30 日間)
Chet Price 2012 年 5 月 11 日

I get an error for this code, i dont know where or why. Any help would be appreciated.
function project Penduli
function Penduli
close all clear all %user input gravity = 5; mass = 1; Beta = 0.2; y0 = [45 0]; time = [0:.1:30];
for L = linspace(.5,1.5,5) [T,Y] = ode45(@pendulum,time,y0,[],gravity,L,mass,Beta); hold on animatePendulum(T,Y,L); end hold off
function dydt = pendulum(t,y,gravity,L,mass,Beta)
dydt = zeros(2,1);
dydt(1) = y(2); dydt(2) = -Beta*y(2)-mass*gravity/L*sin(y(1));
function animatePendulum(T,Y,L) nT = length(T); hold on for pt = 1:nT origin=zeros(size(L)); x_pos = L*sin(Y(pt,1)); y_pos = -L*cos(Y(pt,1));
for x=1:5
axis equal
axis([-1 1 -1.8 .2])
pendulA = line([origin;origin],[origin;origin],'Marker','o')
set(pendulA(x),'visible','on');
set(pendulA(x),'XData',[0 x_pos(x)]);
set(pendulA(x),'YData',[0 y_pos(x)]);
drawnow
end
% plot(0,0,'g.','markersize',25) % hold on % plot([0 xpos],[0 ypos],'r') % plot(xpos,ypos,'g.','markersize',25) % hold off
drawnow
end
##### 2 件のコメント表示非表示 1 件の古いコメント
Jan 2012 年 5 月 11 日
@geoff: I'd vote this, if it was posted as answer.
@Chet: Please format the code. Using "clear all" on top of a function is strange and not useful. Using an anonymous function to carry additional parameters is recommended. Please format the code such that it is readable. All these three topics have been discussed repeatedly in this forum and a search is recommended.

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

### 回答 (1 件)

Steve Miller 2022 年 12 月 21 日
As posted by Geoff in the comments above:
The usual procedure is to read the error message, go to the source file and line number that the error message tells you, and look at the code. If you want to ask us what's wrong, then you should tell us which line the error occurs on. Unless you want to rely on somebody here taking the time to fix your unformatted code, run it and debug it for you.
This is also helpful... Run the following command and then run your script.
dbstop if error
That will break execution where your error occurs. Then you can play with the data values that are in scope, examining them in the command window and whatnot.
--Steve

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

### カテゴリ

Find more on Programming 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!

Translated by