If function, Index exceeds matrix dimensions.

2 views (last 30 days)
Hi All, my code is:
syms x y m n;
funt=@(x,m,n,y) (100-10-x+y).^(-0.5)-(2.*n+4.*m.*y);
if funt(x,m,n,0)<=0
funfun(x,m,n)=0;
elseif funt(x,m,n,50)>=0
funfun(x,m,n)=50;
else funfun(x,m,n)=fzero(@(y) (100-10-x+y).^(-0.5)-(2.*n+4.*m.*y),[0,50]);
end
feval(funfun,10,0.002,0.4)
The error is:
Conversion to logical from sym is not possible.
I would like to construct a function of x and z. z can be regarded as an input and the range of x is [0,50]. Then I test this function at the value of 10 and z=[0.002,0.4]. This is my first time to use if function in matlab. So it might be some errors in my code. Thank you so much in advance. Any help is appreciated!
  2 Comments
Star Strider
Star Strider on 5 Feb 2019
The error is:
Index exceeds matrix dimensions.
This is most likely because ‘funfun’ does not exist until you define it here (as a matrix, not a function).
What what is ‘funfun’, and do you want to do with ‘funfun’?
James_111
James_111 on 6 Feb 2019
Thanks Star!
I still struggle with this code. For my code, my target is to construct a function of funfun(x,m,n) depends on x, m and n. Finally, by taking integration of funfun(x,m,n) with respect to x from 0 to 50, it will become a function of m and n. Then the last step is to solve the two unknowns m and n by using two equations.
So my code is just the first step, to construct a function of funfun(x,m,n). The value of funfun(x,m,n) will be divided into three cases depend on the funt(x,m,n,0) and funt(x,m,n,50). The first case is, if funt(x,m,n,0)<=0, then funfun(x,m,n)=0. The second case, if funt(x,m,n,50)>=0, then funfun(x,m,n)=50. The final case is, funfun(x,m,n)=fzero(@(y) (100-10-x+y).^(-0.5)-(2.*n+4.*m.*y),[0,50]).

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 5 Feb 2019
Edited: Adam Danz on 5 Feb 2019
When evaluating your anonymous function, the error is caused by "z(2)". In your code, size(z) = [1, 1] and you're trying to index the 2nd value; hence, "Index exceeds matrix dimensions".
funt=@(x,y,z) (100-10-x+y).^(-0.5)-(2.*z(2)+4.*z(1).*y);
% **** error
If z is always [0.002, 0.4] then you can replace z(1) and z(2) with those values in the anonymous function. If the value of z changes, then you'll need to provide those inputs when you call the function. But currently, your symbolic 'z' is does not have 2 elements.
  3 Comments
Adam Danz
Adam Danz on 5 Feb 2019
Edited: Adam Danz on 5 Feb 2019
In your 3rd line, if you evaluate the anonymous function like so (below), it produces a symbolic expression.
funt(x,0,m,n)
ans =
1/(90 - x)^(1/2) - 2*n % <-- symbolic !
You're then entering that symbolic expression in to feval(). The first input to feval() needs to be a function handle, or a string / character vector to a function. When you enter a symbolic expression, feval() fails and throws an error.
It's not clear to me what you're doing so I'm not sure what adivice to give. If you have values for x, m, and n, then avoid the call to syms() at the beginning and skip the call to feval().
if funt(x,0,m,n) <= 0 %instead of if feval(funt()) <=0
Lastly, there's nothing wrong with indexing within your anonymous function. You just need to make sure the input variables have a sufficient number of elements so your indices don't cause an error. In your case, 'z' was a symbol of size [1,1] and you were trying to index the 2nd position.
James_111
James_111 on 6 Feb 2019
Thanks Adam for your detailed explanation!
I still struggle with symbolic expression. For my code, my target is to construct a function of funfun(x,m,n) depends on x, m and n. Finally, by taking integration of funfun(x,m,n) with respect to x from 0 to 50, it will become a function of m and n. Then the last step is to solve the two unknowns m and n by using two equations.
So my code is just the first step, to construct a function of funfun(x,m,n). The value of funfun(x,m,n) will be divided into three cases depend on the funt(x,m,n,0) and funt(x,m,n,50). The first case is, if funt(x,m,n,0)<=0, then funfun(x,m,n)=0. The second case, if funt(x,m,n,50)>=0, then funfun(x,m,n)=50. The final case is, funfun(x,m,n)=fzero(@(y) (100-10-x+y).^(-0.5)-(2.*n+4.*m.*y),[0,50]).

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Feb 2019
You need to switch to using piecewise()
Note that if you matlabFunction an expression involving piecewise then you must specify the 'File' option and what you get out will not be vectorized with respect to that variable .

Community Treasure Hunt

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

Start Hunting!