Index exceeds matrix dimensions
3 views (last 30 days)
Show older comments
Chet Price on 11 May 2012
Answered: Steve Miller on 21 Dec 2022
I get an error for this code, i dont know where or why. Any help would be appreciated.
function project 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));
axis([-1 1 -1.8 .2])
pendulA = line([origin;origin],[origin;origin],'Marker','o')
% plot(0,0,'g.','markersize',25) % hold on % plot([0 xpos],[0 ypos],'r') % plot(xpos,ypos,'g.','markersize',25) % hold off
Jan on 11 May 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.
Steve Miller on 21 Dec 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.
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!