From: Sayanatn on
I am barely starting amtlab and i am trying to figure out how f zero is working

on a new m file i am writing this code


function f = myfun(z)

z = fzero(@x^2-4,2);
z

but there is an error occuring
??? Undefined function or method 'mpower' for input arguments of type 'function_handle'.

Error in ==> myfun at 5
z = fzero(@x^2-4,2);


pleas let me know why, and how should i proceed ,
From: Sayanatn on
"Sayanatn " <sayan.polo(a)gmail.com> wrote in message <hntu01$kqk$1(a)fred.mathworks.com>...
> I am barely starting amtlab and i am trying to figure out how f zero is working
>
> on a new m file i am writing this code
>
>
> function f = myfun(z)
>
> z = fzero(@x^2-4,2);
> z
>
> but there is an error occuring
> ??? Undefined function or method 'mpower' for input arguments of type 'function_handle'.
>
> Error in ==> myfun at 5
> z = fzero(@x^2-4,2);
>
>
> pleas let me know why, and how should i proceed ,

one more querry regarding this
i had been trying another example
on m file
function y = f(x)
y = x.^3-2*x-5;
%%To find the zero near 2
z = fzero('f',2)
the error was
??? Input argument "x" is undefined.

Error in ==> f at 3
y = x.^3-2*x-5;

hope to hear from you guys soon

SD
From: Steven Lord on

"Sayanatn " <sayan.polo(a)gmail.com> wrote in message
news:hntv9n$hbi$1(a)fred.mathworks.com...
> "Sayanatn " <sayan.polo(a)gmail.com> wrote in message
> <hntu01$kqk$1(a)fred.mathworks.com>...
>> I am barely starting amtlab and i am trying to figure out how f zero is
>> working
>>
>> on a new m file i am writing this code
>>
>>
>> function f = myfun(z)
>> z = fzero(@x^2-4,2);
>> z
>>
>> but there is an error occuring ??? Undefined function or method 'mpower'
>> for input arguments of type 'function_handle'.
>>
>> Error in ==> myfun at 5
>> z = fzero(@x^2-4,2);
>> pleas let me know why, and how should i proceed ,

The command @x^2 attempts to take a function handle to a function named x:

@x

and raise the function handle to the second power. This doesn't work, and
wouldn't really make sense. What you want instead is a function handle to a
function that accepts an input argument x and returns that value squared.
You can do this two ways, assuming you're using version 7.0 (release R14) or
later of MATLAB. The first is to write an "anonymous function"

fh = @(x) x.^2;

and pass that anonymous function into FZERO -- "z = fzero(fh, 2)". The
Functions portion of the Getting Started chapter in the documentation for
MATLAB discusses anonymous functions; I strongly recommend you read through
at least that section, and preferably the whole Getting Started chapter,
before going too much further.

The other alternative is to write a regular function that accepts an input x
and returns x.^2. I see you tried this but had some difficulty ...

> one more querry regarding this
> i had been trying another example on m file
> function y = f(x)
> y = x.^3-2*x-5;
> %%To find the zero near 2
> z = fzero('f',2)
> the error was
> ??? Input argument "x" is undefined.
>
> Error in ==> f at 3
> y = x.^3-2*x-5;

Let me guess ... the line where you call FZERO was inside the function f?
Don't do that. If you do, then f will call FZERO which will call f which
will call FZERO which will call f which will call FZERO which will call f
which will call FZERO ... I think you get the picture. Instead, put your
FZERO call inside another function, or run it at the command line.

% begin f.m
function y = f
y = fzero(@subfunF, 2)

function z = subfunF(x)
z = x.^3-2*x-5;
% end f.m

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Sayanatn on
I tried to do the following


% begin f.m
function y = f
y = fzero(@subfunF, 2);

function z = subfunF(x)
z = x.^3-2*x-5;
% end f.m

but the error was still there as follows

??? Error: File: fzero.m Line: 1 Column: 8
Unexpected MATLAB expression.

Error in ==> f at 4
y = fzero(@subfunF, 2);

>>

what should i do , ia m reading getting started


"Steven Lord" <slord(a)mathworks.com> wrote in message <hnu42p$fqt$1(a)fred.mathworks.com>...
>
> "Sayanatn " <sayan.polo(a)gmail.com> wrote in message
> news:hntv9n$hbi$1(a)fred.mathworks.com...
> > "Sayanatn " <sayan.polo(a)gmail.com> wrote in message
> > <hntu01$kqk$1(a)fred.mathworks.com>...
> >> I am barely starting amtlab and i am trying to figure out how f zero is
> >> working
> >>
> >> on a new m file i am writing this code
> >>
> >>
> >> function f = myfun(z)
> >> z = fzero(@x^2-4,2);
> >> z
> >>
> >> but there is an error occuring ??? Undefined function or method 'mpower'
> >> for input arguments of type 'function_handle'.
> >>
> >> Error in ==> myfun at 5
> >> z = fzero(@x^2-4,2);
> >> pleas let me know why, and how should i proceed ,
>
> The command @x^2 attempts to take a function handle to a function named x:
>
> @x
>
> and raise the function handle to the second power. This doesn't work, and
> wouldn't really make sense. What you want instead is a function handle to a
> function that accepts an input argument x and returns that value squared.
> You can do this two ways, assuming you're using version 7.0 (release R14) or
> later of MATLAB. The first is to write an "anonymous function"
>
> fh = @(x) x.^2;
>
> and pass that anonymous function into FZERO -- "z = fzero(fh, 2)". The
> Functions portion of the Getting Started chapter in the documentation for
> MATLAB discusses anonymous functions; I strongly recommend you read through
> at least that section, and preferably the whole Getting Started chapter,
> before going too much further.
>
> The other alternative is to write a regular function that accepts an input x
> and returns x.^2. I see you tried this but had some difficulty ...
>
> > one more querry regarding this
> > i had been trying another example on m file
> > function y = f(x)
> > y = x.^3-2*x-5;
> > %%To find the zero near 2
> > z = fzero('f',2)
> > the error was
> > ??? Input argument "x" is undefined.
> >
> > Error in ==> f at 3
> > y = x.^3-2*x-5;
>
> Let me guess ... the line where you call FZERO was inside the function f?
> Don't do that. If you do, then f will call FZERO which will call f which
> will call FZERO which will call f which will call FZERO which will call f
> which will call FZERO ... I think you get the picture. Instead, put your
> FZERO call inside another function, or run it at the command line.
>
> % begin f.m
> function y = f
> y = fzero(@subfunF, 2)
>
> function z = subfunF(x)
> z = x.^3-2*x-5;
> % end f.m
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
From: Steven Lord on

"Sayanatn " <sayan.polo(a)gmail.com> wrote in message
news:ho0p5g$c8b$1(a)fred.mathworks.com...
>I tried to do the following
>
>
> % begin f.m
> function y = f
> y = fzero(@subfunF, 2);
>
> function z = subfunF(x)
> z = x.^3-2*x-5;
> % end f.m
>
> but the error was still there as follows
>
> ??? Error: File: fzero.m Line: 1 Column: 8
> Unexpected MATLAB expression.
>
> Error in ==> f at 4
> y = fzero(@subfunF, 2);
>
>>>
>
> what should i do , ia m reading getting started

Type the following command and post what it says:

which -all fzero

If the first instance that comes up is in a directory named ja, remove all
directories named ja from your path using the File -> Set Path menu item.
The files in the /ja directories are Japanese help text files; they don't
contain the actual function code, and should not be on your path.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ