Indexing error in MATLAB coding project

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
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

  • MyFigure.JPG
    MyFigure.JPG
    36.7 KB · Views: 455
  • matlabError.JPG
    matlabError.JPG
    19.5 KB · Views: 456
  • Spring Mass Code project.docx
    Spring Mass Code project.docx
    19.1 KB · Views: 387
  • Spring Mass Code project.docx
    Spring Mass Code project.docx
    19.1 KB · Views: 387
  • matlabError.JPG
    matlabError.JPG
    19.5 KB · Views: 429
Physics news on Phys.org
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.
 
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.