Subscript Indices MATLAB Error

p=1.2;
q=1;
LRainbow=6.1;
y=linspace(0,100000);
h(y)=(p*(y.^2))/(q+(y.^2));
I keep getting this error in regards to the h(y) line:
Subscript indices must either be real positive integers or logicals.
I don't understand what this means and how to fix it. Can anyone help? Thanks! :)

Réponses (1)

Walter Roberson
Walter Roberson le 16 Fév 2016

1 vote

In MATLAB, only symbolic functions can be defined using the syntax
NAME(VARIABLE) = EXPRESSION
If you are not defining a symbolic function you would have to use an anonymous function:
h = @(y) (p*(y.^2))./(q+(y.^2));
(Note: I corrected your / to ./ along the way)
As this would be a function, you would need to apply it to a value to get a result, such as
plot(y, h(y))
However, in MATLAB when you already know the value of the variables to apply to, you would typically skip creating a function and would instead just use an expression that calculated the values directly:
h = (p*(y.^2))./(q+(y.^2));
plot(y, h);
You need to learn to distinguish functions (formula that say how a value should be created given inputs) from values (actual results of calculation given inputs).

Community Treasure Hunt

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

Start Hunting!

Translated by