Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Anonymous function and function handle

Subject: Anonymous function and function handle

From: Mohamed Nasr

Date: 9 Feb, 2011 21:18:03

Message: 1 of 11

Hi,
I need some help in using anonymous function.I'm trying to write a code which evaluates the determinant of a certain matrix. Every element in this matrix is f(x).
I need to solve the equation determinant=0 and find x which achieves this so
I defined x as symbolic variable (syms x;) and N=11 and I wrote:
solve('eye(2*N+1)-A*B',x);
where A and B are 2 matrices whose each element is f(x)(A*B is 11 x 11 matrix). The result here was that x was got to be a symbol...no number was got as solution to the equation...just symbol, so I tried another way :
D=@(x)(eye(2*N+1)-A(x)*B(x)); %evaluating the determinant as f(x)
x=fsolve(@(x)D(x),[0.1 1000]);% solving for value of x
This also didn't work with the error of:
"Subscript indices must either be real positive integers or logicals"
I also had errors in other trials that "'mtimes' is not defined for class 'function_handle'"
So I don't understand why doesn't matlab solve the equation and give me the result directly (for ex: x=5)...BTW I'm using Matlab R2006a version 7.2.0.232
Any help would be appreciated!

Subject: Anonymous function and function handle

From: Steven_Lord

Date: 10 Feb, 2011 04:27:22

Message: 2 of 11



"Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
news:iiv0ab$5sv$1@fred.mathworks.com...
> Hi, I need some help in using anonymous function.I'm trying to write a
> code which evaluates the determinant of a certain matrix. Every element in
> this matrix is f(x).
> I need to solve the equation determinant=0 and find x which achieves this
> so I defined x as symbolic variable (syms x;) and N=11 and I wrote:
> solve('eye(2*N+1)-A*B',x);

Do not specify the input to SOLVE as a string.

solve(eye(2*N+1)-A*B, x)

> where A and B are 2 matrices whose each element is f(x)(A*B is 11 x 11
> matrix).

This isn't consistent with your earlier statement that N is 11.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Anonymous function and function handle

From: Mohamed Nasr

Date: 10 Feb, 2011 11:00:05

Message: 3 of 11

When I write:
 solve(eye(2*N+1)-A*B, x);
I get the error: "Function 'mtimes' is not defined for values of class 'function_handle'"
so still it doesn't run...

"Steven_Lord" <slord@mathworks.com> wrote in message <iivpfb$1ru$1@fred.mathworks.com>...
>
>
> "Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
> news:iiv0ab$5sv$1@fred.mathworks.com...
> > Hi, I need some help in using anonymous function.I'm trying to write a
> > code which evaluates the determinant of a certain matrix. Every element in
> > this matrix is f(x).
> > I need to solve the equation determinant=0 and find x which achieves this
> > so I defined x as symbolic variable (syms x;) and N=11 and I wrote:
> > solve('eye(2*N+1)-A*B',x);
>
> Do not specify the input to SOLVE as a string.
>
> solve(eye(2*N+1)-A*B, x)
>
> > where A and B are 2 matrices whose each element is f(x)(A*B is 11 x 11
> > matrix).
>
> This isn't consistent with your earlier statement that N is 11.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Anonymous function and function handle

From: Steven_Lord

Date: 10 Feb, 2011 14:33:03

Message: 4 of 11



"Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
news:ij0gfl$bn0$1@fred.mathworks.com...
> When I write:
> solve(eye(2*N+1)-A*B, x);
> I get the error: "Function 'mtimes' is not defined for values of class
> 'function_handle'"
> so still it doesn't run...

You will need to post in the newsgroup the code that you use to create A and
B. My guess is that one or both of them are function handles even though
you said that A and B were matrices.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Anonymous function and function handle

From: Mohamed Nasr

Date: 10 Feb, 2011 15:05:06

Message: 5 of 11

Sorry, they are function handles and not matrices also N=5 and not 11 so that the matrix dimensions of [eye(2*N+1)-A*B] agree and here is the code:

N=5;M=10;j=0.01;d=0.02
for m=1:(M+1)
    for n=1:(2*N+1)
       S=2*pi*(n-N-1)/d;
       f=@(z)exp(sqrt(-1)*S*z).*cos(((m-1)*z)/(j));
       B(m,n)=@(x)(lm((m-1),x,s)*sinh(c*lm((m-1),x,s)))*
                    (quad(@(z)f,0,s))/(d*hn((n-N-1),x,BETA(i),d)*sinh((b-c)*hn((n-
                     N-1),x,BETA(i),d)));
     end
end

like that we formed a matrix of function handles each element is f(x) and hn, lm are both 2 functions each of which calls an m-file and returns the result as a number.

Note that:This is only a part of my code not all the code as this is a project so I cannot write all code...A is formed the same way as B


"Steven_Lord" <slord@mathworks.com> wrote in message <ij0suv$917$1@fred.mathworks.com>...
>
>
> "Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
> news:ij0gfl$bn0$1@fred.mathworks.com...
> > When I write:
> > solve(eye(2*N+1)-A*B, x);
> > I get the error: "Function 'mtimes' is not defined for values of class
> > 'function_handle'"
> > so still it doesn't run...
>
> You will need to post in the newsgroup the code that you use to create A and
> B. My guess is that one or both of them are function handles even though
> you said that A and B were matrices.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Anonymous function and function handle

From: Steven_Lord

Date: 10 Feb, 2011 17:50:59

Message: 6 of 11



"Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
news:ij0ur2$bui$1@fred.mathworks.com...
> Sorry, they are function handles and not matrices also N=5 and not 11 so
> that the matrix dimensions of [eye(2*N+1)-A*B] agree and here is the code:
>
> N=5;M=10;j=0.01;d=0.02
> for m=1:(M+1)
> for n=1:(2*N+1)
> S=2*pi*(n-N-1)/d;
> f=@(z)exp(sqrt(-1)*S*z).*cos(((m-1)*z)/(j));
> B(m,n)=@(x)(lm((m-1),x,s)*sinh(c*lm((m-1),x,s)))*
>
> (quad(@(z)f,0,s))/(d*hn((n-N-1),x,BETA(i),d)*sinh((b-c)*hn((n-
> N-1),x,BETA(i),d)));

You should not store function handles (including anonymous functions) in an
array; you should have received a warning or error when you tried to do
this.

http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1012896

The expression you have for B is complicated enough that I think it's
probably appropriate to write a regular function to evaluate each element of
B(m, n) -- since the value is the product of an expression that depends on m
but not n and an expression that depends on n but not m, you can evaluate
each once per row or column and simply multiply the appropriate elements of
those results together.

Once you've done this use FZERO or FSOLVE to solve the system of equations
rather than using SOLVE.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Anonymous function and function handle

From: Mohamed Nasr

Date: 10 Feb, 2011 19:41:09

Message: 7 of 11

I cannot evaluate B(m,n)...it must be function in x...the idea is that I need to find the value for x at which the determinant of (eye(2*N+1)-A*B) equals zero...this cannot happen except if we made each element in A and B as function of x then get the determinant and solve its equation in x (determinant=0).


"Steven_Lord" <slord@mathworks.com> wrote in message <ij18i3$bvd$1@fred.mathworks.com>...
>
>
> "Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
> news:ij0ur2$bui$1@fred.mathworks.com...
> > Sorry, they are function handles and not matrices also N=5 and not 11 so
> > that the matrix dimensions of [eye(2*N+1)-A*B] agree and here is the code:
> >
> > N=5;M=10;j=0.01;d=0.02
> > for m=1:(M+1)
> > for n=1:(2*N+1)
> > S=2*pi*(n-N-1)/d;
> > f=@(z)exp(sqrt(-1)*S*z).*cos(((m-1)*z)/(j));
> > B(m,n)=@(x)(lm((m-1),x,s)*sinh(c*lm((m-1),x,s)))*
> >
> > (quad(@(z)f,0,s))/(d*hn((n-N-1),x,BETA(i),d)*sinh((b-c)*hn((n-
> > N-1),x,BETA(i),d)));
>
> You should not store function handles (including anonymous functions) in an
> array; you should have received a warning or error when you tried to do
> this.
>
> http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1012896
>
> The expression you have for B is complicated enough that I think it's
> probably appropriate to write a regular function to evaluate each element of
> B(m, n) -- since the value is the product of an expression that depends on m
> but not n and an expression that depends on n but not m, you can evaluate
> each once per row or column and simply multiply the appropriate elements of
> those results together.
>
> Once you've done this use FZERO or FSOLVE to solve the system of equations
> rather than using SOLVE.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Anonymous function and function handle

From: Steven_Lord

Date: 10 Feb, 2011 19:59:43

Message: 8 of 11



"Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
news:ij1f0l$cvm$1@fred.mathworks.com...
> I cannot evaluate B(m,n)...it must be function in x...the idea is that I
> need to find the value for x at which the determinant of (eye(2*N+1)-A*B)
> equals zero...this cannot happen except if we made each element in A and B
> as function of x then get the determinant and solve its equation in x
> (determinant=0).

Write:

x = fzero(@myFunction, 1)

where myFunction accepts the input x, computes particularA = A(x) and
particularB = B(x) for that particular value of x and returns
eye(11)-particularA*particularB.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Anonymous function and function handle

From: Mohamed Nasr

Date: 11 Feb, 2011 13:58:03

Message: 9 of 11

When I did that I faced the following error within the fzero function:
??? Operands to the || and && operators must be convertible to logical scalar values.
Error in ==> fzero at 308
    elseif ~isfinite(fx) || ~isreal(fx)
Error in ==> Report2 at 8
x=fzero(@(x)myfun(x,BETA(i)),1);
where BETA(i) is an input to the function you mentioned in last post...
so I tried to use fsolve and I found that the output flag value is -2 (Algorithm converges to a value that is not a root)...any ideas?

"Steven_Lord" <slord@mathworks.com> wrote in message <ij1g3f$m71$1@fred.mathworks.com>...
>
>
> "Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
> news:ij1f0l$cvm$1@fred.mathworks.com...
> > I cannot evaluate B(m,n)...it must be function in x...the idea is that I
> > need to find the value for x at which the determinant of (eye(2*N+1)-A*B)
> > equals zero...this cannot happen except if we made each element in A and B
> > as function of x then get the determinant and solve its equation in x
> > (determinant=0).
>
> Write:
>
> x = fzero(@myFunction, 1)
>
> where myFunction accepts the input x, computes particularA = A(x) and
> particularB = B(x) for that particular value of x and returns
> eye(11)-particularA*particularB.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Anonymous function and function handle

From: Steven_Lord

Date: 11 Feb, 2011 14:22:27

Message: 10 of 11



"Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
news:ij3f9b$29s$1@fred.mathworks.com...
> When I did that I faced the following error within the fzero function:
> ??? Operands to the || and && operators must be convertible to logical
> scalar values.
> Error in ==> fzero at 308
> elseif ~isfinite(fx) || ~isreal(fx)
> Error in ==> Report2 at 8
> x=fzero(@(x)myfun(x,BETA(i)),1);
> where BETA(i) is an input to the function you mentioned in last post...
> so I tried to use fsolve and I found that the output flag value is -2
> (Algorithm converges to a value that is not a root)...any ideas?

You should probably send your code to Technical Support along with a
description (written out, not in code form) to ensure that you're solving
the system correctly.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Anonymous function and function handle

From: Mohamed Nasr

Date: 11 Feb, 2011 16:41:04

Message: 11 of 11

Thanks for your patience and trials to help

"Steven_Lord" <slord@mathworks.com> wrote in message <ij3gn3$5vu$1@fred.mathworks.com>...
>
>
> "Mohamed Nasr" <eng.mohamednasr2010@gmail.com> wrote in message
> news:ij3f9b$29s$1@fred.mathworks.com...
> > When I did that I faced the following error within the fzero function:
> > ??? Operands to the || and && operators must be convertible to logical
> > scalar values.
> > Error in ==> fzero at 308
> > elseif ~isfinite(fx) || ~isreal(fx)
> > Error in ==> Report2 at 8
> > x=fzero(@(x)myfun(x,BETA(i)),1);
> > where BETA(i) is an input to the function you mentioned in last post...
> > so I tried to use fsolve and I found that the output flag value is -2
> > (Algorithm converges to a value that is not a root)...any ideas?
>
> You should probably send your code to Technical Support along with a
> description (written out, not in code form) to ensure that you're solving
> the system correctly.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us