I am trying to plot the function
f(x, y) = (x – 3).^2 – (y – 2).^2.
x is a vector from 2 to 4, and y is a vector from 1 to 3, both with increments of 0.2. However, I am getting the error:
“Subscript indices must either be real positive integers or logicals”.
What do I do to fix this error?
I (think) I see what you are trying to achieve. You are writing your syntax like a mathematical function definition. Matlab is interpreting
fas a 2-dimensional data type and trying to assign the value of the expression to data indexed atx,y. The values ofxandyare not integers, so Matlab complains.If you want to plot the output of the function (we’ll call it
z) as a function ofxandy, you need to define the function quite differently . . .This will give you an output like this . . .

The
f = @(x,y)part of the first line states you want to define a function calledftaking variablesxandy. The rest of the line is the definition of that function.If you want to plot
zas a function of bothxandy, then you need to supply all possible combinations in your range. This is what the line containing therepmatcommands is for.EDIT
There is a neat Matlab function
meshgridthat can replace the repmat version of the script as suggested by @bas (welcome bas, please scroll to bas’ answer and +1 it!) …