# Index exceeds matrix dimensions

4 Ansichten (letzte 30 Tage)
Chet Price am 11 Mai 2012
Beantwortet: Steve Miller am 21 Dez. 2022
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 KommentareKeine anzeigenKeine ausblenden
Geoff am 11 Mai 2012
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.
Jan am 11 Mai 2012
@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.

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Steve Miller am 21 Dez. 2022
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by