Info
This question is closed. Reopen it to edit or answer.
How to get rid of this error please help me? Subscript indices must either be real positive integers or logicals. Error in rough (line 10) c(t)=A1*cos(2*pi)*f1*t
2 views (last 30 days)
Show older comments
Subscript indices must either be real positive integers or logicals.
Error in rough (line 10)
c(t)=A1*cos(2*pi)*f1*t
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199529/image.png)
0 Comments
Answers (2)
madhan ravi
on 21 Dec 2018
Edited: madhan ravi
on 21 Dec 2018
Add clear all at the very beginning , replace * with .* and remove (t)
0 Comments
Walter Roberson
on 21 Dec 2018
MATLAB uses the same syntax (value) for both indexing and function calls . When you have the syntax name(value) on the right hand side of an = and value is numeric then you cannot immediately tell whether you are invoking indexing or invoking a function: MATLAB looks up the data type associated with the name to determine what is being done.
So c(t) with numeric t appearing on the right hand side of an = could be either a call to a function c or indexing of an array with that name . In the case of a function the numeric values can be arbitrary including values such as 0.1 . But in the case of indexing then the values must be postive integers indicating relative array position, and an input value such as 0.1 would not be permitted . It therefore becomes important to know how c has been defined .
That is on the right hand side of an = . However when name(value) appears on the left hand side of = and the value is numeric such as your c(t) assignment then that is only ever array indexing never a call to a function (other than the assignment function ) . And since that is the case then your c(t) on the left is invalid because t includes values such as 0.1 that are not positive integers .
If you have a look at what you were coding you can see that you were trying to define a formula in traditional function notation. In basic MATLAB it is not possible to define formula in traditional function notation: numeric functions have to be created using other notation .
It is possible to use the Symbolic Toolbox product to define functions in traditional function notation . In order to do that the variable such as t that appears in the formula must not have a numeric value: it must be declared as symbolic . For example
syms t
c(t) = sin(t)^2/(t+1)
Again , it is crucial that the variable that appears in () on the left must not have a numeric value when you are defining a symbolic function .
There are two ways to define numeric functions , one of which would look like
c = @(t) sin(t).^2./(t+1)
notice the variable t does not appear on the left side.
Emphasis: c(t) on the left side of an assignment with t already defined as numeric is always interpreted as aa request to index .So once you defined tt as those values from 0.1 it is not possible to have c(t) on the left side of an assignment . You cannot define formula that way with the variable already having a numeric value .
Now as madhan says you can also remove the (t) . With numeric t defined it is valid to have
c = sin(t).^2./(t+1)
This is not indexing, this is vectorized calculation . You can access individual results with indexing such ass c(1) for the first and c(2) for the second . These are relative positions not time values: c(1) does not mean c at time t==1
You need to keep straight indexing (relative positions) and function evaluation , even though they both use () notation .
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!