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! :)

답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 16일

1 개 추천

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).

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2016년 2월 16일

답변:

2016년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by