Indexing error in MATLAB coding project

3 replies · 2K views · last reply
cookiemnstr510510
Messages
162
Reaction score
14
Hello, I am working on a MATLAB project for my intro to MATLAB course. My project evaluates certain parameters of a spring mass system and graphs the result along with a little mock animation.
inputs the user is allowed to change:
Initial Position at time =0
Initial Velocity at time=0
Mass of block
Spring Constant
Damping coefficient
External force

Once the user inputs these coefficients, my program computes the differential equation, graphs the result, and displays an animation that is directly connected to the results. It does a few other things as well, but I believe my error falls somewhere in the described part of my program.

I notice when i type in certain parameters my program gives me the error attached.
I will add several attachments to this post along with my code and hopefully someone can figure out where the issue arises. I am a bit pressed for time since my project is due on Tuesday.
The MyFigure.jpg attachment shows what the output is when I type in the specific numbers that give me the error which is in the attachment matlabError.jpg. I have attached my code in Spring Mass Code Project.docx.

Any help would greatly appreciated!
Thanks in advanced.
 
Attachments
  • MATLAB app window titled Spring Mass For Second Order Ordinary Differential Equations, showing input fields for position and velocity at time 0, mass, damping, spring constant and external force, with a Calculate button and a plot of a rising curve.
    MyFigure.JPG
    36.7 KB · Views: 469
  • MATLAB error message in red text: "Array indices must be positive integers or logical values." Stack shows Error in SpringMassP>pushbutton1_Callback (line 121) at P=circular(y(k)+1,.5,.5,'r'), then gui_mainfcn (line 95), SpringMassP (line 19), and a final error while evaluating UIControl Callback.
    matlabError.JPG
    19.5 KB · Views: 468
  • Spring Mass Code project.docx
    Spring Mass Code project.docx
    19.1 KB · Views: 400
  • Spring Mass Code project.docx
    Spring Mass Code project.docx
    19.1 KB · Views: 396
  • MATLAB Command Window error text in red: "Array indices must be positive integers or logical values." Stack shows Error in SpringMassP>pushbutton1_Callback (line 121) with code P=circular(y(k)+1,.5,.5,'r'), then errors in gui_mainfcn (line 95) and SpringMassP (line 19).
    matlabError.JPG
    19.5 KB · Views: 439
You could follow the lead from the error message and do a check see if 'k' is, in fact, an integer. Use isinteger() to catch a non-integer case.
 
What is 'circular' supposed to do ? Where is it ?
Idem feval and gui_mainfcn ?
 
The error report you show is a stack trace. The error occurred in the lowest function and percolated back to the top function. I look at th lowest function that is my code and start from there making sure I called whatever function with the correct inputs and array inputs.

I believe Matlab has a stepping debugger that you can use to step through your code and look at variables before they are passed to a function. Often just stepping through your code you will spot the error.