0
votes

So I need to plot a function, let's say it's: y = sin( xk )

But I can only write the matlab code like:

x = -pi : .1 : pi;
y = sin(x);
plot(x,y);

If I try to do xk, then it cries about not knowing what k is. Any idea how I can plot functions with variables that contain subscripts (the subscripts are just descriptive, they don't hold any value)? Thanks

1
Where do you want the subscript to appear, in the plot, say the legend? You cannot have subscripts in the code itself and to a limited extent in symbolic expressions. - Oleg

1 Answers

2
votes

Variables cannot have subscripts. You don't have to reproduce the formula exactly in a MATLAB statement. This is fine to name variable just x, or xk, or x_k, etc.

On the other hand, if you have multiple vectors that you want to associate with the same name, you can put them into a cell array and get each vector as x{k}.

You can use subscripts in axes labels, title and text annotations using Tex (default) or Latex interpretor. Use underscore character followed by subscript in a text string.

title('y = sin(x_k)')

or

title('y = sin(x_{several chars})')