Index exceeds matrix dimensions

I'm trying to write this code to define the variables I have and use the subplot feature to plot the equation I have. Unfortunately, I was trying to write for example, 'omega_{n}', it didn't work with me so I had to delete the n because it kept saying that n is undefined. So, I'm trying to write this code and run it so that I would get the subplot I want, hopefully. However, I've encountered a problem that I don't know how to fix it. Here is the code I'm trying to write:
omega=2.5;
t=.02,1
k='(10)ˆ5';
m=4397.7;
F=10*cos(omega)*t;
omega_=sqrt(k/m);
f_=10/m;
x(t)=sin(omega_*t)+cos(omega_*t)+(f_('omega_ˆ{2}')+'(omega)ˆ{2}'*cos*omega*t)
subplot x(t)
I have the error massage at x(t)=sin(omega_*t)+cos(f_('omega_^{2}'+.... where it says index exceeds matrix dimensions. Plus, if you see any other mistakes, let me know please. Many thanks to you.

Réponses (2)

Guillaume
Guillaume le 24 Juil 2018
Modifié(e) : Guillaume le 24 Juil 2018

0 votes

There are many problems with your code. The line
t=.02,1
is equivalent to
t = 0.02
1
I have no idea what you intended to do there. Perhaps, the ,1 is a typo.
Then,
k = '(10)^5';
omega_ = sqrt(k/m);
Again, not sure what the intention is. k is a char array. Matlab will let you divide a char array by a number, each character being converted to its numerical (ASCII) value, thus '(' is 40, '1' is 49, etc. Overall, the above is equivalent to:
omega_ = sqrt([40, 49, 48, 41, 710, 53] / m);
Then, we have in your x(t) = ... line, we have
f_('omega_ˆ{2}')
Again, 'omega_^{2}' is a char array, whose numerical value is [111, 109, 101, 103, 97, 95, 710, 123, 50, 125]|. You're using these numerical values as indices into f_. However, f_ is scalar, the only valid index is 1. Hence why you get index exceeds matrix dimension. Again, no idea what you intended there.
On the same line, we also have
'(omega)ˆ{2}'*cos*omega*t
Again, the char array '(omega)ˆ{2}'. In addition we have a call to cos with no argument, so that will error with _not enough input argument.
The final (!) problem on that line is x(t) which will throw the error index must be positive integers or logical values. Your t is 0.02, this will never be a valid index for x. I'm not even sure why you're trying to index x since t is scalar.
Finally, you're asking for trouble by having two variables whose name differ only by a trailing _. You can be guaranteed that at some point you'll mistype one of them. You can use any name you want for any variable why do you have to recycle omega?
Overall, there's not much that is right in your code and you need to start paying attention to what you write and test each line of code to be sure it does what you intend.

Cette question est clôturée.

Question posée :

le 24 Juil 2018

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by