Thread Subject: help

Subject: help

From: Sayanatn

Date: 18 Mar, 2010 19:13:06

Message: 1 of 12

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 ,

Subject: help

From: Sayanatn

Date: 18 Mar, 2010 19:35:19

Message: 2 of 12

"Sayanatn " <sayan.polo@gmail.com> wrote in message <hntu01$kqk$1@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

Subject: help

From: Steven Lord

Date: 18 Mar, 2010 20:57:04

Message: 3 of 12


"Sayanatn " <sayan.polo@gmail.com> wrote in message
news:hntv9n$hbi$1@fred.mathworks.com...
> "Sayanatn " <sayan.polo@gmail.com> wrote in message
> <hntu01$kqk$1@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@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: help

From: Sayanatn

Date: 19 Mar, 2010 21:09:04

Message: 4 of 12

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@mathworks.com> wrote in message <hnu42p$fqt$1@fred.mathworks.com>...
>
> "Sayanatn " <sayan.polo@gmail.com> wrote in message
> news:hntv9n$hbi$1@fred.mathworks.com...
> > "Sayanatn " <sayan.polo@gmail.com> wrote in message
> > <hntu01$kqk$1@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@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: help

From: Steven Lord

Date: 19 Mar, 2010 21:54:44

Message: 5 of 12


"Sayanatn " <sayan.polo@gmail.com> wrote in message
news:ho0p5g$c8b$1@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@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: help

From: Sayanatn

Date: 20 Mar, 2010 00:08:02

Message: 6 of 12

Posting what it showing

>> which -all fzero
C:\Users\name\Doc\MATLAB\fzero.m
C:\Program Files (x86)\MATLAB\R2008b\toolbox\matlab\funfun\fzero.m % Shadowed
>>
there is nothing named ja , now?

"Steven Lord" <slord@mathworks.com> wrote in message <ho0rqt$ppb$1@fred.mathworks.com>...
>
> "Sayanatn " <sayan.polo@gmail.com> wrote in message
> news:ho0p5g$c8b$1@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@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: help

From: Sayanatn

Date: 20 Mar, 2010 01:13:06

Message: 7 of 12

hey ,
thanks a lot i deleted the one with the same name and its working great ,,,thanks for your help i do appreciate it its just that i am learning this to work on a big equation i dont know if fzero will work for complesx equation .."Sayanatn " <sayan.polo@gmail.com> wrote in message <ho13l2$m3u$1@fred.mathworks.com>...
> Posting what it showing
>
> >> which -all fzero
> C:\Users\name\Doc\MATLAB\fzero.m
> C:\Program Files (x86)\MATLAB\R2008b\toolbox\matlab\funfun\fzero.m % Shadowed
> >>
> there is nothing named ja , now?
>
> "Steven Lord" <slord@mathworks.com> wrote in message <ho0rqt$ppb$1@fred.mathworks.com>...
> >
> > "Sayanatn " <sayan.polo@gmail.com> wrote in message
> > news:ho0p5g$c8b$1@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@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >

Subject: help

From: Sayanatn

Date: 20 Mar, 2010 01:44:03

Message: 8 of 12

Hey
I was trying to find complex zeroes by this code

% begin f.m
function y = f
fid = fopen('Newtons.dat','w');%%the file where the output will be saved,fopen opens a file which is writeable%%
y = fzero(@subfunF,-2i);
    fprintf(fid,'%15.12f \n',y);
    
fclose(fid)


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

but its showing error as
 
??? Error using ==> fzero at 334
Function value at starting guess must be finite and real.

Error in ==> f at 5
y = fzero(@subfunF,-2i);
 
>>

how should i fix it?
"Sayanatn " <sayan.polo@gmail.com> wrote in message <ho17f2$gch$1@fred.mathworks.com>...
> hey ,
> thanks a lot i deleted the one with the same name and its working great ,,,thanks for your help i do appreciate it its just that i am learning this to work on a big equation i dont know if fzero will work for complesx equation .."Sayanatn " <sayan.polo@gmail.com> wrote in message <ho13l2$m3u$1@fred.mathworks.com>...
> > Posting what it showing
> >
> > >> which -all fzero
> > C:\Users\name\Doc\MATLAB\fzero.m
> > C:\Program Files (x86)\MATLAB\R2008b\toolbox\matlab\funfun\fzero.m % Shadowed
> > >>
> > there is nothing named ja , now?
> >
> > "Steven Lord" <slord@mathworks.com> wrote in message <ho0rqt$ppb$1@fred.mathworks.com>...
> > >
> > > "Sayanatn " <sayan.polo@gmail.com> wrote in message
> > > news:ho0p5g$c8b$1@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@mathworks.com
> > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> > >

Subject: help

From: Sayanatn

Date: 20 Mar, 2010 03:24:03

Message: 9 of 12

I am sorry to bother again but i am in real fixation how to do the previous posted message and how to calcualte the zeroes of
x^3+2*x^2+x+4i=0

depending upon the initial guess i am not getting anyresult even if the constant term is non imaginary (i.e 4i=4)Please please help me how to proceeed


Regards

SD
"Sayanatn " <sayan.polo@gmail.com> wrote in message <ho1993$cat$1@fred.mathworks.com>...
> Hey
> I was trying to find complex zeroes by this code
>
> % begin f.m
> function y = f
> fid = fopen('Newtons.dat','w');%%the file where the output will be saved,fopen opens a file which is writeable%%
> y = fzero(@subfunF,-2i);
> fprintf(fid,'%15.12f \n',y);
>
> fclose(fid)
>
>
> function z = subfunF(x)
> z=x.^2+2*x-4i;
> %%z = x.^3-2*x -5;
> % end f.m
>
> but its showing error as
>
> ??? Error using ==> fzero at 334
> Function value at starting guess must be finite and real.
>
> Error in ==> f at 5
> y = fzero(@subfunF,-2i);
>
> >>
>
> how should i fix it?
> "Sayanatn " <sayan.polo@gmail.com> wrote in message <ho17f2$gch$1@fred.mathworks.com>...
> > hey ,
> > thanks a lot i deleted the one with the same name and its working great ,,,thanks for your help i do appreciate it its just that i am learning this to work on a big equation i dont know if fzero will work for complesx equation .."Sayanatn " <sayan.polo@gmail.com> wrote in message <ho13l2$m3u$1@fred.mathworks.com>...
> > > Posting what it showing
> > >
> > > >> which -all fzero
> > > C:\Users\name\Doc\MATLAB\fzero.m
> > > C:\Program Files (x86)\MATLAB\R2008b\toolbox\matlab\funfun\fzero.m % Shadowed
> > > >>
> > > there is nothing named ja , now?
> > >
> > > "Steven Lord" <slord@mathworks.com> wrote in message <ho0rqt$ppb$1@fred.mathworks.com>...
> > > >
> > > > "Sayanatn " <sayan.polo@gmail.com> wrote in message
> > > > news:ho0p5g$c8b$1@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@mathworks.com
> > > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> > > >

Subject: help

From: Matt Fig

Date: 20 Mar, 2010 03:53:04

Message: 10 of 12

Please don't top post.

Since you have a polynomial, use roots.

f = @(x) x.^3+2*x.^2+x+4*i;
rt = roots([1 2 1 4*i])
f(rt) % Just to check

Subject: help

From: Sayanatn

Date: 20 Mar, 2010 07:12:05

Message: 11 of 12

Steve lord i need your help on the question i have and thanks matt,but i guess steve is handling my case and he will better understand my problem , i know you might be right but honestly i am a rookie and i didnt get it.sorry for toping the post.I need an explaination of the code and how will i write it ...

"Matt Fig" <spamanon@yahoo.com> wrote in message <ho1gr0$sgd$1@fred.mathworks.com>...
> Please don't top post.
>
> Since you have a polynomial, use roots.
>
> f = @(x) x.^3+2*x.^2+x+4*i;
> rt = roots([1 2 1 4*i])
> f(rt) % Just to check

Subject: help

From: Walter Roberson

Date: 20 Mar, 2010 07:50:59

Message: 12 of 12

Sayanatn wrote:
> Hey
> I was trying to find complex zeroes by this code
> % begin f.m
> function y = f
> fid = fopen('Newtons.dat','w');%%the file where the output will be
> saved,fopen opens a file which is writeable%%
> y = fzero(@subfunF,-2i);
> fprintf(fid,'%15.12f \n',y);
> fclose(fid)
>
>
> function z = subfunF(x)
> z=x.^2+2*x-4i;
> %%z = x.^3-2*x -5;
> % end f.m
>
> but its showing error as
>
> ??? Error using ==> fzero at 334
> Function value at starting guess must be finite and real.

fzero is not designed to work with complex numbers. It has to choose a
direction, left or right, to look for the next potential sign change,
but complex numbers have four possible directions, greater or lesser on
the real axis and greater or lesser on the imaginary axis.

For your particular quadradic function, you would be better off using
the exact method, that the roots are at (-b +/- sqrt(b^2 - 4*a*c))/(2*a)

Likewise there are exact solutions for cubic functions; for

x^3+b*x^2+c*x+d = 0, then the roots can be found as:

P = -8*b^3 + 36*b*c - 108*d + 12*sqrt(12*b^3*d - 3*b^2*c^2 - 54*b*c*d +
12*c^3 + 81*d^2)

t1 = -1/12*P^(1/3) + 3*(1/3*c - 1/9*b^2)/P^(1/3) - 1/3*b
t2 = 1/2*I*3^(1/2)*(1/6*P^(1/3) + 6*(1/3*c - 1/9*b^2)/P^(1/3))

R1 = 1/6*P^(1/3)-6*(1/3*c-1/9*b^2)/P^(1/3)-1/3*b
R2 = t1 + t2
R3 = t1 - t2

If for some reason you are required to use root-finding mechanism
instead of the exact solution, then I suggest you use a minimizer on a
function of two variables, with x = x1 + i*x2 . You will, however, have
to think carefully about what quantity you will minimize in order to
find the roots. abs(z) is tempting and always 0 or a positive real, but
I am not convinced it will properly explore the full complex space.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com