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:
Undefined function for function_handle

Subject: Undefined function for function_handle

From: Nicholas Tan

Date: 27 Nov, 2010 02:39:04

Message: 1 of 4

I need to find the roots of an equation later on. Because of my equation, r is too long, i have to separate the terms in a few parts. However, by doing this, it shows an error:


syms y;
syms z;

JRa= @(y) besselj(1,y);
JR1a= @(y) besselj(0, y) - besselj(1, y)./y;

JRb= @(y) besselj(1,z);
JR1b= @(y) besselj(0, z) - besselj(1, z)./z;

Yb= @(y) bessely(1,z);
Y1b= @(y) bessely(0, z) - bessely(1, z)./z;

Ya= @(y) bessely(1,y);
Y1a= @(y) bessely(0, y) - bessely(1, y)./y;

r= y.*((JR1a.*Y1b-JR1b.*Y1a)./(JRa.*Y1b-JR1b.*Ya))

Error:

??? Undefined function or method 'times' for input arguments of type
'function_handle'.
 
Anyway i can solve the error?

Subject: Undefined function for function_handle

From: Steven Lord

Date: 27 Nov, 2010 18:47:04

Message: 2 of 4

"Nicholas Tan" <cytan89@yahoo.com> wrote in message <icpr08$dqv$1@fred.mathworks.com>...
> I need to find the roots of an equation later on. Because of my equation, r is too long, i have to separate the terms in a few parts. However, by doing this, it shows an error:
>
>
> syms y;
> syms z;

None of the rest of your code treats y or z as symbolic variables; remove the previous two lines.

> JRa= @(y) besselj(1,y);
> JR1a= @(y) besselj(0, y) - besselj(1, y)./y;
>
> JRb= @(y) besselj(1,z);
> JR1b= @(y) besselj(0, z) - besselj(1, z)./z;
>
> Yb= @(y) bessely(1,z);
> Y1b= @(y) bessely(0, z) - bessely(1, z)./z;
>
> Ya= @(y) bessely(1,y);
> Y1a= @(y) bessely(0, y) - bessely(1, y)./y;
>
> r= y.*((JR1a.*Y1b-JR1b.*Y1a)./(JRa.*Y1b-JR1b.*Ya))
>
> Error:
>
> ??? Undefined function or method 'times' for input arguments of type
> 'function_handle'.

That's correct -- multiplication is NOT defined for function handles. Multiplication is defined for the values returned by function handles when they're evaluated, however.

r= @(y) y.*((JR1a(y).*Y1b(y)-JR1b(y).*Y1a(y))./(JRa(y).*Y1b(y)-JR1b(y).*Ya(y)))

Now you can plug in whatever value of y you want:

y = 2;
r(y)

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

Subject: Undefined function for function_handle

From: Nicholas Tan

Date: 1 Dec, 2010 10:15:04

Message: 3 of 4

"Steven Lord" <slord@mathworks.com> wrote in message <icrjn8$a2r$1@fred.mathworks.com>...
> "Nicholas Tan" <cytan89@yahoo.com> wrote in message <icpr08$dqv$1@fred.mathworks.com>...
> > I need to find the roots of an equation later on. Because of my equation, r is too long, i have to separate the terms in a few parts. However, by doing this, it shows an error:
> >
> >
> > syms y;
> > syms z;
>
> None of the rest of your code treats y or z as symbolic variables; remove the previous two lines.
>
> > JRa= @(y) besselj(1,y);
> > JR1a= @(y) besselj(0, y) - besselj(1, y)./y;
> >
> > JRb= @(y) besselj(1,z);
> > JR1b= @(y) besselj(0, z) - besselj(1, z)./z;
> >
> > Yb= @(y) bessely(1,z);
> > Y1b= @(y) bessely(0, z) - bessely(1, z)./z;
> >
> > Ya= @(y) bessely(1,y);
> > Y1a= @(y) bessely(0, y) - bessely(1, y)./y;
> >
> > r= y.*((JR1a.*Y1b-JR1b.*Y1a)./(JRa.*Y1b-JR1b.*Ya))
> >
> > Error:
> >
> > ??? Undefined function or method 'times' for input arguments of type
> > 'function_handle'.
>
> That's correct -- multiplication is NOT defined for function handles. Multiplication is defined for the values returned by function handles when they're evaluated, however.
>
> r= @(y) y.*((JR1a(y).*Y1b(y)-JR1b(y).*Y1a(y))./(JRa(y).*Y1b(y)-JR1b(y).*Ya(y)))
>
> Now you can plug in whatever value of y you want:
>
> y = 2;
> r(y)
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on http://www.mathworks.com



Thanks for your reply. For JRb, JR1b, Yb, Y1b, since the variable is z, should i use @(z) instead of @(y) of function handle?

To proceed with finding the roots of these complicated equations, I have to come out with a quadratic equation:

JRa= @(y) besselj(1,y);
JR1a= @(y) besselj(0, y) - besselj(1, y)./y;
 
JRb= @(y) besselj(1,z);
JR1b= @(y) besselj(0, z) - besselj(1, z)./z;

Yb= @(y) bessely(1,z);
Y1b= @(y) bessely(0, z) - bessely(1, z)./z;
 
Ya= @(y) bessely(1,y);
Y1a= @(y) bessely(0, y) - bessely(1, y)./y;
 
r= @(y) y.*((JR1a(y).*Y1b(y)-JR1b(y).*Y1a(y))./(JRa(y).*Y1b(y)-JR1b(y).*Ya(y)));
s=@(y) y.*((JR1a(y).*Yb(y)-JRb(y).*Y1a(y))./(JRa(y).*Y1b(y)-JRb(y).*Ya(y)));

P=@(y) 2.7*y;
Q=@(y) -((y)^2*(3.6.*s+3.3.*r));
R=@(y) ((1/y)^2)*((2.2*(k1a)^4).*r.*s-(2.7*(y^4)-4.9*(y^2)*((y)^2)+2.2));
 
g= ((-Q+sqrt(Q.^2-4.*P.*R))/2*P)
      ...

for equation g above, should i include @(y) as well?

thank you very much.
~Nicholas

Subject: Undefined function for function_handle

From: Steven_Lord

Date: 1 Dec, 2010 15:16:30

Message: 4 of 4



"Nicholas Tan" <cytan89@yahoo.com> wrote in message
news:id5778$627$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <icrjn8$a2r$1@fred.mathworks.com>...
>> "Nicholas Tan" <cytan89@yahoo.com> wrote in message
>> <icpr08$dqv$1@fred.mathworks.com>...
>> > I need to find the roots of an equation later on. Because of my
>> > equation, r is too long, i have to separate the terms in a few parts.
>> > However, by doing this, it shows an error:

*snip*

> Thanks for your reply. For JRb, JR1b, Yb, Y1b, since the variable is z,
> should i use @(z) instead of @(y) of function handle?

Yes.

> To proceed with finding the roots of these complicated equations, I have
> to come out with a quadratic equation:
>
> JRa= @(y) besselj(1,y);
> JR1a= @(y) besselj(0, y) - besselj(1, y)./y;
>
> JRb= @(y) besselj(1,z);
> JR1b= @(y) besselj(0, z) - besselj(1, z)./z;

Note that if you change JRb to:

JRb = @(z) besselj(1, z);

then it is identical to JRa, as they will both be of the form:

@(someVariableName) besselj(1, someVariableName)

just with different names substituted for someVariableName. The same holds
for your JR1*, Y*, and Y1* functions.

> Yb= @(y) bessely(1,z);
> Y1b= @(y) bessely(0, z) - bessely(1, z)./z;
>
> Ya= @(y) bessely(1,y);
> Y1a= @(y) bessely(0, y) - bessely(1, y)./y;
>
> r= @(y)
> y.*((JR1a(y).*Y1b(y)-JR1b(y).*Y1a(y))./(JRa(y).*Y1b(y)-JR1b(y).*Ya(y)));
> s=@(y)
> y.*((JR1a(y).*Yb(y)-JRb(y).*Y1a(y))./(JRa(y).*Y1b(y)-JRb(y).*Ya(y)));
>
> P=@(y) 2.7*y;
> Q=@(y) -((y)^2*(3.6.*s+3.3.*r));

You can't multiple a scalar like 3.6 times a function handle.

> R=@(y) ((1/y)^2)*((2.2*(k1a)^4).*r.*s-(2.7*(y^4)-4.9*(y^2)*((y)^2)+2.2));

You can't multiply two function handles together.

> g= ((-Q+sqrt(Q.^2-4.*P.*R))/2*P)

You can't raise a function handle to a power.

> for equation g above, should i include @(y) as well?

Well, you want g to be a function of y, don't you? Then when you have a
specific value of y for which you want to calculate the values of g, you
would use:

myY = 1.5;
myGValue = g(myY)

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