Index in position 1 is invalid. Array indices must be positive integers or logical values?
Show older comments
>> lambda = 1;
>> phi = zero;
Undefined function or variable 'zero'.
>> phi = 0;
>> theta = 90;
>> B= 2*pi/lambda;
>> Lx = 20*lambda;
>> Ly = 10*lambda;
>> u = sin(theta);
>> v = 0;
>> f(u,v) = (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Index in position
1 is invalid. Array indices must be positive integers or logical values.
Hi,
After I try to get values to f(u,v) .I got this error
Index in position
1 is invalid. Array indices must be positive integers or logical values.
Could you please help solve this problem?
3 Comments
Mil Shastri
on 10 Mar 2020
Edited: Mil Shastri
on 10 Mar 2020
Looks like you want to create a function that takes u,v as inputs and provides an output as the result equation you have written. To write a function in MATLAB you have to follow the syntax, see function documentation here https://www.mathworks.com/help/matlab/ref/function.html#btexm3w
You can try the following by pasting this in a Live Script:
phi = 0;
theta = 90;
u = sin(theta);
v = 0;
result=myf(u,v)
function w=myf(u,v)
lambda = 1;
B= 2*pi/lambda;
Lx = 20*lambda;
Ly = 10*lambda;
w=(sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
end
Now, if you want to run this for multiple values of , then try this:
phi = 0;
theta = 90;
u = sin(theta);
v = 0;
%result=myf(u,v)
num_points=100;
vs=linspace(0,0.1,num_points);
for n=1:num_points
results(n)=myf(u,vs(n));
end
plot(results)
function w=myf(u,v)
lambda = 1;
B= 2*pi/lambda;
Lx = 20*lambda;
Ly = 10*lambda;
w=(sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
end

tmarske
on 10 Mar 2020
Here is the issue:
>> theta = 90;
...
>> u = sin(theta);
>> v = 0;
>> f(u,v) = (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Matlab interprets this last line as 'create a new array named f, evaluate the RHS of this expression, and assign that value to the (u, v) entry of the array f. Since neither u nor v are positive integers this is not possible, so Matlab throws the error.
It looks like what you actually wanted to do was create a new function named f. The syntax for this is different. You should either:
1) create a new function in a file as described here: https://uk.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html
2) create an anonymous function and assign it to f as follows:
>> f = @(u,v) (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Walter Roberson
on 10 Mar 2020
Are you defining a formula, where you want to be able to provide arbitrary u and v afterwards and have it calculate the f value? Or are you defining an array, in which you have specific numeric u and v values and the result for the (J, K)'th pair of input values should be stored in output location indexed by (J, K)?
By the way: you divide by v but your v is 0, so your right hand side is going to be strictly inf or nan.
Answers (1)
James Tursa
on 10 Mar 2020
If you are trying to create an anonymous function, the syntax is:
f = @(u,v) (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Then downstream in your code you can call f(u,v) with arbitrary u and v inputs.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!